Skip to content

WrongNullable false positive #368

@hemberger

Description

@hemberger
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@9a502fd.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions