ray-di /
Ray.Di
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ray\Di; |
||
| 6 | |||
| 7 | use Ray\Aop\ReflectionClass; |
||
| 8 | use Ray\Aop\ReflectionMethod; |
||
| 9 | use Ray\Di\Di\InjectInterface; |
||
| 10 | use ReflectionAttribute; |
||
| 11 | |||
| 12 | final class AnnotatedClassMethods |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @phpstan-param ReflectionClass<object> $class |
||
| 16 | */ |
||
| 17 | public function getConstructorName(ReflectionClass $class): Name |
||
| 18 | { |
||
| 19 | $constructor = $class->getConstructor(); |
||
| 20 | if (! $constructor) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 21 | return new Name(Name::ANY); |
||
| 22 | } |
||
| 23 | |||
| 24 | $reflMethod = new \ReflectionMethod($class->getName(), '__construct'); |
||
| 25 | $name = Name::withAttributes($reflMethod); |
||
| 26 | if ($name) { |
||
| 27 | return $name; |
||
| 28 | } |
||
| 29 | |||
| 30 | return new Name(Name::ANY); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getSetterMethod(ReflectionMethod $method): ?SetterMethod |
||
| 34 | { |
||
| 35 | $inject = $method->getAnnotation(InjectInterface::class, ReflectionAttribute::IS_INSTANCEOF); |
||
| 36 | if (! $inject instanceof InjectInterface) { |
||
| 37 | return null; |
||
| 38 | } |
||
| 39 | |||
| 40 | $name = $this->getName($method); |
||
| 41 | $setterMethod = new SetterMethod($method, $name); |
||
| 42 | if ($inject->isOptional()) { |
||
| 43 | $setterMethod->setOptional(); |
||
| 44 | } |
||
| 45 | |||
| 46 | return $setterMethod; |
||
| 47 | } |
||
| 48 | |||
| 49 | private function getName(ReflectionMethod $method): Name |
||
| 50 | { |
||
| 51 | $name = Name::withAttributes($method); |
||
| 52 | if ($name) { |
||
| 53 | return $name; |
||
| 54 | } |
||
| 55 | |||
| 56 | return new Name(Name::ANY); |
||
| 57 | } |
||
| 58 | } |
||
| 59 |