8000 [PropertyAccess] Handle interfaces in the invalid argument exception by fancyweb · Pull Request #21360 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Handle interfaces in the invalid argument exception #21360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static function handleError($type, $message, $file, $line, $context)
private static function throwInvalidArgumentException($message, $trace, $i)
{
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file']) {
$pos = strpos($message, $delim = 'must be of the type ') ?: strpos($message, $delim = 'must be an instance of ');
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
$pos += strlen($delim);
$type = $trace[$i]['args'][0];
$type = is_object($type) ? get_class($type) : gettype($type);
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/TypeHinted.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class TypeHinted
{
private $date;

/**
* @var \Countable
*/
private $countable;

public function setDate(\DateTime $date)
{
$this->date = $date;
Expand All @@ -27,4 +32,20 @@ public function getDate()
{
return $this->date;
}

/**
* @return \Countable
*/
public function getCountable()
{
return $this->countable;
}

/**
* @param \Countable $countable
*/
public function setCountable(\Countable $countable)
{
$this->countable = $countable;
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,15 @@ public function testArrayNotBeeingOverwritten()
$this->assertSame('baz', $this->propertyAccessor->getValue($object, 'publicAccessor[value2]'));
$this->assertSame(array('value1' => 'foo', 'value2' => 'baz'), $object->getPublicAccessor());
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected argument of type "Countable", "string" given
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected a test checking for the new "must implement interface" string in the exception message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it : the "must implement interface" comes from the PHP exception that is handled and then the InvalidArgumentException is thrown.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you are right. I misread the changes you made.

*/
public function testThrowTypeErrorWithInterface()
{
$object = new TypeHinted();

$this->propertyAccessor->setValue($object, 'countable', 'This is a string, \Countable expected.');
}
}
0