Skip to content

Commit

Permalink
Fixed missing return rule for native mixed type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 27, 2021
1 parent fc7bcff commit 03d8312
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Rules/Missing/MissingReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function processNode(Node $node, Scope $scope): array
if (
$returnType instanceof MixedType
&& !$returnType instanceof TemplateMixedType
&& !$node->hasNativeReturnTypehint()
&& (
!$returnType->isExplicitMixed()
|| !$this->checkExplicitMixedMissingReturn
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ public function dataFileAsserts(): iterable

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

if (PHP_VERSION_ID >= 80000 || self::$useStaticReflectionProvider) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/model-mixin.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5529.php');
}

Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/data/model-mixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php // lint >= 8.0

namespace ModelMixin;

use function PHPStan\Testing\assertType;

/** @mixin Builder<static> */
class Model
{
/** @param array<int, mixed> $args */
public static function __callStatic(string $method, array $args): mixed
{
(new self)->$method(...$args);
}
}

/** @template TModel as Model */
class Builder
{
/** @return array<int, TModel> */
public function all() { return []; }
}

class User extends Model
{
}

function (): void {
assertType('array<int, ModelMixin\User>', User::all());
};
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,36 @@ public function testCheckPhpDocMissingReturn(bool $checkPhpDocMissingReturn, arr
$this->analyse([__DIR__ . '/data/check-phpdoc-missing-return.php'], $errors);
}

public function dataModelMixin(): array
{
return [
[
true,
],
[
false,
],
];
}

/**
* @dataProvider dataModelMixin
* @param bool $checkExplicitMixedMissingReturn
*/
public function testModelMixin(bool $checkExplicitMixedMissingReturn): void
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkExplicitMixedMissingReturn = $checkExplicitMixedMissingReturn;
$this->checkPhpDocMissingReturn = true;
$this->analyse([__DIR__ . '/../../Analyser/data/model-mixin.php'], [
[
'Method ModelMixin\Model::__callStatic() should return mixed but return statement is missing.',
13,
],
]);
}

}

0 comments on commit 03d8312

Please sign in to comment.