Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] PHP7.4 public typed properties cause cache clearing to crash #32465

Closed
tvandervorm opened this issue Jul 9, 2019 · 2 comments
Closed

Comments

@tvandervorm
Copy link
Contributor

tvandervorm commented Jul 9, 2019

Symfony version(s) affected: 4.*

Description

Clearing the cache when having a class with a public typed property (available from PHP 7.4) will cause the command to fail with the following error:

In ReflectionClassResource.php line 139:
                              
  [ErrorException]            
  Notice: Undefined index: bar  

Exception trace:
 () at /var/wwww/vendor/symfony/config/Resource/ReflectionClassResource.php:139
 Symfony\Component\Config\Resource\ReflectionClassResource->generateSignature() at /var/wwww/vendor/symfony/config/Resource/ReflectionClassResource.php:113
 Symfony\Component\Config\Resource\ReflectionClassResource->computeHash() at /var/wwww/vendor/symfony/config/Resource/ReflectionClassResource.php:52
 Symfony\Component\Config\Resource\ReflectionClassResource->isFresh() at /var/wwww/vendor/symfony/config/Resource/SelfCheckingResourceChecker.php:34
 Symfony\Component\Config\Resource\SelfCheckingResourceChecker->isFresh() at /var/wwww/vendor/symfony/config/ResourceCheckerConfigCache.php:99
 Symfony\Component\Config\ResourceCheckerConfigCache->isFresh() at /var/wwww/vendor/symfony/config/ConfigCache.php:60
 Symfony\Component\Config\ConfigCache->isFresh() at /var/wwww/vendor/symfony/http-kernel/Kernel.php:486
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at /var/wwww/vendor/symfony/http-kernel/Kernel.php:133
 Symfony\Component\HttpKernel\Kernel->boot() at /var/wwww/vendor/symfony/framework-bundle/Console/Application.php:159
 Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands() at /var/wwww/vendor/symfony/framework-bundle/Console/Application.php:65
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/wwww/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /var/wwww/bin/console:38

Where 'bar' is the name of the property. This is because the assumption is made that a default value is available for each property, which is null by default for untyped properties. However, for a typed property null is no longer a valid value, so it will be undefined instead. Undefined default values are missing from $class->getDefaultProperties(); when doing reflection, hence the 'undefined index' error.

How to reproduce

Generate a class like so:

namespace App;

class Foo {
    public int $bar;
} 

save it in src/Foo.php and run bin/console cache:clear. Requires PHP7.4

Possible Solution

The offending line is this one:

yield print_r($defaults[$p->name], true);

changing it so it accepts missing default values removes the error:

yield print_r($defaults[$p->name] ?? null, true);

Additional context

From the RFC:

Typed properties cannot have a null default value, unless the type is explicitly nullable (?Type). This is in contrast to parameter types, where a null default value automatically implies a nullable type. We consider this to be a legacy behavior, which we do not wish to support for newly introduced syntax.

tvandervorm added a commit to tvandervorm/symfony that referenced this issue Jul 9, 2019
@tvandervorm tvandervorm changed the title PHP7.4 public typed properties cause cache clearing to crash [Cache] PHP7.4 public typed properties cause cache clearing to crash Jul 9, 2019
@nicolas-grekas
Copy link
Member

PHP 7.4 is WIP. Would you like to submit a PR to fix this issue?

@fabpot fabpot closed this as completed Jul 15, 2019
fabpot added a commit that referenced this issue Jul 15, 2019
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
@ksiatka
Copy link

ksiatka commented Apr 24, 2023

Quick problem solving. (For example if you are upgrading from 3.4 -> 4.0)

Initialize variable with null directly:
public ?int $bar = null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants