Skip to content

Commit dce2671

Browse files
committed
bug symfony#23100 [PropertyAccess] Do not silence TypeErrors from client code. (tsufeki)
This PR was merged into the 3.2 branch. Discussion ---------- [PropertyAccess] Do not silence TypeErrors from client code. | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Fixes TypeError silencing in `setValue()` when said error is thrown inside setter/adder/etc. An example is given in the included test, but more real-life story is botched accessors for a many-to-one association on a Doctrine entity: ```php class B { function setA(A $a) { ... } // forgotten "= null" here } class A { function removeB(B $b) { if ($this->bs->contains($b)) { $this->bs->removeElement($b); $b->setA(null); // TypeError thrown } return $this; } } ``` No error is shown to the user, even though removing doesn't work. This bug is not present in 2.7 & 2.8. Commits ------- 45b961d [PropertyAccess] Do not silence TypeErrors from client code.
2 parents aa94dd6 + 45b961d commit dce2671

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
244244
}
245245
} catch (\TypeError $e) {
246246
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0);
247+
248+
// It wasn't thrown in this class so rethrow it
249+
throw $e;
247250
} finally {
248251
if (\PHP_VERSION_ID < 70000 && false !== self::$previousErrorHandler) {
249252
restore_error_handler();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyAccess\Tests\Fixtures;
13+
14+
class TestClassTypeErrorInsideCall
15+
{
16+
public function expectsDateTime(\DateTime $date)
17+
{
18+
}
19+
20+
public function getProperty()
21+
{
22+
}
23+
24+
public function setProperty($property)
25+
{
26+
$this->expectsDateTime(null); // throws TypeError
27+
}
28+
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object;
2222
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassSetValue;
2323
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassIsWritable;
24+
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassTypeErrorInsideCall;
2425
use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted;
2526

2627
class PropertyAccessorTest extends TestCase
@@ -638,4 +639,14 @@ public function setFoo($foo)
638639

639640
return $obj;
640641
}
642+
643+
/**
644+
* @expectedException \TypeError
645+
*/
646+
public function testThrowTypeErrorInsideSetterCall()
647+
{
648+
$object = new TestClassTypeErrorInsideCall();
649+
650+
$this->propertyAccessor->setValue($object, 'property', 'foo');
651+
}
641652
}

0 commit comments

Comments
 (0)