Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@tvandervorm

Description

@tvandervorm

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions