Skip to content

Commit

Permalink
fix: Union return type (PHP 8.2) - Functions\ReturnTypeSniff (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xakki authored Mar 6, 2023
1 parent b892cc6 commit eb64f30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,20 @@ private function getReturnValue(File $phpcsFile, int $ptr) : string
*/
private function hasCorrectType(array $expectedType, array $expectedDoc) : bool
{
$returnTypeValues = $expectedType && $this->returnType && $this->returnTypeIsValid
? explode('|', strtolower(strtr($this->returnTypeValue, [
'(' => '',
')' => '',
'&' => '|',
])))
: [];

if ($expectedType
&& $this->returnType
&& $this->returnTypeIsValid
&& ! in_array(strtolower($this->returnTypeValue), $expectedType, true)
&& ! array_filter($returnTypeValues, static function (string $v) use ($expectedType) {
return in_array($v, $expectedType, true);
})
) {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,13 @@ abstract class FunctionCommentReturn

return $a;
}

public function insert(array $data): bool|mysqli_result
{
if (empty($data)) {
return false;
}

return new mysqli_result;
}
}

0 comments on commit eb64f30

Please sign in to comment.