Skip to content

Commit b26557e

Browse files
fix: Functions\ReturnTypeSniff for mixed declaration when returning null (#186)
Closes #185
1 parent eb64f30 commit b26557e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/WebimpressCodingStandard/Sniffs/Functions/ReturnTypeSniff.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -974,10 +974,10 @@ private function getReturnValue(File $phpcsFile, int $ptr) : string
974974
return 'new';
975975

976976
case T_NULL:
977-
if (! $this->hasCorrectType([], ['null'])
978-
|| ($this->returnType
979-
&& $this->returnTypeIsValid
980-
&& strpos($this->returnTypeValue, '?') !== 0)
977+
if (! $this->hasCorrectType(['null', 'mixed'], ['null', 'mixed'])
978+
&& (! $this->returnType
979+
|| ($this->returnTypeIsValid
980+
&& strpos($this->returnTypeValue, '?') !== 0))
981981
) {
982982
$error = 'Function return type is not nullable, but function returns null here';
983983
$phpcsFile->addError($error, $ptr, 'ReturnNull');

test/Sniffs/Functions/ReturnTypeUnitTest.inc

+13
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,17 @@ abstract class FunctionCommentReturn
406406

407407
return new mysqli_result;
408408
}
409+
410+
public function getStr(string $key): mixed
411+
{
412+
if (is_string($key)) {
413+
return $key;
414+
}
415+
416+
if (is_array($key)) {
417+
return json_encode($key);
418+
}
419+
420+
return null;
421+
}
409422
}

0 commit comments

Comments
 (0)