From f3a9cfc78d855aeebc100ca305a7382bfbfe7a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 5 Feb 2020 17:16:34 +0100 Subject: [PATCH] Fix: Do not import every single class --- ...ophecyRevealDynamicReturnTypeExtension.php | 33 +++++++++---------- ...dOrImplementDynamicReturnTypeExtension.php | 29 ++++++++-------- ...etProphesizeDynamicReturnTypeExtension.php | 29 ++++++++-------- src/PhpDoc/TypeNodeResolverExtension.php | 23 ++++++------- .../ObjectProphecyMethodReflection.php | 27 +++++++-------- ...rophecyMethodsClassReflectionExtension.php | 10 +++--- src/Type/ObjectProphecyType.php | 10 +++--- 7 files changed, 73 insertions(+), 88 deletions(-) diff --git a/src/Extension/ObjectProphecyRevealDynamicReturnTypeExtension.php b/src/Extension/ObjectProphecyRevealDynamicReturnTypeExtension.php index d3a31d3..fe0c9fa 100644 --- a/src/Extension/ObjectProphecyRevealDynamicReturnTypeExtension.php +++ b/src/Extension/ObjectProphecyRevealDynamicReturnTypeExtension.php @@ -14,30 +14,29 @@ namespace JanGregor\Prophecy\Extension; use JanGregor\Prophecy\Type\ObjectProphecyType; -use PhpParser\Node\Expr\MethodCall; -use PHPStan\Analyser\Scope; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; -use PHPStan\Type\DynamicMethodReturnTypeExtension; -use PHPStan\Type\ObjectType; -use PHPStan\Type\Type; -use PHPStan\Type\TypeCombinator; - -class ObjectProphecyRevealDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +use PhpParser\Node; +use PHPStan\Analyser; +use PHPStan\Reflection; +use PHPStan\Type; + +class ObjectProphecyRevealDynamicReturnTypeExtension implements Type\DynamicMethodReturnTypeExtension { public function getClass(): string { return 'Prophecy\Prophecy\ObjectProphecy'; } - public function isMethodSupported(MethodReflection $methodReflection): bool + public function isMethodSupported(Reflection\MethodReflection $methodReflection): bool { return 'reveal' === $methodReflection->getName(); } - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type - { - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + public function getTypeFromMethodCall( + Reflection\MethodReflection $methodReflection, + Node\Expr\MethodCall $methodCall, + Analyser\Scope $scope + ): Type\Type { + $parametersAcceptor = Reflection\ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); $calledOnType = $scope->getType($methodCall->var); @@ -47,12 +46,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method return $returnType; } - $types = \array_map(static function (string $class): ObjectType { - return new ObjectType($class); + $types = \array_map(static function (string $class): Type\ObjectType { + return new Type\ObjectType($class); }, $calledOnType->getProphesizedClasses()); $types[] = $returnType; - return TypeCombinator::intersect(...$types); + return Type\TypeCombinator::intersect(...$types); } } diff --git a/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php b/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php index ec8289b..bd34d5e 100644 --- a/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php +++ b/src/Extension/ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension.php @@ -14,24 +14,20 @@ namespace JanGregor\Prophecy\Extension; use JanGregor\Prophecy\Type\ObjectProphecyType; -use PhpParser\Node\Expr\MethodCall; -use PHPStan\Analyser\Scope; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; +use PhpParser\Node; +use PHPStan\Analyser; +use PHPStan\Reflection; use PHPStan\ShouldNotHappenException; -use PHPStan\Type\Constant\ConstantStringType; -use PHPStan\Type\DynamicMethodReturnTypeExtension; -use PHPStan\Type\Type; -use PHPStan\Type\TypeWithClassName; +use PHPStan\Type; -class ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +class ObjectProphecyWillExtendOrImplementDynamicReturnTypeExtension implements Type\DynamicMethodReturnTypeExtension { public function getClass(): string { return 'Prophecy\Prophecy\ObjectProphecy'; } - public function isMethodSupported(MethodReflection $methodReflection): bool + public function isMethodSupported(Reflection\MethodReflection $methodReflection): bool { $methodNames = [ 'willImplement', @@ -45,9 +41,12 @@ public function isMethodSupported(MethodReflection $methodReflection): bool ); } - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type - { - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + public function getTypeFromMethodCall( + Reflection\MethodReflection $methodReflection, + Node\Expr\MethodCall $methodCall, + Analyser\Scope $scope + ): Type\Type { + $parametersAcceptor = Reflection\ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); $calledOnType = $scope->getType($methodCall->var); @@ -63,13 +62,13 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method $argumentType = $scope->getType($methodCall->args[0]->value); - if (!$argumentType instanceof ConstantStringType) { + if (!$argumentType instanceof Type\Constant\ConstantStringType) { return $returnType; } $className = $argumentType->getValue(); - if (!$returnType instanceof TypeWithClassName) { + if (!$returnType instanceof Type\TypeWithClassName) { throw new ShouldNotHappenException(); } diff --git a/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php b/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php index dfeba7f..8160f20 100644 --- a/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php +++ b/src/Extension/ProphetProphesizeDynamicReturnTypeExtension.php @@ -14,17 +14,13 @@ namespace JanGregor\Prophecy\Extension; use JanGregor\Prophecy\Type\ObjectProphecyType; -use PhpParser\Node\Expr\MethodCall; -use PHPStan\Analyser\Scope; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; +use PhpParser\Node; +use PHPStan\Analyser; +use PHPStan\Reflection; use PHPStan\ShouldNotHappenException; -use PHPStan\Type\Constant\ConstantStringType; -use PHPStan\Type\DynamicMethodReturnTypeExtension; -use PHPStan\Type\Type; -use PHPStan\Type\TypeWithClassName; +use PHPStan\Type; -class ProphetProphesizeDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +class ProphetProphesizeDynamicReturnTypeExtension implements Type\DynamicMethodReturnTypeExtension { private $className; @@ -38,14 +34,17 @@ public function getClass(): string return $this->className; } - public function isMethodSupported(MethodReflection $methodReflection): bool + public function isMethodSupported(Reflection\MethodReflection $methodReflection): bool { return 'prophesize' === $methodReflection->getName(); } - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type - { - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + public function getTypeFromMethodCall( + Reflection\MethodReflection $methodReflection, + Node\Expr\MethodCall $methodCall, + Analyser\Scope $scope + ): Type\Type { + $parametersAcceptor = Reflection\ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); $returnType = $parametersAcceptor->getReturnType(); @@ -55,13 +54,13 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method $argumentType = $scope->getType($methodCall->args[0]->value); - if (!$argumentType instanceof ConstantStringType) { + if (!$argumentType instanceof Type\Constant\ConstantStringType) { return $returnType; } $className = $argumentType->getValue(); - if (!$returnType instanceof TypeWithClassName) { + if (!$returnType instanceof Type\TypeWithClassName) { throw new ShouldNotHappenException(); } diff --git a/src/PhpDoc/TypeNodeResolverExtension.php b/src/PhpDoc/TypeNodeResolverExtension.php index c502399..0900dc4 100644 --- a/src/PhpDoc/TypeNodeResolverExtension.php +++ b/src/PhpDoc/TypeNodeResolverExtension.php @@ -14,23 +14,20 @@ namespace JanGregor\Prophecy\PhpDoc; use JanGregor\Prophecy\Type\ObjectProphecyType; -use PHPStan\Analyser\NameScope; -use PHPStan\PhpDoc\TypeNodeResolver; -use PHPStan\PhpDoc\TypeNodeResolverAwareExtension; -use PHPStan\PhpDocParser\Ast\Type\TypeNode; -use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; -use PHPStan\Type\Type; -use PHPStan\Type\TypeWithClassName; +use PHPStan\Analyser; +use PHPStan\PhpDoc; +use PHPStan\PhpDocParser; +use PHPStan\Type; use Prophecy\Prophecy\ObjectProphecy; -class TypeNodeResolverExtension implements \PHPStan\PhpDoc\TypeNodeResolverExtension, TypeNodeResolverAwareExtension +class TypeNodeResolverExtension implements PhpDoc\TypeNodeResolverAwareExtension, PhpDoc\TypeNodeResolverExtension { /** - * @var TypeNodeResolver + * @var PhpDoc\TypeNodeResolver */ private $typeNodeResolver; - public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void + public function setTypeNodeResolver(PhpDoc\TypeNodeResolver $typeNodeResolver): void { $this->typeNodeResolver = $typeNodeResolver; } @@ -40,9 +37,9 @@ public function getCacheKey(): string return 'prophecy-v1'; } - public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type + public function resolve(PhpDocParser\Ast\Type\TypeNode $typeNode, Analyser\NameScope $nameScope): ?Type\Type { - if (!$typeNode instanceof UnionTypeNode) { + if (!$typeNode instanceof PhpDocParser\Ast\Type\UnionTypeNode) { return null; } @@ -53,7 +50,7 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type foreach ($typeNode->types as $innerType) { $type = $this->typeNodeResolver->resolve($innerType, $nameScope); - if ($type instanceof TypeWithClassName) { + if ($type instanceof Type\TypeWithClassName) { if (ObjectProphecy::class === $type->getClassName()) { $objectProphecyType = $type; } else { diff --git a/src/Reflection/ObjectProphecyMethodReflection.php b/src/Reflection/ObjectProphecyMethodReflection.php index bc5da54..d02206a 100644 --- a/src/Reflection/ObjectProphecyMethodReflection.php +++ b/src/Reflection/ObjectProphecyMethodReflection.php @@ -13,29 +13,24 @@ namespace JanGregor\Prophecy\Reflection; -use PHPStan\Reflection\ClassMemberReflection; -use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\FunctionVariant; -use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection; use PHPStan\TrinaryLogic; -use PHPStan\Type\Generic\TemplateTypeMap; -use PHPStan\Type\ObjectType; -use PHPStan\Type\Type; -use Prophecy\Prophecy\MethodProphecy; +use PHPStan\Type; +use Prophecy\Prophecy; -class ObjectProphecyMethodReflection implements MethodReflection +class ObjectProphecyMethodReflection implements Reflection\MethodReflection { private $declaringClass; private $name; - public function __construct(ClassReflection $declaringClass, string $name) + public function __construct(Reflection\ClassReflection $declaringClass, string $name) { $this->declaringClass = $declaringClass; $this->name = $name; } - public function getDeclaringClass(): ClassReflection + public function getDeclaringClass(): Reflection\ClassReflection { return $this->declaringClass; } @@ -63,17 +58,17 @@ public function getName(): string public function getVariants(): array { return [ - new FunctionVariant( - TemplateTypeMap::createEmpty(), + new Reflection\FunctionVariant( + Type\Generic\TemplateTypeMap::createEmpty(), null, [], true, - new ObjectType(MethodProphecy::class) + new Type\ObjectType(Prophecy\MethodProphecy::class) ), ]; } - public function getPrototype(): ClassMemberReflection + public function getPrototype(): Reflection\ClassMemberReflection { return $this; } @@ -98,7 +93,7 @@ public function isInternal(): TrinaryLogic return TrinaryLogic::createNo(); } - public function getThrowType(): ?Type + public function getThrowType(): ?Type\Type { return null; } diff --git a/src/Reflection/ProphecyMethodsClassReflectionExtension.php b/src/Reflection/ProphecyMethodsClassReflectionExtension.php index d7da77d..4482134 100644 --- a/src/Reflection/ProphecyMethodsClassReflectionExtension.php +++ b/src/Reflection/ProphecyMethodsClassReflectionExtension.php @@ -13,20 +13,18 @@ namespace JanGregor\Prophecy\Reflection; -use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\MethodsClassReflectionExtension; +use PHPStan\Reflection; -class ProphecyMethodsClassReflectionExtension implements MethodsClassReflectionExtension +class ProphecyMethodsClassReflectionExtension implements Reflection\MethodsClassReflectionExtension { - public function hasMethod(ClassReflection $classReflection, string $methodName): bool + public function hasMethod(Reflection\ClassReflection $classReflection, string $methodName): bool { // don't know which class is prophesized here, so let's say yes to every method // must match class in MockBuilderType parent::__construct() equivalent return 'Prophecy\Prophecy\ObjectProphecy' === $classReflection->getName(); } - public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection + public function getMethod(Reflection\ClassReflection $classReflection, string $methodName): Reflection\MethodReflection { return new ObjectProphecyMethodReflection($classReflection, $methodName); } diff --git a/src/Type/ObjectProphecyType.php b/src/Type/ObjectProphecyType.php index 9dc6e0c..f51568f 100644 --- a/src/Type/ObjectProphecyType.php +++ b/src/Type/ObjectProphecyType.php @@ -13,11 +13,9 @@ namespace JanGregor\Prophecy\Type; -use PHPStan\Type\ObjectType; -use PHPStan\Type\Type; -use PHPStan\Type\VerbosityLevel; +use PHPStan\Type; -class ObjectProphecyType extends ObjectType +class ObjectProphecyType extends Type\ObjectType { /** * @var string[] @@ -31,12 +29,12 @@ public function __construct(string ...$prophesizedClasses) parent::__construct('Prophecy\Prophecy\ObjectProphecy'); } - public static function __set_state(array $properties): Type + public static function __set_state(array $properties): Type\Type { return new self($properties['prophesizedClasses']); } - public function describe(VerbosityLevel $level): string + public function describe(Type\VerbosityLevel $level): string { return \sprintf( '%s<%s>',