Skip to content

Commit 03d8312

Browse files
committed
Fixed missing return rule for native mixed type
1 parent fc7bcff commit 03d8312

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/Rules/Missing/MissingReturnRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function processNode(Node $node, Scope $scope): array
114114
if (
115115
$returnType instanceof MixedType
116116
&& !$returnType instanceof TemplateMixedType
117+
&& !$node->hasNativeReturnTypehint()
117118
&& (
118119
!$returnType->isExplicitMixed()
119120
|| !$this->checkExplicitMixedMissingReturn

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ public function dataFileAsserts(): iterable
482482

483483
yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-var-returns-non-empty-string.php');
484484

485+
if (PHP_VERSION_ID >= 80000 || self::$useStaticReflectionProvider) {
486+
yield from $this->gatherAssertTypes(__DIR__ . '/data/model-mixin.php');
487+
}
488+
485489
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5529.php');
486490
}
487491

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php // lint >= 8.0
2+
3+
namespace ModelMixin;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @mixin Builder<static> */
8+
class Model
9+
{
10+
/** @param array<int, mixed> $args */
11+
public static function __callStatic(string $method, array $args): mixed
12+
{
13+
(new self)->$method(...$args);
14+
}
15+
}
16+
17+
/** @template TModel as Model */
18+
class Builder
19+
{
20+
/** @return array<int, TModel> */
21+
public function all() { return []; }
22+
}
23+
24+
class User extends Model
25+
{
26+
}
27+
28+
function (): void {
29+
assertType('array<int, ModelMixin\User>', User::all());
30+
};

tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,36 @@ public function testCheckPhpDocMissingReturn(bool $checkPhpDocMissingReturn, arr
250250
$this->analyse([__DIR__ . '/data/check-phpdoc-missing-return.php'], $errors);
251251
}
252252

253+
public function dataModelMixin(): array
254+
{
255+
return [
256+
[
257+
true,
258+
],
259+
[
260+
false,
261+
],
262+
];
263+
}
264+
265+
/**
266+
* @dataProvider dataModelMixin
267+
* @param bool $checkExplicitMixedMissingReturn
268+
*/
269+
public function testModelMixin(bool $checkExplicitMixedMissingReturn): void
270+
{
271+
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
272+
$this->markTestSkipped('Test requires PHP 8.0.');
273+
}
274+
275+
$this->checkExplicitMixedMissingReturn = $checkExplicitMixedMissingReturn;
276+
$this->checkPhpDocMissingReturn = true;
277+
$this->analyse([__DIR__ . '/../../Analyser/data/model-mixin.php'], [
278+
[
279+
'Method ModelMixin\Model::__callStatic() should return mixed but return statement is missing.',
280+
13,
281+
],
282+
]);
283+
}
284+
253285
}

0 commit comments

Comments
 (0)