Skip to content

Commit 7126c16

Browse files
VincentLangletgithub-actions[bot]
authored andcommitted
Fix seemingly same type reports as invalid/incompatible. (phpstan#5094)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 457c32f commit 7126c16

File tree

5 files changed

+140
-5
lines changed

5 files changed

+140
-5
lines changed

src/Rules/RuleLevelHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
9595

9696
return new CallableType(
9797
$acceptedType->getParameters(),
98-
$traverse($this->transformCommonType($acceptedType->getReturnType())),
98+
$traverse($acceptedType->getReturnType()),
9999
$acceptedType->isVariadic(),
100100
$acceptedType->getTemplateTypeMap(),
101101
$acceptedType->getResolvedTemplateTypeMap(),
@@ -111,7 +111,7 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
111111

112112
return new ClosureType(
113113
$acceptedType->getParameters(),
114-
$traverse($this->transformCommonType($acceptedType->getReturnType())),
114+
$traverse($acceptedType->getReturnType()),
115115
$acceptedType->isVariadic(),
116116
$acceptedType->getTemplateTypeMap(),
117117
$acceptedType->getResolvedTemplateTypeMap(),
@@ -142,10 +142,10 @@ private function transformAcceptedType(Type $acceptingType, Type $acceptedType):
142142
}
143143
}
144144

145-
return $traverse($this->transformCommonType($acceptedType));
145+
return $traverse($acceptedType);
146146
});
147147

148-
return [$acceptedType, $checkForUnion];
148+
return [$this->transformCommonType($acceptedType), $checkForUnion];
149149
}
150150

151151
/** @api */

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
class InstantiationRuleTest extends RuleTestCase
2121
{
2222

23+
private bool $checkExplicitMixed = false;
24+
2325
protected function getRule(): Rule
2426
{
2527
$reflectionProvider = self::createReflectionProvider();
2628
$container = self::getContainer();
2729
return new InstantiationRule(
2830
$container,
2931
$reflectionProvider,
30-
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, true, true, true, true),
32+
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, $this->checkExplicitMixed, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, true, true, true, true),
3133
new ClassNameCheck(
3234
new ClassCaseSensitivityCheck($reflectionProvider, true),
3335
new ClassForbiddenNameCheck($container),
@@ -570,6 +572,13 @@ public function testBug14097(): void
570572
$this->analyse([__DIR__ . '/data/bug-14097.php'], []);
571573
}
572574

575+
#[RequiresPhp('>= 8.0')]
576+
public function testBug13440(): void
577+
{
578+
$this->checkExplicitMixed = true;
579+
$this->analyse([__DIR__ . '/data/bug-13440.php'], []);
580+
}
581+
573582
public function testNewStaticWithConsistentConstructor(): void
574583
{
575584
$this->analyse([__DIR__ . '/data/instantiation-new-static-consistent-constructor.php'], [
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug13440;
6+
7+
use Closure;
8+
9+
/** @template T */
10+
interface Foo {}
11+
12+
/**
13+
* @template TVal
14+
* @template TReturn
15+
*/
16+
class Box
17+
{
18+
/**
19+
* @param TVal $val
20+
* @param Closure(Foo<TVal>): TReturn $cb
21+
*/
22+
public function __construct(
23+
private mixed $val,
24+
private Closure $cb,
25+
) {
26+
}
27+
28+
/**
29+
* @template TNewReturn
30+
* @param Closure(Foo<TVal>): TNewReturn $cb
31+
* @return self<TVal, TNewReturn>
32+
*/
33+
public function test(Closure $cb): self
34+
{
35+
return new self($this->val, $cb);
36+
}
37+
}
38+
39+
/**
40+
* @template TVal
41+
* @template TReturn
42+
*/
43+
class Box2
44+
{
45+
/**
46+
* @param TVal $val
47+
* @param callable(Foo<TVal>): TReturn $cb
48+
*/
49+
public function __construct(
50+
private mixed $val,
51+
private $cb,
52+
) {
53+
}
54+
55+
/**
56+
* @template TNewReturn
57+
* @param callable(Foo<TVal>): TNewReturn $cb
58+
* @return self<TVal, TNewReturn>
59+
*/
60+
public function test($cb): self
61+
{
62+
return new self($this->val, $cb);
63+
}
64+
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,14 @@ public function testBug12250(): void
10251025
$this->analyse([__DIR__ . '/data/bug-12250.php'], []);
10261026
}
10271027

1028+
#[RequiresPhp('>= 8.0')]
1029+
public function testBug12688(): void
1030+
{
1031+
$this->checkExplicitMixed = true;
1032+
$this->checkImplicitMixed = true;
1033+
$this->analyse([__DIR__ . '/data/bug-12688.php'], []);
1034+
}
1035+
10281036
public function testBug4525(): void
10291037
{
10301038
$this->analyse([__DIR__ . '/data/bug-4525.php'], []);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug12688;
4+
5+
/**
6+
* @template T = mixed
7+
*/
8+
interface I {}
9+
10+
/**
11+
* @implements I<mixed>
12+
*/
13+
enum E implements I
14+
{
15+
case E;
16+
}
17+
18+
/**
19+
* @template T
20+
*/
21+
final class TemplateWithoutDefaultWorks
22+
{
23+
/**
24+
* @var I<T>
25+
*/
26+
public readonly I $i;
27+
28+
/**
29+
* @param I<T> $i
30+
*/
31+
public function __construct(I $i = E::E)
32+
{
33+
$this->i = $i;
34+
}
35+
}
36+
37+
/**
38+
* @template T = mixed
39+
*/
40+
final class TemplateWithDefaultDoesNotWork
41+
{
42+
/**
43+
* @var I<T>
44+
*/
45+
public readonly I $i;
46+
47+
/**
48+
* @param I<T> $i
49+
*/
50+
public function __construct(I $i = E::E)
51+
{
52+
$this->i = $i;
53+
}
54+
}

0 commit comments

Comments
 (0)