add property path to exception message when error writing property

This commit is contained in:
Rhodri Pugh 2018-06-12 16:19:40 +01:00
parent 5c338cc7ab
commit 473a025643
2 changed files with 4 additions and 4 deletions

View File

@ -151,14 +151,14 @@ class PropertyAccessor implements PropertyAccessorInterface
$value = $zval[self::VALUE];
}
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0);
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
private static function throwInvalidArgumentException($message, $trace, $i)
private static function throwInvalidArgumentException($message, $trace, $i, $propertyPath)
{
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && isset($trace[$i]['args'][0])) {
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
@ -166,7 +166,7 @@ class PropertyAccessor implements PropertyAccessorInterface
$type = $trace[$i]['args'][0];
$type = is_object($type) ? get_class($type) : gettype($type);
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given.', substr($message, $pos, strpos($message, ',', $pos) - $pos), $type));
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', substr($message, $pos, strpos($message, ',', $pos) - $pos), $type, $propertyPath));
}
}

View File

@ -529,7 +529,7 @@ class PropertyAccessorTest extends TestCase
/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected argument of type "DateTime", "string" given
* @expectedExceptionMessage Expected argument of type "DateTime", "string" given at property path "date"
*/
public function testThrowTypeError()
{