bug #39896 [PropertyInfo] Fix breaking change with has*(arguments...) methods (YaFou)

This PR was merged into the 5.1 branch.

Discussion
----------

[PropertyInfo] Fix breaking change with has*(arguments...) methods

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39885
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - 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 branch 5.x.
-->

Until 5.0:
```php
class Dummy
{
    private $elements;

    public function hasElement($element): bool
    {
        // ...
    }
}

$extractor = new ReflectionExtractor();
$extractor->isReadable('Dummy', 'element'); // false
```

After 5.0:
```php
class Dummy
{
    private $elements;

    public function hasElement($element): bool
    {
        // ...
    }
}

$extractor = new ReflectionExtractor();
$extractor->isReadable('Dummy', 'element'); // true => BREAKING CHANGE
```

Commits
-------

37cc16e3d8 [PropertyInfo] Fix breaking change with has*(arguments...) methods
This commit is contained in:
Christian Flothmann 2021-01-22 18:02:52 +01:00
commit de8ddfdfba
3 changed files with 9 additions and 1 deletions

View File

@ -239,7 +239,7 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
foreach ($this->accessorPrefixes as $prefix) {
$methodName = $prefix.$camelProp;
if ($reflClass->hasMethod($methodName) && ($reflClass->getMethod($methodName)->getModifiers() & $this->methodReflectionFlags)) {
if ($reflClass->hasMethod($methodName) && $reflClass->getMethod($methodName)->getModifiers() & $this->methodReflectionFlags && !$reflClass->getMethod($methodName)->getNumberOfRequiredParameters()) {
$method = $reflClass->getMethod($methodName);
return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD, $methodName, $this->getReadVisiblityForMethod($method), $method->isStatic(), false);

View File

@ -75,6 +75,7 @@ class ReflectionExtractorTest extends TestCase
'xTotals',
'YT',
'date',
'element',
'c',
'd',
'e',
@ -291,6 +292,7 @@ class ReflectionExtractorTest extends TestCase
['id', true],
['Guid', true],
['guid', false],
['element', false],
];
}

View File

@ -130,6 +130,8 @@ class Dummy extends ParentDummy
*/
public $nestedIterators;
private $elements;
public static function getStatic()
{
}
@ -218,4 +220,8 @@ class Dummy extends ParentDummy
public function addDate(\DateTime $date)
{
}
public function hasElement(string $element): bool
{
}
}