| Total Complexity | 41 |
| Total Lines | 192 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ListenerClassFile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ListenerClassFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class ListenerClassFile |
||
| 28 | { |
||
| 29 | private readonly string $className; |
||
| 30 | /** @var array<array{source: string, event: string, method: string, priority: int}> */ |
||
| 31 | private array $listenerBindings = []; |
||
| 32 | |||
| 33 | public function __construct( |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sets the dependency injection container |
||
| 42 | * |
||
| 43 | * @param ContainerInterface $container The container to be set |
||
| 44 | * @return self Returns an instance of the class with the updated container |
||
| 45 | */ |
||
| 46 | public function withContainer(ContainerInterface $container): self |
||
| 47 | { |
||
| 48 | $this->container = $container; |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Check if the object has any listener bindings. |
||
| 54 | * |
||
| 55 | * @return bool True if the object has listener bindings, false otherwise. |
||
| 56 | */ |
||
| 57 | public function isListener(): bool |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the listener bindings of the Symfony application. |
||
| 64 | * |
||
| 65 | * @return array<array{source: string, event: string, method: string, priority: int}> The listener bindings array |
||
| 66 | */ |
||
| 67 | public function bindings(): array |
||
| 68 | { |
||
| 69 | return $this->listenerBindings; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Resolves the listeners for the Symfony application. |
||
| 74 | * |
||
| 75 | * @return array<array{event: string, listener: EventListener|CallableEventListener, priority: int}> |
||
| 76 | * The resolved listeners array |
||
| 77 | * @throws ContainerExceptionInterface |
||
| 78 | * @throws NotFoundExceptionInterface |
||
| 79 | * @throws RuntimeException When the container is not available |
||
| 80 | */ |
||
| 81 | public function resolveListeners(): array |
||
| 82 | { |
||
| 83 | if (!$this->container) { |
||
| 84 | throw new RuntimeException("Container not available."); |
||
| 85 | } |
||
| 86 | |||
| 87 | $instance = $this->container->get($this->className); |
||
| 88 | |||
| 89 | return array_map(function ($binding) use ($instance) { |
||
| 90 | $method = $binding['method']; |
||
| 91 | |||
| 92 | $listener = $instance instanceof EventListener |
||
| 93 | ? $instance |
||
| 94 | : new CallableEventListener(fn(object $event) => $instance->{$method}($event)); |
||
| 95 | |||
| 96 | return [ |
||
| 97 | 'event' => $binding['event'], |
||
| 98 | 'listener' => $listener, |
||
| 99 | 'priority' => $binding['priority'], |
||
| 100 | ]; |
||
| 101 | }, $this->listenerBindings); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function className(): ?string |
||
| 107 | } |
||
| 108 | |||
| 109 | public function filePath(): string |
||
| 110 | { |
||
| 111 | return $this->filePath; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function __serialize(): array |
||
| 115 | { |
||
| 116 | return [ |
||
| 117 | 'className' => $this->className, |
||
| 118 | 'listenerBindings' => $this->listenerBindings, |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function __unserialize(array $data): void |
||
| 123 | { |
||
| 124 | $this->listenerBindings = $data['listenerBindings'] ?? []; |
||
| 125 | $this->className = $data['className'] ?? null; |
||
|
|
|||
| 126 | } |
||
| 127 | |||
| 128 | private function analyze(): void |
||
| 129 | { |
||
| 130 | $className = $this->getClassFromFile($this->filePath); |
||
| 131 | if (!$className || !class_exists($className)) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->className = $className; |
||
| 136 | $refClass = new ReflectionClass($className); |
||
| 137 | |||
| 138 | // Class-level attributes |
||
| 139 | $attributes = $refClass->getAttributes(AsEventListener::class, ReflectionAttribute::IS_INSTANCEOF); |
||
| 140 | foreach ($attributes as $attr) { |
||
| 141 | /** @var AsEventListener $instance */ |
||
| 142 | $instance = $attr->newInstance(); |
||
| 143 | $isInterface = $refClass->implementsInterface(EventListener::class); |
||
| 144 | $this->listenerBindings[] = [ |
||
| 145 | 'event' => $instance->event, |
||
| 146 | 'method' => $isInterface ? 'handle' : $instance->method ?? '__invoke', |
||
| 147 | 'priority' => $instance->priority ?? 0, |
||
| 148 | 'source' => $isInterface ? 'interface' : 'class' |
||
| 149 | ]; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Method-level attributes |
||
| 153 | foreach ($refClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 154 | foreach ($method->getAttributes(AsEventListener::class, ReflectionAttribute::IS_INSTANCEOF) as $attr) { |
||
| 155 | /** @var AsEventListener $instance */ |
||
| 156 | $instance = $attr->newInstance(); |
||
| 157 | $this->listenerBindings[] = [ |
||
| 158 | 'event' => $instance->event, |
||
| 159 | 'method' => $method->getName(), |
||
| 160 | 'priority' => $instance->priority ?? 0, |
||
| 161 | 'source' => 'method' |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | |||
| 165 | // EventListener interface |
||
| 166 | if (count($attributes) <= 0 && $refClass->implementsInterface(EventListener::class)) { |
||
| 167 | $this->listenerBindings[] = [ |
||
| 168 | 'event' => $className, |
||
| 169 | 'method' => 'handle', |
||
| 170 | 'priority' => 0, |
||
| 171 | 'source' => 'interface' |
||
| 172 | ]; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Retrieves the class name from a PHP file based on its namespace declaration. |
||
| 179 | * |
||
| 180 | * @param string $filePath The path to the PHP file |
||
| 181 | * @return string|null The fully qualified class name or null if class is not found |
||
| 182 | */ |
||
| 183 | private function getClassFromFile(string $filePath): ?string |
||
| 219 | } |
||
| 220 | } |
||
| 221 |