Skip to content

Commit 76e446e

Browse files
committed
Fix false positive for a null return on mixed function
1 parent 23d06fc commit 76e446e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Inpsyde/Sniffs/CodeQuality/ReturnTypeDeclarationSniff.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ private function checkNonEmptyReturnTypes(
185185
array $returnInfo
186186
): void {
187187

188+
if ($returnTypes === ['mixed']) {
189+
$this->checkIsNotVoid($file, $position, $returnInfo);
190+
191+
return;
192+
}
193+
188194
if (($returnTypes === ['void']) || ($returnTypes === ['null'])) {
189195
$this->checkIsActualVoid($file, $position, $returnInfo, $returnTypes === ['null']);
190196

@@ -196,6 +202,30 @@ private function checkNonEmptyReturnTypes(
196202
|| $this->checkIncorrectVoid($file, $position, $returnTypes, $returnInfo);
197203
}
198204

205+
/**
206+
* @param File $file
207+
* @param int $position
208+
* @param array $returnInfo
209+
* @param bool $checkNull
210+
* @return void
211+
*/
212+
private function checkIsNotVoid(
213+
File $file,
214+
int $position,
215+
array $returnInfo
216+
): void {
217+
218+
if ($returnInfo['void'] === 0) {
219+
return;
220+
}
221+
222+
$file->addError(
223+
'Return type is declared non-void but void return statement(s) found',
224+
$position,
225+
'IncorrectVoidReturn'
226+
);
227+
}
228+
199229
/**
200230
* @param File $file
201231
* @param int $position

tests/fixtures/return-type-declaration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
use Psr\Container\ContainerInterface as PsrContainer;
55

6+
function returnMixed(): mixed
7+
{
8+
return null;
9+
}
10+
611
add_filter('x', function () {
712
return '';
813
});
@@ -218,6 +223,12 @@ function genMultiReturn(): \Generator
218223
return 2;
219224
}
220225

226+
// @phpcsErrorCodeOnNextLine IncorrectVoidReturn
227+
function returnMixed2(): mixed
228+
{
229+
return;
230+
}
231+
221232
// @phpcsErrorCodeOnNextLine IncorrectVoidReturnType
222233
add_filter('x', function (): void {
223234
return '0';

0 commit comments

Comments
 (0)