Skip to content

Commit

Permalink
bug #32466 [Config] Fix for signatures of typed properties (tvandervorm)
Browse files Browse the repository at this point in the history
This PR was submitted for the 4.3 branch but it was squashed and merged into the 3.4 branch instead (closes #32466).

Discussion
----------

[Config] Fix for signatures of typed properties

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32465
| License       | MIT
| Doc PR        | -

Also see the issue description, when using public typed properties ([new in PHP7.4](https://wiki.php.net/rfc/typed_properties_v2)) like this:

```
namespace App;

class Foo {
    public int $bar;
}
```

will cause `$defaults['bar']` not to be set in Symfony/Component/Config/Resource/ReflectionClassResource.php::139.

This is because `$bar` doesn't have a default value, but does have a type hint, meaning it's default value is not `null` but undefined. This causes an 'undefined index' error when clearing the cache through `bin/console cache:clear` when running PHP7.4.

The default value is used here for the class signature, having `null` should be appropriate for all cases.

Commits
-------

bad2a2c [Config] Fix for signatures of typed properties
  • Loading branch information
fabpot committed Jul 15, 2019
2 parents 0825ea7 + bad2a2c commit e347e41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -140,7 +140,7 @@ private function generateSignature(\ReflectionClass $class)

foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $p) {
yield $p->getDocComment().$p;
yield print_r($defaults[$p->name], true);
yield print_r($defaults[$p->name] ?? null, true);
}
}

Expand Down
Expand Up @@ -137,6 +137,14 @@ public function provideHashedSignature()
yield [1, 13, 'protected function prot($a = [123]) {}'];
yield [0, 14, '/** priv docblock */'];
yield [0, 15, ''];

if (\PHP_VERSION_ID >= 70400) {
// PHP7.4 typed properties without default value are
// undefined, make sure this doesn't throw an error
yield [1, 5, 'public array $pub;'];
yield [0, 7, 'protected int $prot;'];
yield [0, 9, 'private string $priv;'];
}
}

public function testEventSubscriber()
Expand Down

0 comments on commit e347e41

Please sign in to comment.