Skip to content

Commit 880eaa0

Browse files
committed
Implement FunctionReflection::getOnlyVariant() and ExtendedMethodReflection::getOnlyVariant() as an internal replacement for ParametersAcceptorSelector::selectSingle()
1 parent c30921b commit 880eaa0

File tree

69 files changed

+238
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+238
-132
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
17141714
return new MixedType();
17151715
}
17161716

1717-
$returnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
1717+
$returnType = $functionReflection->getOnlyVariant()->getReturnType();
17181718
$generatorSendType = $returnType->getTemplateType(Generator::class, 'TSend');
17191719
if ($generatorSendType instanceof ErrorType) {
17201720
return new MixedType();
@@ -3132,7 +3132,7 @@ private function enterFunctionLike(
31323132
bool $preserveThis,
31333133
): self
31343134
{
3135-
$acceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants());
3135+
$acceptor = $functionReflection->getOnlyVariant();
31363136
$parametersByName = [];
31373137

31383138
foreach ($acceptor->getParameters() as $parameter) {
@@ -5508,7 +5508,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type
55085508

55095509
$assignedToProperty = $node->getAttribute(NewAssignedToPropertyVisitor::ATTRIBUTE_NAME);
55105510
if ($assignedToProperty !== null) {
5511-
$constructorVariant = ParametersAcceptorSelector::selectSingle($constructorMethod->getVariants());
5511+
$constructorVariant = $constructorMethod->getOnlyVariant();
55125512
$classTemplateTypes = $classReflection->getTemplateTypeMap()->getTypes();
55135513
$originalClassTemplateTypes = $classTemplateTypes;
55145514
foreach ($constructorVariant->getParameters() as $parameter) {

src/Dependency/DependencyResolver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use PHPStan\Reflection\ClassReflection;
1919
use PHPStan\Reflection\FunctionReflection;
2020
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
21-
use PHPStan\Reflection\ParametersAcceptorSelector;
2221
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
2322
use PHPStan\Reflection\ReflectionProvider;
2423
use PHPStan\Type\ClosureType;
@@ -70,7 +69,7 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
7069
}
7170
} elseif ($node instanceof InClassMethodNode) {
7271
$nativeMethod = $node->getMethodReflection();
73-
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($nativeMethod->getVariants());
72+
$parametersAcceptor = $nativeMethod->getOnlyVariant();
7473
$this->extractThrowType($nativeMethod->getThrowType(), $dependenciesReflections);
7574
$this->extractFromParametersAcceptor($parametersAcceptor, $dependenciesReflections);
7675
foreach ($nativeMethod->getAsserts()->getAll() as $assertTag) {
@@ -103,7 +102,7 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
103102
} elseif ($node instanceof InFunctionNode) {
104103
$functionReflection = $node->getFunctionReflection();
105104
$this->extractThrowType($functionReflection->getThrowType(), $dependenciesReflections);
106-
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants());
105+
$parametersAcceptor = $functionReflection->getOnlyVariant();
107106

108107
$this->extractFromParametersAcceptor($parametersAcceptor, $dependenciesReflections);
109108
foreach ($functionReflection->getAsserts()->getAll() as $assertTag) {

src/Internal/ContainerDynamicReturnTypeExtension.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3333
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
3434
{
3535
if (count($methodCall->getArgs()) === 0) {
36-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
36+
return ParametersAcceptorSelector::selectFromArgs(
37+
$scope,
38+
$methodCall->getArgs(),
39+
$methodReflection->getVariants(),
40+
)->getReturnType();
3741
}
3842
$argType = $scope->getType($methodCall->getArgs()[0]->value);
3943
if (!$argType instanceof ConstantStringType) {
40-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
44+
return ParametersAcceptorSelector::selectFromArgs(
45+
$scope,
46+
$methodCall->getArgs(),
47+
$methodReflection->getVariants(),
48+
)->getReturnType();
4149
}
4250

4351
$type = new ObjectType($argType->getValue());

src/Reflection/Annotations/AnnotationMethodReflection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ExtendedMethodReflection;
99
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
10+
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
1011
use PHPStan\TrinaryLogic;
1112
use PHPStan\Type\Generic\TemplateTypeMap;
1213
use PHPStan\Type\MixedType;
@@ -83,6 +84,11 @@ public function getVariants(): array
8384
return $this->variants;
8485
}
8586

87+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
88+
{
89+
return $this->getVariants()[0];
90+
}
91+
8692
public function getNamedArgumentsVariants(): ?array
8793
{
8894
return null;

src/Reflection/ClassReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ private function findAttributeFlags(): ?int
13051305
return null;
13061306
}
13071307
$attributeConstructor = $attributeClass->getConstructor();
1308-
$attributeConstructorVariant = ParametersAcceptorSelector::selectSingle($attributeConstructor->getVariants());
1308+
$attributeConstructorVariant = $attributeConstructor->getOnlyVariant();
13091309

13101310
if (count($arguments) === 0) {
13111311
$flagType = $attributeConstructorVariant->getParameters()[0]->getDefaultValue();

src/Reflection/Dummy/ChangedTypeMethodReflection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ExtendedMethodReflection;
99
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
10+
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\TrinaryLogic;
1112
use PHPStan\Type\Type;
13+
use function count;
1214
use function is_bool;
1315

1416
final class ChangedTypeMethodReflection implements ExtendedMethodReflection
@@ -62,6 +64,16 @@ public function getVariants(): array
6264
return $this->variants;
6365
}
6466

67+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
68+
{
69+
$variants = $this->getVariants();
70+
if (count($variants) !== 1) {
71+
throw new ShouldNotHappenException();
72+
}
73+
74+
return $variants[0];
75+
}
76+
6577
public function getNamedArgumentsVariants(): ?array
6678
{
6779
return $this->namedArgumentsVariants;

src/Reflection/Dummy/DummyConstructorReflection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ExtendedMethodReflection;
99
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
10+
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
1011
use PHPStan\TrinaryLogic;
1112
use PHPStan\Type\Generic\TemplateTypeMap;
1213
use PHPStan\Type\MixedType;
@@ -66,6 +67,11 @@ public function getVariants(): array
6667
];
6768
}
6869

70+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
71+
{
72+
return $this->getVariants()[0];
73+
}
74+
6975
public function getNamedArgumentsVariants(): ?array
7076
{
7177
return null;

src/Reflection/Dummy/DummyMethodReflection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Reflection\ClassMemberReflection;
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\ExtendedMethodReflection;
9+
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
910
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
1011
use PHPStan\Reflection\TrivialParametersAcceptor;
1112
use PHPStan\TrinaryLogic;
@@ -58,6 +59,11 @@ public function getVariants(): array
5859
];
5960
}
6061

62+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
63+
{
64+
return $this->getVariants()[0];
65+
}
66+
6167
public function getNamedArgumentsVariants(): ?array
6268
{
6369
return null;

src/Reflection/ExtendedMethodReflection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ interface ExtendedMethodReflection extends MethodReflection
2727
*/
2828
public function getVariants(): array;
2929

30+
/**
31+
* @internal
32+
*/
33+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs;
34+
3035
/**
3136
* @return ParametersAcceptorWithPhpDocs[]|null
3237
*/

src/Reflection/FunctionReflection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public function getFileName(): ?string;
1818
*/
1919
public function getVariants(): array;
2020

21+
/**
22+
* @internal
23+
*/
24+
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs;
25+
2126
/**
2227
* @return ParametersAcceptorWithPhpDocs[]|null
2328
*/

0 commit comments

Comments
 (0)