Lines
76.92%
10 / 13
Functions and Methods
71.42%
5 / 7
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppClassReflection | 76.92% | 10 / 13 | 71.42% | 5 / 7 | 15.08 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findForClassName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| getName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| findAutomation | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| isTemplate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| isRunnableApp | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 6 | |||||
| requireRunnableApp | 50.00% | 2 / 4 | 0.00% | 0 / 1 | 4.12 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\App; | |
| 6 | ||
| 7 | use ReflectionClass; | |
| 8 | use Stewart\Contracts\App; | |
| 9 | use Stewart\Contracts\AppTemplate; | |
| 10 | use Stewart\Contracts\Automation; | |
| 11 | use Stewart\Runtime\Exception\AppError; | |
| 12 | use Stewart\Runtime\Exception\AppException; | |
| 13 | ||
| 14 | final readonly class AppClassReflection | |
| 15 | { | |
| 16 | /** @param ReflectionClass<object> $reflection */ | |
| 17 | public function __construct(private ReflectionClass $reflection) {} | |
| 18 | ||
| 19 | public static function findForClassName(string $class): ?self | |
| 20 | { | |
| 21 | return class_exists($class) ? new self(new ReflectionClass($class)) : null; | |
| 22 | } | |
| 23 | ||
| 24 | /** @return class-string */ | |
| 25 | public function getName(): string | |
| 26 | { | |
| 27 | return $this->reflection->getName(); | |
| 28 | } | |
| 29 | ||
| 30 | /** @throws AppException */ | |
| 31 | public function findAutomation(): ?Automation | |
| 32 | { | |
| 33 | $automation = $this->reflection->getAttributes(Automation::class)[0] ?? null; | |
| 34 | ||
| 35 | if ($automation !== null && $this->isTemplate()) { | |
| 36 | throw AppException::createForReason(AppError::TemplateAndAutomation, className: $this->getName()); | |
| 37 | } | |
| 38 | ||
| 39 | return $automation?->newInstance(); | |
| 40 | } | |
| 41 | ||
| 42 | public function isTemplate(): bool | |
| 43 | { | |
| 44 | return $this->reflection->getAttributes(AppTemplate::class) !== []; | |
| 45 | } | |
| 46 | ||
| 47 | public function isRunnableApp(): bool | |
| 48 | { | |
| 49 | return $this->reflection->isInstantiable() && $this->reflection->implementsInterface(App::class); | |
| 50 | } | |
| 51 | ||
| 52 | /** @throws AppException */ | |
| 53 | public function requireRunnableApp(string $attribute): void | |
| 54 | { | |
| 55 | if (!$this->reflection->implementsInterface(App::class)) { | |
| 56 | throw AppException::createForReason(AppError::NotAnApp, className: $this->getName(), attribute: $attribute, appInterface: App::class); | |
| 57 | } | |
| 58 | ||
| 59 | if (!$this->reflection->isInstantiable()) { | |
| 60 | throw AppException::createForReason(AppError::NotInstantiable, className: $this->getName(), attribute: $attribute); | |
| 61 | } | |
| 62 | } | |
| 63 | } |