Skip to content

Commit db257f3

Browse files
committed
[PropertyAccessor] Add missing TypeError catch
1 parent 582f572 commit db257f3

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 <[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 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)