Skip to content

Commit

Permalink
Introduce @internal getOnlyVariant() method on FunctionReflection…
Browse files Browse the repository at this point in the history
…/ExtendedMethodReflection to use instead of `selectSingle()`
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent 1322aaf commit 714877b
Show file tree
Hide file tree
Showing 28 changed files with 149 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5514,7 +5514,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type

$assignedToProperty = $node->getAttribute(NewAssignedToPropertyVisitor::ATTRIBUTE_NAME);
if ($assignedToProperty !== null) {
$constructorVariant = ParametersAcceptorSelector::selectSingle($constructorMethod->getVariants());
$constructorVariant = $constructorMethod->getOnlyVariant();
$classTemplateTypes = $classReflection->getTemplateTypeMap()->getTypes();
$originalClassTemplateTypes = $classTemplateTypes;
foreach ($constructorVariant->getParameters() as $parameter) {
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -83,6 +84,11 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ private function findAttributeFlags(): ?int
return null;
}
$attributeConstructor = $attributeClass->getConstructor();
$attributeConstructorVariant = ParametersAcceptorSelector::selectSingle($attributeConstructor->getVariants());
$attributeConstructorVariant = $attributeConstructor->getOnlyVariant();

if (count($arguments) === 0) {
$flagType = $attributeConstructorVariant->getParameters()[0]->getDefaultValue();
Expand Down
12 changes: 12 additions & 0 deletions src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use function count;
use function is_bool;

final class ChangedTypeMethodReflection implements ExtendedMethodReflection
Expand Down Expand Up @@ -62,6 +64,16 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
$variants = $this->getVariants();
if (count($variants) !== 1) {
throw new ShouldNotHappenException();
}

return $variants[0];
}

public function getNamedArgumentsVariants(): ?array
{
return $this->namedArgumentsVariants;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Dummy/DummyConstructorReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -66,6 +67,11 @@ public function getVariants(): array
];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Dummy/DummyMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\TrinaryLogic;
Expand Down Expand Up @@ -58,6 +59,11 @@ public function getVariants(): array
];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/ExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ interface ExtendedMethodReflection extends MethodReflection
*/
public function getVariants(): array;

/**
* @internal
*/
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs;

/**
* @return ParametersAcceptorWithPhpDocs[]|null
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/FunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function getFileName(): ?string;
*/
public function getVariants(): array;

/**
* @internal
*/
public function getOnlyVariant(): ParametersAcceptorWithPhpDocs;

/**
* @return ParametersAcceptorWithPhpDocs[]|null
*/
Expand Down
15 changes: 12 additions & 3 deletions src/Reflection/Native/NativeFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use function count;

final class NativeFunctionReflection implements FunctionReflection
{
Expand Down Expand Up @@ -46,14 +48,21 @@ public function getFileName(): ?string
return null;
}

/**
* @return ParametersAcceptorWithPhpDocs[]
*/
public function getVariants(): array
{
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
$variants = $this->getVariants();
if (count($variants) !== 1) {
throw new ShouldNotHappenException();
}

return $variants[0];
}

public function getNamedArgumentsVariants(): ?array
{
return $this->namedArgumentsVariants;
Expand Down
12 changes: 12 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\Php\BuiltinMethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use ReflectionException;
use function count;
use function strtolower;

final class NativeMethodReflection implements ExtendedMethodReflection
Expand Down Expand Up @@ -109,6 +111,16 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
$variants = $this->getVariants();
if (count($variants) !== 1) {
throw new ShouldNotHappenException();
}

return $variants[0];
}

public function getNamedArgumentsVariants(): ?array
{
return $this->namedArgumentsVariants;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Php/ClosureCallMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\PassedByReference;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ClosureType;
Expand Down Expand Up @@ -105,6 +106,11 @@ public function getVariants(): array
];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Php/EnumCasesMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand Down Expand Up @@ -75,6 +76,11 @@ public function getVariants(): array
];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/ExitFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function getVariants(): array
];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

/**
* @return ParametersAcceptorWithPhpDocs[]
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this;
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/PhpFunctionReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ public function getVariants(): array
return $this->variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -66,13 +65,13 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
}

if ($classReflection->hasNativeMethod('__get')) {
$readableType = ParametersAcceptorSelector::selectSingle($classReflection->getNativeMethod('__get')->getVariants())->getReturnType();
$readableType = $classReflection->getNativeMethod('__get')->getOnlyVariant()->getReturnType();
} else {
$readableType = new MixedType();
}

if ($classReflection->hasNativeMethod('__set')) {
$writableType = ParametersAcceptorSelector::selectSingle($classReflection->getNativeMethod('__set')->getVariants())->getParameters()[1]->getType();
$writableType = $classReflection->getNativeMethod('__set')->getOnlyVariant()->getParameters()[1]->getType();
} else {
$writableType = new MixedType();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/ResolvedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function getVariants(): array
return $this->variants = $this->resolveVariants($this->reflection->getVariants());
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
$variants = $this->namedArgumentVariants;
Expand Down
11 changes: 11 additions & 0 deletions src/Reflection/Type/IntersectionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -94,6 +95,16 @@ public function getVariants(): array
), $this->methods[0]->getVariants());
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
$variants = $this->getVariants();
if (count($variants) !== 1) {
throw new ShouldNotHappenException();
}

return $variants[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/Type/UnionTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -82,6 +83,11 @@ public function getVariants(): array
return [ParametersAcceptorSelector::combineAcceptors($variants)];
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/WrappedExtendedMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public function getVariants(): array
return $variants;
}

public function getOnlyVariant(): ParametersAcceptorWithPhpDocs
{
return $this->getVariants()[0];
}

public function getNamedArgumentsVariants(): ?array
{
return null;
Expand Down
Loading

0 comments on commit 714877b

Please sign in to comment.