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

nullable_type_declaration_for_default_null_value fails for nullable readonly parameter in promoted constructor #6221

Closed
krzysztofrewak opened this issue Jan 4, 2022 · 2 comments · Fixed by #6224
Labels

Comments

@krzysztofrewak
Copy link
Contributor

krzysztofrewak commented Jan 4, 2022

I think NullableTypeDeclarationForDefaultNullValueFixer crashes when I'm trying to add readonly modifier to nullable parameter in promoted constructor.

Bug report

  • PHP version: PHP 8.1.0 (cli) (built: Nov 30 2021 07:26:16) (NTS)
  • PHP CS Fixer version: PHP CS Fixer 3.4.0 Si! by Fabien Potencier and Dariusz Ruminski
  • the command used to run PHP CS Fixer (run with -vvv):
PHP CS Fixer 3.4.0 Si! by Fabien Potencier and Dariusz Ruminski
Runtime: PHP 8.1.0
Loaded config default.
Using cache file ".php-cs-fixer.cache".
E                                                                                                                                                                                                                                                                                                           1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error

Fixed all files in 0.004 seconds, 12.000 MB memory used

Files that were not fixed due to errors reported during linting after fixing:
   1) /application/src/ReadonlyThing.php

                                                                            
        [PhpCsFixer\Linter\LintingException]                                
        Parse error: syntax error, unexpected token "readonly" on line 11.  
                                                                            

      Applied fixers: nullable_type_declaration_for_default_null_value
ERROR: 64
  • the configuration (file) you used: none

Code snippet that reproduces the problem

<?php

class ReadonlyThing
{
    public function __construct(
        protected readonly bool $nullable = null,
    ) {
    }
}

Expected output

<?php

class ReadonlyThing
{
    public function __construct(
        protected readonly ?bool $nullable = null,
    ) {
    }
}

Btw. when it's used with easy-coding-standard, it's even worse:

<?php

class ReadonlyThing
{
    public function __construct(
        protected ?readonly ?bool $nullable = null,
    ) {
    }
}
@SpacePossum
Copy link
Contributor

Thanks for reporting this issue to us.

Looking at your example I wonder why the tool tries to fix the code as it does not lint:

$ sudo docker-compose run --rm --user "$(id --user):$(id --group)" php php -l test2.php
Fatal error: Cannot use null as default value for parameter $nullable of type bool in test2.php on line 5
$ sudo docker-compose run --rm --user "$(id --user):$(id --group)" php php -v
PHP 8.1.0 (cli) (built: Dec  2 2021 12:47:04) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies

@SpacePossum
Copy link
Contributor

to clarify; it does "fix" the following correct PHP incorrectly so there is indeed a bug in the tool

<?php
class Foo
{
    public function __construct(
        protected readonly ?bool $nullable = null,
    ) {}
}

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

Successfully merging a pull request may close this issue.

2 participants