Skip to content

Commit 3bea46d

Browse files
committed
Fix WrongNullable false positive
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 #368.
1 parent 6984cbf commit 3bea46d

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Spryker/Traits/SignatureTrait.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ protected function getMethodSignature(File $phpCsFile, int $stackPtr): array
5757
$typehint = substr($typehint, 1);
5858
}
5959

60+
// Determine if parameter accepts null (nullable or explicit null)
61+
$nullable = $parameter['nullable_type'];
62+
if ($nullable === false && $typehint != '') {
63+
for ($i = $parameter['type_hint_token']; $i <= $parameter['type_hint_end_token']; $i++) {
64+
if ($tokens[$i]['code'] === T_NULL) {
65+
$nullable = true;
66+
break;
67+
}
68+
}
69+
}
70+
6071
$arguments[] = [
6172
'variableIndex' => $parameter['token'],
6273
'variable' => $parameter['name'],
6374
'typehint' => $typehint,
6475
'typehintFull' => $parameter['type_hint'],
65-
'nullable' => $parameter['nullable_type'],
76+
'nullable' => $nullable,
6677
'defaultIndex' => $defaultIndex,
6778
'default' => $default,
6879
];

tests/_data/DocBlockParamAllowDefaultValue/after.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
6767
public function toPHP(mixed $value): mixed {
6868
return $value;
6969
}
70+
71+
/**
72+
* This typehint is correct and should not change. See issue #368.
73+
*
74+
* @param int|string|null $value
75+
* @return void
76+
*/
77+
public function multipleUnion(int|string|null $value): void
78+
{
79+
}
7080
}

tests/_data/DocBlockParamAllowDefaultValue/before.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
6767
public function toPHP(mixed $value): mixed {
6868
return $value;
6969
}
70+
71+
/**
72+
* This typehint is correct and should not change. See issue #368.
73+
*
74+
* @param int|string|null $value
75+
* @return void
76+
*/
77+
public function multipleUnion(int|string|null $value): void
78+
{
79+
}
7080
}

0 commit comments

Comments
 (0)