Skip to content

Commit 85ab8cf

Browse files
committed
Fix TemplateTypeArgumentStrategy::accepts()
1 parent 5b2d7b6 commit 85ab8cf

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

src/Type/Generic/TemplateTypeArgumentStrategy.php

-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPStan\TrinaryLogic;
66
use PHPStan\Type\CompoundType;
7-
use PHPStan\Type\IntersectionType;
87
use PHPStan\Type\Type;
98

109
/**
@@ -15,16 +14,6 @@ class TemplateTypeArgumentStrategy implements TemplateTypeStrategy
1514

1615
public function accepts(TemplateType $left, Type $right, bool $strictTypes): TrinaryLogic
1716
{
18-
if ($right instanceof IntersectionType) {
19-
foreach ($right->getTypes() as $type) {
20-
if ($this->accepts($left, $type, $strictTypes)->yes()) {
21-
return TrinaryLogic::createYes();
22-
}
23-
}
24-
25-
return TrinaryLogic::createNo();
26-
}
27-
2817
if ($right instanceof CompoundType) {
2918
$accepts = $right->isAcceptedBy($left, $strictTypes);
3019
} else {

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,10 @@ public function testBug7265(): void
700700
$this->analyse([__DIR__ . '/data/bug-7265.php'], []);
701701
}
702702

703+
public function testBug7460(): void
704+
{
705+
$this->checkExplicitMixed = true;
706+
$this->analyse([__DIR__ . '/data/bug-7460.php'], []);
707+
}
708+
703709
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Bug7460;
4+
5+
interface PositionEntityInterface {
6+
public function getPosition(): int;
7+
}
8+
interface TgEntityInterface {}
9+
10+
abstract class HelloWorld
11+
{
12+
/**
13+
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
14+
*
15+
* @param iterable<T> $tgs
16+
*
17+
* @return array<T>
18+
*
19+
* @throws \Exception
20+
*/
21+
public function computeForFrontByPosition($tgs)
22+
{
23+
/** @phpstan-var array<T> $res */
24+
$res = [];
25+
26+
foreach ($tgs as $tgItem) {
27+
$position = $tgItem->getPosition();
28+
29+
if (!isset($res[$position])) {
30+
$res[$position] = $tgItem;
31+
} else {
32+
/** @phpstan-var T $tgItemToKeep */
33+
$tgItemToKeep = $this->compare($tgItem, $res[$position]);
34+
$res[$position] = $tgItemToKeep;
35+
}
36+
}
37+
ksort($res);
38+
39+
return $res;
40+
}
41+
42+
/**
43+
* @phpstan-template T of PositionEntityInterface&TgEntityInterface
44+
*
45+
* @param iterable<T> $tgs
46+
*
47+
* @return array<T>
48+
*
49+
* @throws \Exception
50+
*/
51+
public function computeForFrontByPosition2($tgs)
52+
{
53+
/** @phpstan-var array<T> $res */
54+
$res = [];
55+
56+
foreach ($tgs as $tgItem) {
57+
$position = $tgItem->getPosition();
58+
59+
if (!isset($res[$position])) {
60+
$res[$position] = $tgItem;
61+
} else {
62+
/** @phpstan-var T $tgItemToKeep */
63+
$tgItemToKeep = $this->compare($tgItem, $res[$position]);
64+
$res[$position] = $tgItemToKeep;
65+
}
66+
}
67+
ksort($res);
68+
69+
return $res;
70+
}
71+
72+
/**
73+
* @phpstan-template S of TgEntityInterface
74+
* @phpstan-param S $nextTg
75+
* @phpstan-param S $currentTg
76+
* @phpstan-return S
77+
*/
78+
abstract protected function compare(TgEntityInterface $nextTg, TgEntityInterface $currentTg): TgEntityInterface;
79+
}

0 commit comments

Comments
 (0)