8000 bug #36904 [PropertyAccess] Parse php 8 TypeErrors correctly (derrabus) · symfony/symfony@e220e7c · GitHub
[go: up one dir, main page]

Skip to content

Commit e220e7c

Browse files
bug #36904 [PropertyAccess] Parse php 8 TypeErrors correctly (derrabus)
This PR was merged into the 3.4 branch. Discussion ---------- [PropertyAccess] Parse php 8 TypeErrors correctly | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #36872 | License | MIT | Doc PR | N/A The wording of `TypeError` messages has changed in php 8. Since `PropertyAccessor` parses those messages, the logic needed some adjustments. Commits ------- 7100c3c [PropertyAccess] Parse php 8 TypeErrors correctly.
2 parents 4e19168 + 7100c3c commit e220e7c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,15 @@ public static function handleError($type, $message, $file, $line, $context = [])
255255

256256
private static function throwInvalidArgumentException($message, $trace, $i, $previous = null)
257257
{
258-
// the type mismatch is not caused by invalid arguments (but e.g. by an incompatible return type hint of the writer method)
259-
if (0 !== strpos($message, 'Argument ')) {
258+
if (!isset($trace[$i]['file']) || __FILE__ !== $trace[$i]['file']) {
260259
return;
261260
}
262261

263-
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file']) {
262+
if (\PHP_VERSION_ID < 80000) {
263+
if (0 !== strpos($message, 'Argument ')) {
264+
return;
265+
}
266+
264267
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
265268
$pos += \strlen($delim);
266269
$j = strpos($message, ',', $pos);
@@ -269,6 +272,12 @@ private static function throwInvalidArgumentException($message, $trace, $i, $pre
269272

270273
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given.', $message, 'NULL' === $type ? 'null' : $type), 0, $previous);
271274
}
275+
276+
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
277+
list(, $expectedType, $actualType) = $matches;
278+
279+
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given.', $expectedType, 'NULL' === $actualType ? 'null' : $actualType), 0, $previous);
280+
}
272281
}
273282

274283
/**
@@ -471,7 +480,7 @@ private function readProperty($zval, $property)
471480
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
472481
} catch (\TypeError $e) {
473482
// handle uninitialized properties in PHP >= 7
474-
if (preg_match((sprintf('/^Return value of %s::%s\(\) must be of the type (\w+), null returned$/', preg_quote(\get_class($object)), $access[self::ACCESS_NAME])), $e->getMessage(), $matches)) {
483+
if (preg_match((sprintf('/^Return value of %s::%s\(\) must be of (?:the )?type (\w+), null returned$/', preg_quote(\get_class($object)), $access[self::ACCESS_NAME])), $e->getMessage(), $matches)) {
475484
throw new AccessException(sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', \get_class($object), $access[self::ACCESS_NAME], $matches[1]), 0, $e);
476485
}
477486

0 commit comments

Comments
 (0)
0