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 Typehint is not detected as nullable #3782

Closed
wendelb opened this issue Mar 27, 2023 · 2 comments
Closed

Nullable Typehint is not detected as nullable #3782

wendelb opened this issue Mar 27, 2023 · 2 comments

Comments

@wendelb
Copy link

wendelb commented Mar 27, 2023

Describe the bug
A clear and concise description of what the bug is.

Code sample

class MyClass {
    private function myFunction(ClassA|ClassB|null $param): bool {
    }
}

To reproduce
Steps to reproduce the behavior:

  1. Acquire the method parameters in a Sniff that is registered for functions (\PHP_CodeSniffer\Files\File::getMethodParameters)
  2. Test if the only parameter is nullable

Actual behavior
The Type Hint is detected as ClassA|ClassB|null, but the property "nullable_type" is set to false.

Expected behavior
PHPCS should be able to detect that the $param parameter is nullable

Versions (please complete the following information):

  • OS: N/A (Ubuntu, some current version)
  • PHP: 8.1
  • PHPCS: 3.7.1
  • Standard: N/A

Additional context
The Sniff here https://github.com/spryker/code-sniffer/blob/master/Spryker/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniff.php is using get getMethodParameters method to validate docblock and typehint. Due to wrong input from PHPCS it create a violation where none should be.

@jrfnl
Copy link
Contributor

jrfnl commented Mar 27, 2023

This is intentional and was discussed in detail when support for union types was implemented. Please read the discussion here: #2968 (comment)

@gsherwood
Copy link
Member

Hopefully that linked comment from @jrfnl helped explain the behaviour.

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

No branches or pull requests

3 participants