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

Arrow function not tokenized correctly when using null in union type #3316

Closed
simPod opened this issue Apr 22, 2021 · 1 comment
Closed

Arrow function not tokenized correctly when using null in union type #3316

simPod opened this issue Apr 22, 2021 · 1 comment

Comments

@simPod
Copy link

simPod commented Apr 22, 2021

Describe the bug

phpcs shoots:

PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket: Space before opening parenthesis of function call prohibited
Squiz.Arrays.ArrayDeclaration.IndexNoNewline: Each index in a multi-line array must be on a new line

when types are declared as union types. array|null triggers Squiz.Arrays.ArrayDeclaration.IndexNoNewline. ?array is fine.

Code sample

<?php

declare(strict_types=1);

return [
    'this is fine' => static fn (?DateTimeImmutable $value) : ?array => $value === null
        ? null
        : [],
    'this shoots false positives' => static fn (DateTimeImmutable|null $value) : array|null => $value === null
        ? null
        : [],
];

Custom ruleset

doctrine/coding-standard v9

To reproduce

 9 | ERROR | [x] Space before opening parenthesis of function call prohibited
   |       |     (PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket)
 9 | ERROR | [x] Each index in a multi-line array must be on a new line (Squiz.Arrays.ArrayDeclaration.IndexNoNewline)

Expected behavior

No errors.

Versions (please complete the following information):

  • OS: [MacOS]
  • PHP: [8.0]
  • PHPCS: [master]
  • Standard: [Doctrine]
@simPod simPod changed the title PHP 8_ False positives when using arrow function as an array value across multiple lines only when union types are used PHP 8: False positives when using arrow function as an array value when union types are used Apr 22, 2021
@gsherwood gsherwood added this to the 3.6.1 milestone Apr 22, 2021
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Apr 22, 2021
@gsherwood gsherwood changed the title PHP 8: False positives when using arrow function as an array value when union types are used Arrow function not tokenized correctly when using null in union type Apr 23, 2021
gsherwood added a commit that referenced this issue Apr 23, 2021
gsherwood added a commit that referenced this issue Apr 23, 2021
@gsherwood
Copy link
Member

This issue was caused by the arrow function tokenizing code not allowing for the null type in a type union. It appears this was the only place that had this issue in the codebase.

Thanks a lot for the bug report. The fix will be in 3.6.1.

PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Apr 23, 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

No branches or pull requests

2 participants