Skip to content

Commit c0f2e9e

Browse files
committed
Fix resolving generics from phpstorm-stubs
1 parent c0f6116 commit c0f2e9e

File tree

5 files changed

+77
-5
lines changed

5 files changed

+77
-5
lines changed

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ private function createNativeMethodVariant(
914914
$phpDocType = $stubPhpDocParameterTypes[$parameterSignature->getName()];
915915
} elseif (isset($phpDocParameterTypes[$phpDocParameterName])) {
916916
$phpDocType = $phpDocParameterTypes[$phpDocParameterName];
917+
$type = TypehintHelper::decideType($parameterSignature->getType(), $phpDocType);
917918
}
918919

919920
if (isset($stubPhpDocParameterOutTypes[$parameterSignature->getName()])) {

tests/PHPStan/Reflection/ParametersAcceptorSelectorTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
use PhpParser\Node\Name;
99
use PHPStan\Reflection\Native\NativeParameterReflection;
1010
use PHPStan\Reflection\Php\DummyParameter;
11+
use PHPStan\Reflection\Php\ExtendedDummyParameter;
1112
use PHPStan\Testing\PHPStanTestCase;
1213
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
1314
use PHPStan\Type\ArrayType;
1415
use PHPStan\Type\Constant\ConstantBooleanType;
1516
use PHPStan\Type\Constant\ConstantIntegerType;
1617
use PHPStan\Type\FloatType;
1718
use PHPStan\Type\Generic\TemplateTypeFactory;
19+
use PHPStan\Type\Generic\TemplateTypeHelper;
1820
use PHPStan\Type\Generic\TemplateTypeMap;
1921
use PHPStan\Type\Generic\TemplateTypeScope;
2022
use PHPStan\Type\Generic\TemplateTypeVariance;
23+
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
2124
use PHPStan\Type\IntegerType;
2225
use PHPStan\Type\IntersectionType;
2326
use PHPStan\Type\MixedType;
@@ -28,7 +31,9 @@
2831
use PHPStan\Type\Type;
2932
use PHPStan\Type\UnionType;
3033
use PHPStan\Type\VerbosityLevel;
34+
use PHPStan\Type\VoidType;
3135
use PHPUnit\Framework\Attributes\DataProvider;
36+
use function array_map;
3237
use function count;
3338

3439
class ParametersAcceptorSelectorTest extends PHPStanTestCase
@@ -69,7 +74,27 @@ public static function dataSelectFromTypes(): Generator
6974
],
7075
$datePeriodConstructorVariants,
7176
false,
72-
$datePeriodConstructorVariants[0],
77+
new FunctionVariant(
78+
TemplateTypeMap::createEmpty(),
79+
TemplateTypeMap::createEmpty(),
80+
array_map(static fn ($parameter) => new ExtendedDummyParameter(
81+
$parameter->getName(),
82+
TemplateTypeHelper::resolveToBounds($parameter->getType()),
83+
$parameter->isOptional(),
84+
$parameter->passedByReference(),
85+
$parameter->isVariadic(),
86+
$parameter->getDefaultValue(),
87+
$parameter->getNativeType(),
88+
$parameter->getPhpDocType(),
89+
$parameter->getOutType(),
90+
$parameter->isImmediatelyInvokedCallable(),
91+
$parameter->getClosureThisType(),
92+
$parameter->getAttributes(),
93+
), $datePeriodConstructorVariants[0]->getParameters()),
94+
false,
95+
new VoidType(),
96+
TemplateTypeVarianceMap::createEmpty(),
97+
),
7398
];
7499
yield [
75100
[
@@ -80,7 +105,27 @@ public static function dataSelectFromTypes(): Generator
80105
],
81106
$datePeriodConstructorVariants,
82107
false,
83-
$datePeriodConstructorVariants[1],
108+
new FunctionVariant(
109+
TemplateTypeMap::createEmpty(),
110+
TemplateTypeMap::createEmpty(),
111+
array_map(static fn ($parameter) => new ExtendedDummyParameter(
112+
$parameter->getName(),
113+
TemplateTypeHelper::resolveToBounds($parameter->getType()),
114+
$parameter->isOptional(),
115+
$parameter->passedByReference(),
116+
$parameter->isVariadic(),
117+
$parameter->getDefaultValue(),
118+
$parameter->getNativeType(),
119+
$parameter->getPhpDocType(),
120+
$parameter->getOutType(),
121+
$parameter->isImmediatelyInvokedCallable(),
122+
$parameter->getClosureThisType(),
123+
$parameter->getAttributes(),
124+
), $datePeriodConstructorVariants[1]->getParameters()),
125+
false,
126+
new VoidType(),
127+
TemplateTypeVarianceMap::createEmpty(),
128+
),
84129
];
85130
yield [
86131
[

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public function testBug7048(): void
395395
21,
396396
],
397397
[
398-
'Missing parameter $end (DateTimeInterface|int) in call to DatePeriod constructor.',
398+
'Missing parameter $end (int|TEnd of DateTimeInterface) in call to DatePeriod constructor.',
399399
18,
400400
],
401401
[
@@ -407,11 +407,11 @@ public function testBug7048(): void
407407
24,
408408
],
409409
[
410-
'Parameter #3 $end of class DatePeriod constructor expects DateTimeInterface|int, string given.',
410+
'Parameter #3 $end of class DatePeriod constructor expects int|TEnd of DateTimeInterface, string given.',
411411
41,
412412
],
413413
[
414-
'Parameter $end of class DatePeriod constructor expects DateTimeInterface|int, string given.',
414+
'Parameter $end of class DatePeriod constructor expects int|TEnd of DateTimeInterface, string given.',
415415
49,
416416
],
417417
]);

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,4 +3556,20 @@ public function testBug12940(): void
35563556
$this->analyse([__DIR__ . '/data/bug-12940.php'], []);
35573557
}
35583558

3559+
#[RequiresPhp('>= 8.1')]
3560+
public function testBug13171(): void
3561+
{
3562+
$this->checkThisOnly = false;
3563+
$this->checkNullables = true;
3564+
$this->checkUnionTypes = true;
3565+
$this->checkExplicitMixed = true;
3566+
3567+
$this->analyse([__DIR__ . '/data/bug-13171.php'], [
3568+
[
3569+
'Parameter #1 $value of method Fiber<void,void,void,void>::resume() expects void, int given.',
3570+
9,
3571+
],
3572+
]);
3573+
}
3574+
35593575
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Bug13171;
4+
5+
use Fiber;
6+
7+
/** @param Fiber<void, void, void, void> $a */
8+
function foo (Fiber $a): void {
9+
$a->resume(1);
10+
};

0 commit comments

Comments
 (0)