|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Proget\PHPStan\PhpSpec\Reflection; |
| 6 | + |
| 7 | +use PhpSpec\Wrapper\Collaborator; |
| 8 | +use PHPStan\Analyser\OutOfClassScope; |
| 9 | +use PHPStan\Broker\Broker; |
| 10 | +use PHPStan\Reflection\BrokerAwareExtension; |
| 11 | +use PHPStan\Reflection\ClassReflection; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Reflection\PropertiesClassReflectionExtension; |
| 14 | +use PHPStan\Reflection\PropertyReflection; |
| 15 | +use Proget\PHPStan\PhpSpec\Registry\SpoofedCollaboratorRegistry; |
| 16 | +use Proget\PHPStan\PhpSpec\Wrapper\SpoofedCollaborator; |
| 17 | + |
| 18 | +final class SpoofedCollaboratorPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension, BrokerAwareExtension |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Broker |
| 22 | + */ |
| 23 | + private $broker; |
| 24 | + |
| 25 | + public function setBroker(Broker $broker): void |
| 26 | + { |
| 27 | + $this->broker = $broker; |
| 28 | + } |
| 29 | + |
| 30 | + public function hasProperty(ClassReflection $classReflection, string $propertyName): bool |
| 31 | + { |
| 32 | + return in_array(SpoofedCollaborator::class, array_map(function (ClassReflection $interface):string { |
| 33 | + return $interface->getName(); |
| 34 | + }, $classReflection->getInterfaces()), true); |
| 35 | + } |
| 36 | + |
| 37 | + public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection |
| 38 | + { |
| 39 | + $collaboratorClassName = (string) preg_replace('/Collaborator$/', '', SpoofedCollaboratorRegistry::getAlias($classReflection->getName())); |
| 40 | + |
| 41 | + return new CollaboratorPropertyReflection($this->broker->getClass($collaboratorClassName)->getProperty($propertyName, new OutOfClassScope())); |
| 42 | + } |
| 43 | + |
| 44 | + public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 45 | + { |
| 46 | + return in_array(SpoofedCollaborator::class, array_map(function (ClassReflection $interface):string { |
| 47 | + return $interface->getName(); |
| 48 | + }, $classReflection->getInterfaces()), true); |
| 49 | + } |
| 50 | + |
| 51 | + public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 52 | + { |
| 53 | + $collaboratorReflection = $this->broker->getClass(Collaborator::class); |
| 54 | + if ($collaboratorReflection->hasMethod($methodName)) { |
| 55 | + return $collaboratorReflection->getMethod($methodName, new OutOfClassScope()); |
| 56 | + } |
| 57 | + |
| 58 | + $collaboratorClassName = (string) preg_replace('/Collaborator$/', '', SpoofedCollaboratorRegistry::getAlias($classReflection->getName())); |
| 59 | + |
| 60 | + return new CollaboratorMethodReflection($this->broker->getClass($collaboratorClassName)->getMethod($methodName, new OutOfClassScope())); |
| 61 | + } |
| 62 | +} |
0 commit comments