Skip to content

Commit

Permalink
Fix WrongNullable false positive
Browse files Browse the repository at this point in the history
The `nullable` field returned from SignatureTrait::getMethodSignature
is used by DocBlockParamAllowDefaultValueSniff assuming that it means
whether or not the parameter accepts null, so we need to also check if
an explicit null is part of the typehint. This is because in the arrays
returned by File::getMethodParameters, the `nullable_type` field only
indicates if the typehint is prefixed with "?".

Fixes spryker#368.
  • Loading branch information
hemberger committed Mar 3, 2023
1 parent b6d4be0 commit 76bbb3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Spryker/Traits/SignatureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ protected function getMethodSignature(File $phpCsFile, int $stackPtr): array
$typehint = substr($typehint, 1);
}

// Determine if parameter accepts null (nullable or explicit null)
$nullable = $parameter['nullable_type'];
if ($nullable === false && $typehint != '') {
for ($i = $parameter['type_hint_token']; $i <= $parameter['type_hint_end_token']; $i++) {
if ($tokens[$i]['code'] === T_NULL) {
$nullable = true;

break;
}
}
}

$arguments[] = [
'variableIndex' => $parameter['token'],
'variable' => $parameter['name'],
'typehint' => $typehint,
'typehintFull' => $parameter['type_hint'],
'nullable' => $parameter['nullable_type'],
'nullable' => $nullable,
'defaultIndex' => $defaultIndex,
'default' => $default,
];
Expand Down
10 changes: 10 additions & 0 deletions tests/_data/DocBlockParamAllowDefaultValue/after.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
public function toPHP(mixed $value): mixed {
return $value;
}

/**
* This typehint is correct and should not change. See issue #368.
*
* @param int|string|null $value
* @return void
*/
public function multipleUnion(int|string|null $value): void
{
}
}
10 changes: 10 additions & 0 deletions tests/_data/DocBlockParamAllowDefaultValue/before.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
public function toPHP(mixed $value): mixed {
return $value;
}

/**
* This typehint is correct and should not change. See issue #368.
*
* @param int|string|null $value
* @return void
*/
public function multipleUnion(int|string|null $value): void
{
}
}

0 comments on commit 76bbb3c

Please sign in to comment.