Skip to content

Commit 4be202f

Browse files
phpstan-botVincentLangletclaude
authored
Resolve static in parameter and writable property types to the called-on type without the new Foo() exactness (#6946)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2fea75a commit 4be202f

10 files changed

Lines changed: 280 additions & 10 deletions

File tree

‎src/Type/Generic/TemplateTypeVariance.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,13 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b, bo
233233
return new IsSuperTypeOfResult(TrinaryLogic::createFromBoolean($result), $reasons);
234234
}
235235

236-
if ($this->covariant()) {
237-
return $a->isSuperTypeOf($b);
238-
}
236+
if ($this->covariant() || $this->contravariant()) {
237+
// The "exactly this class" flavour of a `new Foo()` value cannot be written
238+
// in a PHPDoc type argument - ignore it like equals() does for invariance.
239+
$a = TemplateTypeHelper::removeFinalByKeywordOverrides($a);
240+
$b = TemplateTypeHelper::removeFinalByKeywordOverrides($b);
239241

240-
if ($this->contravariant()) {
241-
return $b->isSuperTypeOf($a);
242+
return $this->covariant() ? $a->isSuperTypeOf($b) : $b->isSuperTypeOf($a);
242243
}
243244

244245
if ($this->bivariant()) {

‎tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ public function testBug15442(): void
468468
$this->analyse([__DIR__ . '/data/bug-15442.php'], []);
469469
}
470470

471+
#[RequiresPhp('>= 8.1.0')]
472+
public function testBug15235(): void
473+
{
474+
$this->analyse([__DIR__ . '/../Methods/data/bug-15235.php'], []);
475+
}
476+
471477
#[RequiresPhp('>= 8.0.0')]
472478
public function testBug11935WithCheckExplicitMixed(): void
473479
{

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4537,6 +4537,15 @@ public function testBug15166(): void
45374537
$this->analyse([__DIR__ . '/data/bug-15166.php'], []);
45384538
}
45394539

4540+
#[RequiresPhp('>= 8.1.0')]
4541+
public function testBug15235(): void
4542+
{
4543+
$this->checkThisOnly = false;
4544+
$this->checkNullables = true;
4545+
$this->checkUnionTypes = true;
4546+
$this->analyse([__DIR__ . '/data/bug-15235.php'], []);
4547+
}
4548+
45404549
public function testBug6732(): void
45414550
{
45424551
$this->checkThisOnly = false;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,4 +1158,11 @@ public function testBug11421(): void
11581158
]);
11591159
}
11601160

1161+
#[RequiresPhp('>= 8.1.0')]
1162+
public function testBug15235(): void
1163+
{
1164+
$this->checkThisOnly = false;
1165+
$this->analyse([__DIR__ . '/data/bug-15235.php'], []);
1166+
}
1167+
11611168
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15235;
6+
7+
/**
8+
* @template T of object
9+
*/
10+
class A
11+
{
12+
/**
13+
* @param T $object
14+
*/
15+
public function __construct(public object $object) {}
16+
}
17+
18+
class B
19+
{
20+
/**
21+
* @param A<covariant static> $a
22+
*/
23+
public function method(A $a): void {}
24+
25+
/**
26+
* @param A<covariant static> $a
27+
*/
28+
public static function staticMethod(A $a): void {}
29+
30+
/**
31+
* @param A<static> $a
32+
*/
33+
public function invariant(A $a): void {}
34+
35+
/**
36+
* @param A<contravariant static> $a
37+
*/
38+
public function contravariant(A $a): void {}
39+
}
40+
41+
class D
42+
{
43+
public function inconsistent(B $b): void
44+
{
45+
$b->method(new A($b));
46+
}
47+
}
48+
49+
function fromNew(): void
50+
{
51+
$b = new B();
52+
$b->method(new A($b));
53+
$b->invariant(new A($b));
54+
$b->contravariant(new A($b));
55+
$b::staticMethod(new A($b));
56+
57+
$callable = $b->method(...);
58+
$callable(new A($b));
59+
}
60+
61+
function fromGetClass(B $b): void
62+
{
63+
if (get_class($b) !== B::class) {
64+
return;
65+
}
66+
67+
$b->method(new A($b));
68+
}
69+
70+
/** @template-covariant T of object */
71+
class Covariant
72+
{
73+
/** @param T $object */
74+
public function __construct(public object $object) {}
75+
}
76+
77+
/** @template-contravariant T of object */
78+
class Contravariant
79+
{
80+
/** @param T $object */
81+
public function set(object $object): void {}
82+
}
83+
84+
class E
85+
{
86+
/** @param Covariant<static> $c */
87+
public function declaredCovariant(Covariant $c): void {}
88+
89+
/** @param Contravariant<static> $c */
90+
public function declaredContravariant(Contravariant $c): void {}
91+
92+
/** @param \Traversable<int, static> $it */
93+
public function traversable(\Traversable $it): void {}
94+
95+
/** @return Covariant<static> */
96+
public function makeCovariant(): Covariant
97+
{
98+
return new Covariant($this);
99+
}
100+
101+
/** @return Wrapper<static> */
102+
public function makeWrapper(): Wrapper
103+
{
104+
return new Wrapper($this);
105+
}
106+
}
107+
108+
class Consumer
109+
{
110+
/** @param Covariant<E> $c */
111+
public function takesCovariant(Covariant $c): void {}
112+
113+
/** @param Wrapper<contravariant E> $wrapper */
114+
public function takesContravariantWrapper(Wrapper $wrapper): void {}
115+
116+
/** @param Wrapper<Covariant<E>> $wrapper */
117+
public function takesNested(Wrapper $wrapper): void {}
118+
}
119+
120+
/**
121+
* @param Covariant<E> $covariant
122+
* @param Contravariant<E> $contravariant
123+
* @param \Traversable<int, E> $traversable
124+
*/
125+
function declaredVariances(Covariant $covariant, Contravariant $contravariant, \Traversable $traversable): void
126+
{
127+
$e = new E();
128+
$e->declaredCovariant($covariant);
129+
$e->declaredContravariant($contravariant);
130+
$e->traversable($traversable);
131+
}
132+
133+
function flavourOnTheArgumentSide(Consumer $consumer): void
134+
{
135+
$e = new E();
136+
$consumer->takesCovariant($e->makeCovariant());
137+
$consumer->takesContravariantWrapper($e->makeWrapper());
138+
}
139+
140+
/** @template T of object */
141+
class Wrapper
142+
{
143+
/** @param T $object */
144+
public function __construct(public object $object) {}
145+
}
146+
147+
function nestedInGenericType(Consumer $consumer): void
148+
{
149+
$e = new E();
150+
$consumer->takesNested(new Wrapper($e->makeCovariant()));
151+
}
152+
153+
class Composite
154+
{
155+
/** @param Covariant<static>|null $c */
156+
public function inUnion(?Covariant $c): void {}
157+
158+
/** @param array<int, Covariant<static>> $c */
159+
public function inArray(array $c): void {}
160+
161+
/** @param Covariant<Covariant<static>> $c */
162+
public function nested(Covariant $c): void {}
163+
164+
/** @param Wrapper<Covariant<static>> $c */
165+
public function nestedInvariant(Wrapper $c): void {}
166+
}
167+
168+
/**
169+
* @param Covariant<Composite> $covariant
170+
* @param array<int, Covariant<Composite>> $array
171+
* @param Covariant<Covariant<Composite>> $nested
172+
* @param Wrapper<Covariant<Composite>> $nestedInvariant
173+
*/
174+
function compositeShapes(Covariant $covariant, array $array, Covariant $nested, Wrapper $nestedInvariant): void
175+
{
176+
$composite = new Composite();
177+
$composite->inUnion($covariant);
178+
$composite->inUnion(null);
179+
$composite->inArray($array);
180+
$composite->nested($nested);
181+
$composite->nestedInvariant($nestedInvariant);
182+
}

‎tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,12 @@ public function testBug15166(): void
11011101
$this->analyse([__DIR__ . '/data/bug-15166.php'], []);
11021102
}
11031103

1104+
#[RequiresPhp('>= 8.1.0')]
1105+
public function testBug15235(): void
1106+
{
1107+
$this->analyse([__DIR__ . '/data/bug-15235.php'], []);
1108+
}
1109+
11041110
public function testBug6732(): void
11051111
{
11061112
$this->analyse([__DIR__ . '/data/bug-6732.php'], [
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15235Properties;
6+
7+
/**
8+
* @template T of object
9+
*/
10+
class A
11+
{
12+
/**
13+
* @param T $object
14+
*/
15+
public function __construct(public object $object) {}
16+
}
17+
18+
/** @template-covariant T of object */
19+
class Covariant
20+
{
21+
/** @param T $object */
22+
public function __construct(public object $object) {}
23+
}
24+
25+
class B
26+
{
27+
/** @var A<covariant static>|null */
28+
public ?A $callSiteVariance = null;
29+
30+
/** @var Covariant<static>|null */
31+
public ?Covariant $declaredVariance = null;
32+
}
33+
34+
function fromParameter(B $b): void
35+
{
36+
$b->callSiteVariance = new A($b);
37+
$b->declaredVariance = new Covariant($b);
38+
}
39+
40+
function fromNew(): void
41+
{
42+
$b = new B();
43+
$b->callSiteVariance = new A($b);
44+
$b->declaredVariance = new Covariant($b);
45+
}

‎turbo-ext/src/TemplateTypeHelper.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ zv::Val pt_type_template_type_helper_to_argument(zval *type)
558558
return TemplateTypeHelper::toArgument(type);
559559
}
560560

561+
zv::Val pt_type_template_type_helper_remove_final_by_keyword_overrides(zval *type)
562+
{
563+
return TemplateTypeHelper::removeFinalByKeywordOverrides(type);
564+
}
565+
561566
/* }}} */
562567

563568
/* {{{ engine ABI glue: parameter parsing + registration */

‎turbo-ext/src/TemplateTypeVariance.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,17 @@ zv::Val TemplateTypeVariance::isValidVariance(zval *templateType, zval *a, zval
436436
return pt_type_new_ce(pt_ce_is_super_type_of_result, 2, args);
437437
}
438438

439-
if (thisValue == COVARIANT) return pt_type_op(Z_OBJ_P(a), PT_OP_IS_SUPER_TYPE_OF, 1, b);
440-
441-
if (thisValue == CONTRAVARIANT) return pt_type_op(Z_OBJ_P(b), PT_OP_IS_SUPER_TYPE_OF, 1, a);
439+
if (thisValue == COVARIANT || thisValue == CONTRAVARIANT) {
440+
/* the "exactly this class" flavour of a `new Foo()` value cannot be
441+
* written in a PHPDoc type argument - ignore it like equals() does
442+
* for invariance */
443+
zv::Val strippedA = pt_type_template_type_helper_remove_final_by_keyword_overrides(a);
444+
if (UNEXPECTED(strippedA.isUndef())) return zv::Val();
445+
zv::Val strippedB = pt_type_template_type_helper_remove_final_by_keyword_overrides(b);
446+
if (UNEXPECTED(strippedB.isUndef())) return zv::Val();
447+
if (thisValue == COVARIANT) return pt_type_op(Z_OBJ_P(strippedA.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, strippedB.raw());
448+
return pt_type_op(Z_OBJ_P(strippedB.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, strippedA.raw());
449+
}
442450

443451
if (thisValue == BIVARIANT) return pt_type_is_super_type_of_result(PT_TRI_YES);
444452

‎turbo-ext/src/TypeTraits.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,13 @@ zv::Val pt_type_template_type_variance_map_new(zval *variances);
946946
zv::Val pt_type_template_type_map_empty();
947947
zv::Val pt_type_template_type_map_new(zval *types, zval *lowerBoundTypes = NULL);
948948
/* TemplateTypeHelper::resolveTemplateTypes() / resolveToDefaults() /
949-
* resolveToBounds() / toArgument() (the arguments borrowed); UNDEF =
950-
* pending exception */
949+
* resolveToBounds() / toArgument() / removeFinalByKeywordOverrides() (the
950+
* arguments borrowed); UNDEF = pending exception */
951951
zv::Val pt_type_template_type_helper_resolve_template_types(zval *type, zval *standins, zval *callSiteVariances, zval *positionVariance, bool keepErrorTypes);
952952
zv::Val pt_type_template_type_helper_resolve_to_defaults(zval *type);
953953
zv::Val pt_type_template_type_helper_resolve_to_bounds(zval *type);
954954
zv::Val pt_type_template_type_helper_to_argument(zval *type);
955+
zv::Val pt_type_template_type_helper_remove_final_by_keyword_overrides(zval *type);
955956
/* $reference->getType() / ->getPositionVariance() of a reference instance
956957
* (TemplateTypeReference.cpp): the slots of a native instance, the methods
957958
* of anything else (the PHP twin declared next to the native class in the

0 commit comments

Comments
 (0)