Skip to content

Commit b4f81db

Browse files
committed
checkExplicitMixed - replace mixed type recursively
1 parent 92ea742 commit b4f81db

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/Rules/RuleLevelHelper.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Type\StrictMixedType;
1919
use PHPStan\Type\Type;
2020
use PHPStan\Type\TypeCombinator;
21+
use PHPStan\Type\TypeTraverser;
2122
use PHPStan\Type\TypeUtils;
2223
use PHPStan\Type\UnionType;
2324

@@ -60,10 +61,17 @@ public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTyp
6061
{
6162
if (
6263
$this->checkExplicitMixed
63-
&& $acceptedType instanceof MixedType
64-
&& $acceptedType->isExplicitMixed()
6564
) {
66-
$acceptedType = new StrictMixedType();
65+
$acceptedType = TypeTraverser::map($acceptedType, static function (Type $type, callable $traverse): Type {
66+
if (
67+
$type instanceof MixedType
68+
&& $type->isExplicitMixed()
69+
) {
70+
return new StrictMixedType();
71+
}
72+
73+
return $traverse($type);
74+
});
6775
}
6876

6977
if (

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
class ReturnTypeRuleTest extends \PHPStan\Testing\RuleTestCase
1212
{
1313

14+
/** @var bool */
15+
private $checkExplicitMixed = false;
16+
1417
protected function getRule(): \PHPStan\Rules\Rule
1518
{
16-
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false)));
19+
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed)));
1720
}
1821

1922
public function testReturnTypeRule(): void
@@ -527,4 +530,15 @@ public function testTemplateUnion(): void
527530
]);
528531
}
529532

533+
public function testBug5218(): void
534+
{
535+
$this->checkExplicitMixed = true;
536+
$this->analyse([__DIR__ . '/data/bug-5218.php'], [
537+
[
538+
'Method Bug5218\IA::getIterator() should return Traversable<string, int> but returns ArrayIterator<string, mixed>.',
539+
14,
540+
],
541+
]);
542+
}
543+
530544
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug5218;
4+
5+
/**
6+
* @phpstan-implements \IteratorAggregate<string, int>
7+
*/
8+
final class IA implements \IteratorAggregate
9+
{
10+
/** @var array<string, mixed> */
11+
private $data = [];
12+
13+
public function getIterator() : \Traversable {
14+
return new \ArrayIterator($this->data);
15+
}
16+
}

0 commit comments

Comments
 (0)