Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c1e8183

Browse filesBrowse files
committed
bug #23333 [PropertyAccess] Fix TypeError discard (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [PropertyAccess] Fix TypeError discard | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? |no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Given the following code: ```php class Bar { private $foos = []; public function getFoos(): array { return 'It doesn\'t respect the return type'; } public function addFoo(Foo $foo) { // ... } public function removeFoo(Foo $dateTime) { // ... } } $object = new Bar(); $this->propertyAccessor->setValue($object, 'foos', array(new \DateTime())); ``` The `PropertyAccessor` will crash (`[Symfony\Component\Debug\Exception\ContextErrorException] Notice: Undefined offset: 0`) instead of displaying the (valid) PHP error. This PR fixes the issue. Commits ------- e0c5040 [PropertyAccess] Fix TypeError discard
2 parents 44c3f58 + e0c5040 commit c1e8183
Copy full SHA for c1e8183

File tree

3 files changed

+45
-1
lines changed
Filter options

3 files changed

+45
-1
lines changed

‎src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyAccess/PropertyAccessor.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public static function handleError($type, $message, $file, $line, $context)
245245

246246
private static function throwInvalidArgumentException($message, $trace, $i)
247247
{
248-
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file']) {
248+
if (isset($trace[$i]['file']) && __FILE__ === $trace[$i]['file'] && isset($trace[$i]['args'][0])) {
249249
$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
250250
$pos += strlen($delim);
251251
$type = $trace[$i]['args'][0];
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class ReturnTyped
18+
{
19+
public function getFoos(): array
20+
{
21+
return 'It doesn\'t respect the return type on purpose';
22+
}
23+
24+
public function addFoo(\DateTime $dateTime)
25+
{
26+
}
27+
28+
public function removeFoo(\DateTime $dateTime)
29+
{
30+
}
31+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1616
use Symfony\Component\PropertyAccess\PropertyAccessor;
17+
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
1718
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
1819
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
1920
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;
@@ -566,4 +567,16 @@ public function testThrowTypeErrorWithInterface()
566567

567568
$this->propertyAccessor->setValue($object, 'countable', 'This is a string, \Countable expected.');
568569
}
570+
571+
/**
572+
* @requires PHP 7
573+
*
574+
* @expectedException \TypeError
575+
*/
576+
public function testDoNotDiscardReturnTypeError()
577+
{
578+
$object = new ReturnTyped();
579+
580+
$this->propertyAccessor->setValue($object, 'foos', array(new \DateTime()));
581+
}
569582
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.