Skip to content

Commit d057cd1

Browse files
authored
Distrust a TemplateType member's eager Yes in IntersectionType::isAcceptedBy only when that member accepts with Yes (#5962)
1 parent 0c88c32 commit d057cd1

5 files changed

Lines changed: 176 additions & 2 deletions

File tree

‎src/Type/IntersectionType.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsRes
342342

343343
// A TemplateType member accepts eagerly, so lazyMaxMin's Yes may come solely from it.
344344
// A Maybe from the holistic isSuperTypeOf means no member is a definite subtype, so
345-
// when a TemplateType member is present the eager Yes is untrustworthy - trust the Maybe.
345+
// when a TemplateType member forced the eager Yes, that Yes is untrustworthy - trust the Maybe.
346+
// Only distrust when a TemplateType member itself accepts with Yes; if it accepts with Maybe,
347+
// the eager Yes came from a non-template member and reflects a genuine (object-level) match.
346348
if ($isSuperType->maybe()) {
347349
foreach ($this->types as $innerType) {
348-
if ($innerType instanceof TemplateType) {
350+
if ($innerType instanceof TemplateType && $acceptingType->accepts($innerType, $strictTypes)->yes()) {
349351
return $isSuperType->toAcceptsResult();
350352
}
351353
}

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ private static function findTestFiles(): iterable
275275
yield __DIR__ . '/data/dio-functions.php';
276276
}
277277

278+
yield __DIR__ . '/../Rules/Methods/data/bug-14893.php';
278279
yield __DIR__ . '/../Rules/Variables/data/bug-13921.php';
279280
yield __DIR__ . '/../Rules/Arrays/data/bug-14234.php';
280281
yield __DIR__ . '/../Rules/Arrays/data/bug-11679.php';

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,4 +4235,29 @@ public function testBug14808(): void
42354235
$this->analyse([__DIR__ . '/data/bug-14808.php'], []);
42364236
}
42374237

4238+
public function testBug14893(): void
4239+
{
4240+
$this->checkThisOnly = false;
4241+
$this->checkNullables = true;
4242+
$this->checkUnionTypes = true;
4243+
$this->analyse([__DIR__ . '/data/bug-14893.php'], [
4244+
[
4245+
'Parameter #2 $value of method Bug14893\HelloWorld<DateTimeInterface>::offsetSet() expects Closure(Bug14893\HelloWorld<DateTimeInterface>): DateTimeInterface, static-Closure(mixed): stdClass given.',
4246+
55,
4247+
],
4248+
[
4249+
'Parameter #1 $value of method Bug14893\ArrayCase<T of object>::acceptsT() expects T of object, array<T of object>|T of object given.',
4250+
90,
4251+
],
4252+
[
4253+
'Parameter #1 $value of method Bug14893\ArrayCase<T of object>::acceptsArrayT() expects array<T of object>, array<T of object>|T of object given.',
4254+
91,
4255+
],
4256+
[
4257+
'Parameter #1 $value of method Bug14893\ClassStringCase<T of object>::acceptsClassString() expects class-string<T of object>, string given.',
4258+
137,
4259+
],
4260+
]);
4261+
}
4262+
42384263
}

‎tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,4 +1359,9 @@ public function testBug11776(): void
13591359
$this->analyse([__DIR__ . '/data/bug-11776.php'], []);
13601360
}
13611361

1362+
public function testBug14893(): void
1363+
{
1364+
$this->analyse([__DIR__ . '/data/bug-14893.php'], []);
1365+
}
1366+
13621367
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Bug14893;
4+
5+
use function is_array;
6+
use function is_object_in_taxonomy;
7+
use function PHPStan\Testing\assertType;
8+
9+
/**
10+
* @template T of object
11+
*/
12+
class HelloWorld
13+
{
14+
/**
15+
* @param int $offset
16+
* @param \Closure(static): T $value
17+
*/
18+
public function offsetSet($offset, \Closure $value): void
19+
{
20+
}
21+
22+
/**
23+
* @param (\Closure(static): T)|(\Closure&T) $value
24+
*/
25+
public function foo($value): void
26+
{
27+
$this->offsetSet(0, $value);
28+
}
29+
30+
/**
31+
* @param (\Closure(static): T)|(\Closure&T) $value
32+
* @return \Closure(static): T
33+
*/
34+
public function bar($value): \Closure
35+
{
36+
return $value;
37+
}
38+
}
39+
40+
/**
41+
* @param HelloWorld<\DateTimeInterface> $h
42+
*/
43+
function callWithMatchingClosure(HelloWorld $h): void
44+
{
45+
// Closure returning the concrete T is a genuine subtype - accepted.
46+
$h->offsetSet(0, static fn ($self): \DateTimeInterface => new \DateTime());
47+
}
48+
49+
/**
50+
* @param HelloWorld<\DateTimeInterface> $h
51+
*/
52+
function callWithMismatchedClosure(HelloWorld $h): void
53+
{
54+
// Closure returning an unrelated type is not a subtype - rejected.
55+
$h->offsetSet(0, static fn ($self): \stdClass => new \stdClass());
56+
}
57+
58+
/**
59+
* @template T of object
60+
*/
61+
class ArrayCase
62+
{
63+
/**
64+
* @param T $value
65+
*/
66+
public function acceptsT($value): void
67+
{
68+
}
69+
70+
/**
71+
* @param array<T> $value
72+
*/
73+
public function acceptsArrayT($value): void
74+
{
75+
}
76+
77+
/**
78+
* @param T|array<T> $value
79+
*/
80+
public function acceptsTUnionArrayT($value): void
81+
{
82+
}
83+
84+
/**
85+
* @param T|array<T> $value
86+
*/
87+
public function passUnion($value): void
88+
{
89+
// array<T> is genuinely not T - must still be rejected, the fix does not over-accept.
90+
$this->acceptsT($value);
91+
$this->acceptsArrayT($value);
92+
93+
$this->acceptsTUnionArrayT($value); // fine
94+
95+
if (is_array($value)) {
96+
// fine after narrow
97+
$this->acceptsArrayT($value);
98+
99+
assertType('array<T of object (class Bug14893\ArrayCase, argument)>', $value);
100+
} else {
101+
assertType('T of object (class Bug14893\ArrayCase, argument)', $value);
102+
}
103+
104+
if (is_object($value)) {
105+
assertType('T of object (class Bug14893\ArrayCase, argument)', $value);
106+
} else {
107+
assertType('array<T of object (class Bug14893\ArrayCase, argument)>', $value);
108+
}
109+
}
110+
}
111+
112+
/**
113+
* @template T of object
114+
*/
115+
class ClassStringCase
116+
{
117+
/**
118+
* @param class-string<T> $value
119+
*/
120+
public function acceptsClassString(string $value): void
121+
{
122+
}
123+
124+
/**
125+
* @param non-empty-string $value
126+
*/
127+
public function acceptsNonEmptyString(string $value): void
128+
{
129+
}
130+
131+
/**
132+
* @param non-empty-string|class-string<T> $value
133+
*/
134+
public function passUnion(string $value): void
135+
{
136+
// non-empty-string is not a class-string<T> - rejected.
137+
$this->acceptsClassString($value);
138+
// a class-string is always a non-empty-string - accepted.
139+
$this->acceptsNonEmptyString($value);
140+
}
141+
}

0 commit comments

Comments
 (0)