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 parameter after attribute incorrectly tokenized as ternary operator #3445

Closed
mhlsf opened this issue Oct 5, 2021 · 1 comment · Fixed by #3446
Closed

Nullable parameter after attribute incorrectly tokenized as ternary operator #3445

mhlsf opened this issue Oct 5, 2021 · 1 comment · Fixed by #3446

Comments

@mhlsf
Copy link

mhlsf commented Oct 5, 2021

Describe the bug
On a method with a PHP 8 attribute before a nullable parameter, cs if forcing to have a space after the ? of a nullable the nullable parameter

Code sample

public function setDefault(#[ImportValue(
        column: 'Material by default',
        transformer: YesNoBooleanTransformer::class,
        constraints: [
            [
                Assert\Type::class,
                ['type' => 'bool'],
            ],
        ]
    )] ?bool $value = null): void
    {
        $this->setBool('default', $value);
    }

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
 451 | ERROR | [x] Expected at least 1 space after "?"; 0 found
     |       |     (PSR12.Operators.OperatorSpacing.NoSpaceAfter)
 451 | ERROR | [x] Parameter $value has null default value, but is not marked as nullable.
     |       |     (SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue.NullabilityTypeMissing)

Expected behavior
We should be able to put a nullable parameter after a PHP 8 parameter attribute. Here cs is confusing the PHP8 attribute and the bool as a ternary operator, thus forcing the space after the "?" causing the "Parameter $value has null default value" because this work fine :

public function setDefault(#[ImportValue(
        column: 'Material by default',
        transformer: YesNoBooleanTransformer::class,
        constraints: [
            [
                Assert\Type::class,
                ['type' => 'bool'],
            ],
        ]
    )] ? bool $value): void
    {
        $this->setBool('default', $value);
    }

Versions (please complete the following information):

  • PHP: 8.0
  • PHPCS: 3.5
  • Standard: PSR2, PSR12, MySource, PSR1, Squiz, PEAR, Zend, Doctrine and SlevomatCodingStandard
@jrfnl
Copy link
Contributor

jrfnl commented Oct 6, 2021

@mhlsf Thank you for reporting this with a usable code sample. I've been able to reproduce this and have submitted PR #3446 to fix this. If you have the chance, testing appreciated.

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Oct 11, 2021
@gsherwood gsherwood added this to the 3.6.1 milestone Oct 11, 2021
@gsherwood gsherwood changed the title Confusing nullable parameter as ternary operator Nullable parameter incorrectly tokenized as ternary operator Oct 11, 2021
@gsherwood gsherwood changed the title Nullable parameter incorrectly tokenized as ternary operator Nullable parameter after attribute incorrectly tokenized as ternary operator Oct 11, 2021
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging a pull request may close this issue.

3 participants