8000 bug #45306 [PropertyAccessor] Add missing TypeError catch (b1rdex) · symfony/symfony@d3df7be · GitHub
[go: up one dir, main page]

Skip to content

Commit d3df7be

Browse files
bug #45306 [PropertyAccessor] Add missing TypeError catch (b1rdex)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyAccessor] Add missing TypeError catch | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #45199 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This bugfix wraps `TypeError: Cannot assign TYPE to property *::$* of type TYPE` with `InvalidArgumentException`. <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against the latest branch. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- db257f3 [PropertyAccessor] Add missing TypeError catch
2 parents c2e179e + db257f3 commit d3df7be

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ private static function throwInvalidArgumentException(string $message, array $tr
188188
}
189189

190190
if (\PHP_VERSION_ID < 80000) {
191+
if (preg_match('/^Typed property \S+::\$\S+ must be (\S+), (\S+) used$/', $message, $matches)) {
192+
[, $expectedType, $actualType] = $matches;
193+
194+
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
195+
}
196+
191197
if (!str_starts_with($message, 'Argument ')) {
192198
return;
193199
}
@@ -204,6 +210,11 @@ private static function throwInvalidArgumentException(string $message, array $tr
204210
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
205211
[, $expectedType, $actualType] = $matches;
206212

213+
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
214+
}
215+
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
216+
[, $actualType, $expectedType] = $matches;
217+
207218
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
208219
}
209220
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
class TestClassTypedProperty
15+
{
16+
public float $publicProperty;
17+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
3030
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicGet;
3131
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassSetValue;
32+
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassTypedProperty;
3233
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassTypeErrorInsideCall;
3334
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestPublicPropertyDynamicallyCreated;
3435
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestPublicPropertyGetterOnObject;
@@ -945,4 +946,16 @@ public function testGetDynamicPublicPropertyWithMagicGetterAllow()
945946
$object = new TestPublicPropertyGetterOnObjectMagicGet();
946947
$this->assertSame($value, $this->propertyAccessor->getValue($object, $path));
947948
}
949+
950+
/**
951+
* @requires PHP 7.4
952+
*/
953+
public function testSetValueWrongTypeShouldThrowWrappedException()
954+
{
955+
$object = new TestClassTypedProperty();
956+
957+
$this->expectException(InvalidArgumentException::class);
958+
$this->expectExceptionMessage('Expected argument of type "float", "string" given at property path "publicProperty"');
959+
$this->propertyAccessor->setValue($object, 'publicProperty', 'string');
960+
}
948961
}

0 commit comments

Comments
 (0)
0