spryker / code-sniffer

Spryker Code Sniffer
https://spryker.com
MIT License
36 stars 11 forks source link

WrongNullable false positive #368

Open hemberger opened 1 year ago

hemberger commented 1 year ago
LINE 229: ERROR [x] Doc block error: `object|array<mixed>|string|null $object` seems to be having a wrong `null` type hinted, argument is
                    not nullable though. (Spryker.Commenting.DocBlockParamAllowDefaultValue.WrongNullable)
----------------------------------------------------------------------------------------------------------------------------------------
   227: 
   228:     /**
>> 229:      * @param object|array<mixed>|string|null $object
   230:      */
   231:     public function escapeNullableObject(object|array|string|null $object): string {

This false positive occurs for any parameter that uses the "union with null" syntax in the interface typehint and is also included in the doc block. Using the "nullable type" syntax (i.e. ?type) does not trigger this false positive, but it is not possible to use this syntax in cases where the typehint is a multiple union (i.e. ?(type1|type2) or similar is illegal).

The cause of this false positive is the confusingly named nullable_type field in the parameter arrays returned by PHP_CodeSniffer\Files\File::getMethodParameters. It does not identify if the parameter accepts null as one might expect, but rather, it only identifies if the "nullable type" syntax is used. This is clarified in the PHP_CodeSniffer documentation in squizlabs/PHP_CodeSniffer@9a502fdb576b04f7923adbbe0061f2b64648296a.

dereuromark commented 1 year ago

Nice find!