Skip to content

[PropertyAccess] Do not silence TypeErrors from client code. #23100

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

Merged
merged 1 commit into from
Jun 14, 2017
Merged
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
}
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0);

// It wasn't thrown in this class so rethrow it
throw $e;
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't be this done in throwInvalidArgumentException instead?

Copy link
Member

Choose a reason for hiding this comment

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

no, because it is used elsewhere too

Copy link
Member

@nicolas-grekas nicolas-grekas Jun 9, 2017

Choose a reason for hiding this comment

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

shouldn't the other place also throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the other place is error handler callback for PHP 5, there is no exception to rethrow, and errors are passed correctly to the original handler (here)

} finally {
if (\PHP_VERSION_ID < 70000 && false !== self::$previousErrorHandler) {
restore_error_handler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class TestClassTypeErrorInsideCall
{
public function expectsDateTime(\DateTime $date)
{
}

public function getProperty()
{
}

public function setProperty($property)
{
$this->expectsDateTime(null); // throws TypeError
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassSetValue;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassIsWritable;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassTypeErrorInsideCall;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted;

class PropertyAccessorTest extends TestCase
Expand Down Expand Up @@ -578,4 +579,15 @@ public function testThrowTypeErrorWithInterface()

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

/**
* @requires PHP 7.0
* @expectedException \TypeError
*/
public function testThrowTypeErrorInsideSetterCall()
{
$object = new TestClassTypeErrorInsideCall();

$this->propertyAccessor->setValue($object, 'property', 'foo');
}
}