Skip to content

Commit a923e6c

Browse files
bug #62772 [PropertyInfo] Fix calling same-named method with required args instead of reading public property (makraz)
This PR was submitted for the 7.3 branch but it was merged into the 6.4 branch instead. Discussion ---------- [PropertyInfo] Fix calling same-named method with required args instead of reading public property | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62733 | License | MIT - Fixes PropertyAccess choosing a same‑named method over a public property when the method requires parameters. - Ensures jQuery‑style accessors (methods named exactly like the property) are only considered if they have zero required parameters. - Prevents `ArgumentCountError` during serialization and aligns behavior with standard getter rules (`getX()/isX()/hasX()/canX()`), which already require zero required parameters. Commits ------- 02e3153 [PropertyInfo] Fix calling same-named method with required args instead of reading public property
2 parents acbd949 + 02e3153 commit a923e6c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ protected function setUp(): void
5252
$this->propertyAccessor = new PropertyAccessor();
5353
}
5454

55+
public function testPrefersPropertyOverMethodWithSameNameAndRequiredArgs()
56+
{
57+
$obj = new class {
58+
public bool $loaded = true;
59+
60+
// Same name as property, but requires an argument: must NOT be called for reading
61+
public function loaded(string $arg): bool
62+
{
63+
throw new \RuntimeException('Method should not be invoked during property read');
64+
}
65+
};
66+
67+
$this->assertTrue($this->propertyAccessor->getValue($obj, 'loaded'));
68+
}
69+
5570
public static function getPathsWithMissingProperty()
5671
{
5772
return [

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ public function getReadInfo(string $class, string $property, array $context = []
270270

271271
if ($allowGetterSetter && $reflClass->hasMethod($getsetter) && ($reflClass->getMethod($getsetter)->getModifiers() & $this->methodReflectionFlags)) {
272272
$method = $reflClass->getMethod($getsetter);
273-
274-
return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD, $getsetter, $this->getReadVisibilityForMethod($method), $method->isStatic(), false);
273+
// Only consider jQuery-style accessors when they don't require parameters
274+
if (!$method->getNumberOfRequiredParameters()) {
275+
return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD, $getsetter, $this->getReadVisibilityForMethod($method), $method->isStatic(), false);
276+
}
275277
}
276278

277279
if ($allowMagicGet && $reflClass->hasMethod('__get') && (($r = $reflClass->getMethod('__get'))->getModifiers() & $this->methodReflectionFlags)) {

0 commit comments

Comments
 (0)