Total Complexity | 94 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FileLoader 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 FileLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | abstract class FileLoader extends BaseFileLoader |
||
38 | { |
||
39 | public const ANONYMOUS_ID_REGEXP = '/^\.\d+_[^~]*+~[._a-zA-Z\d]{7}$/'; |
||
40 | |||
41 | protected bool $isLoadingInstanceof = false; |
||
42 | protected array $instanceof = []; |
||
43 | protected array $interfaces = []; |
||
44 | protected array $singlyImplemented = []; |
||
45 | /** @var array<string, Alias> */ |
||
46 | protected array $aliases = []; |
||
47 | protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = true; |
||
48 | protected array $extensionConfigs = []; |
||
49 | protected int $importing = 0; |
||
50 | |||
51 | /** |
||
52 | * @param bool $prepend Whether to prepend extension config instead of appending them |
||
53 | */ |
||
54 | public function __construct( |
||
55 | protected ContainerBuilder $container, |
||
56 | FileLocatorInterface $locator, |
||
57 | ?string $env = null, |
||
58 | protected bool $prepend = false, |
||
59 | ) { |
||
60 | parent::__construct($locator, $env); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found |
||
65 | */ |
||
66 | public function import(mixed $resource, ?string $type = null, bool|string $ignoreErrors = false, ?string $sourceResource = null, $exclude = null): mixed |
||
67 | { |
||
68 | $args = \func_get_args(); |
||
69 | |||
70 | if ($ignoreNotFound = 'not_found' === $ignoreErrors) { |
||
71 | $args[2] = false; |
||
72 | } elseif (!\is_bool($ignoreErrors)) { |
||
73 | throw new \TypeError(\sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, get_debug_type($ignoreErrors))); |
||
74 | } |
||
75 | |||
76 | ++$this->importing; |
||
77 | try { |
||
78 | return parent::import(...$args); |
||
|
|||
79 | } catch (LoaderLoadException $e) { |
||
80 | if (!$ignoreNotFound || !($prev = $e->getPrevious()) instanceof FileLocatorFileNotFoundException) { |
||
81 | throw $e; |
||
82 | } |
||
83 | |||
84 | foreach ($prev->getTrace() as $frame) { |
||
85 | if ('import' === ($frame['function'] ?? null) && is_a($frame['class'] ?? '', Loader::class, true)) { |
||
86 | break; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if (__FILE__ !== $frame['file']) { |
||
91 | throw $e; |
||
92 | } |
||
93 | } finally { |
||
94 | --$this->importing; |
||
95 | $this->loadExtensionConfigs(); |
||
96 | } |
||
97 | |||
98 | return null; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Registers a set of classes as services using PSR-4 for discovery. |
||
103 | * |
||
104 | * @param Definition $prototype A definition to use as template |
||
105 | * @param string $namespace The namespace prefix of classes in the scanned directory |
||
106 | * @param string $resource The directory to look for classes, glob-patterns allowed |
||
107 | * @param string|string[]|null $exclude A globbed path of files to exclude or an array of globbed paths of files to exclude |
||
108 | * @param string|null $source The path to the file that defines the auto-discovery rule |
||
109 | */ |
||
110 | public function registerClasses(Definition $prototype, string $namespace, string $resource, string|array|null $exclude = null, ?string $source = null): void |
||
111 | { |
||
112 | if (!str_ends_with($namespace, '\\')) { |
||
113 | throw new InvalidArgumentException(\sprintf('Namespace prefix must end with a "\\": "%s".', $namespace)); |
||
114 | } |
||
115 | if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) { |
||
116 | throw new InvalidArgumentException(\sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace)); |
||
117 | } |
||
118 | // This can happen with YAML files |
||
119 | if (\is_array($exclude) && \in_array(null, $exclude, true)) { |
||
120 | throw new InvalidArgumentException('The exclude list must not contain a "null" value.'); |
||
121 | } |
||
122 | // This can happen with XML files |
||
123 | if (\is_array($exclude) && \in_array('', $exclude, true)) { |
||
124 | throw new InvalidArgumentException('The exclude list must not contain an empty value.'); |
||
125 | } |
||
126 | |||
127 | $autoconfigureAttributes = new RegisterAutoconfigureAttributesPass(); |
||
128 | $autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null; |
||
129 | $classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes, $source); |
||
130 | |||
131 | $getPrototype = static fn () => clone $prototype; |
||
132 | $serialized = serialize($prototype); |
||
133 | |||
134 | // avoid deep cloning if no definitions are nested |
||
135 | if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"', 55) |
||
136 | || strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"', 55) |
||
137 | ) { |
||
138 | // prepare for deep cloning |
||
139 | foreach (['Arguments', 'Properties', 'MethodCalls', 'Configurator', 'Factory', 'Bindings'] as $key) { |
||
140 | $serialized = serialize($prototype->{'get'.$key}()); |
||
141 | |||
142 | if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"') |
||
143 | || strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"') |
||
144 | ) { |
||
145 | $getPrototype = static fn () => $getPrototype()->{'set'.$key}(unserialize($serialized)); |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | unset($serialized); |
||
150 | |||
151 | foreach ($classes as $class => $errorMessage) { |
||
152 | if (null === $errorMessage && $autoconfigureAttributes) { |
||
153 | $r = $this->container->getReflectionClass($class); |
||
154 | if ($r->getAttributes(Exclude::class)[0] ?? null) { |
||
155 | $this->addContainerExcludedTag($class, $source); |
||
156 | continue; |
||
157 | } |
||
158 | if ($this->env) { |
||
159 | $excluded = true; |
||
160 | $whenAttributes = $r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
161 | $notWhenAttributes = $r->getAttributes(WhenNot::class, \ReflectionAttribute::IS_INSTANCEOF); |
||
162 | |||
163 | if ($whenAttributes && $notWhenAttributes) { |
||
164 | throw new LogicException(\sprintf('The "%s" class cannot have both #[When] and #[WhenNot] attributes.', $class)); |
||
165 | } |
||
166 | |||
167 | if (!$whenAttributes && !$notWhenAttributes) { |
||
168 | $excluded = false; |
||
169 | } |
||
170 | |||
171 | foreach ($whenAttributes as $attribute) { |
||
172 | if ($this->env === $attribute->newInstance()->env) { |
||
173 | $excluded = false; |
||
174 | break; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | foreach ($notWhenAttributes as $attribute) { |
||
179 | if ($excluded = $this->env === $attribute->newInstance()->env) { |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if ($excluded) { |
||
185 | $this->addContainerExcludedTag($class, $source); |
||
186 | continue; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if (interface_exists($class, false)) { |
||
192 | $this->interfaces[] = $class; |
||
193 | } else { |
||
194 | $this->setDefinition($class, $definition = $getPrototype()); |
||
195 | if (null !== $errorMessage) { |
||
196 | $definition->addError($errorMessage); |
||
197 | |||
198 | continue; |
||
199 | } |
||
200 | $definition->setClass($class); |
||
201 | |||
202 | $interfaces = []; |
||
203 | foreach (class_implements($class, false) as $interface) { |
||
204 | $this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class; |
||
205 | $interfaces[] = $interface; |
||
206 | } |
||
207 | |||
208 | if (!$autoconfigureAttributes) { |
||
209 | continue; |
||
210 | } |
||
211 | $r = $this->container->getReflectionClass($class); |
||
212 | $defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null; |
||
213 | foreach ($r->getAttributes(AsAlias::class) as $attr) { |
||
214 | /** @var AsAlias $attribute */ |
||
215 | $attribute = $attr->newInstance(); |
||
216 | $alias = $attribute->id ?? $defaultAlias; |
||
217 | $public = $attribute->public; |
||
218 | if (null === $alias) { |
||
219 | throw new LogicException(\sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class)); |
||
220 | } |
||
221 | if (isset($this->aliases[$alias])) { |
||
222 | throw new LogicException(\sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias])); |
||
223 | } |
||
224 | $this->aliases[$alias] = new Alias($class, $public); |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | foreach ($this->aliases as $alias => $aliasDefinition) { |
||
230 | $this->container->setAlias($alias, $aliasDefinition); |
||
231 | } |
||
232 | |||
233 | if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) { |
||
234 | $this->registerAliasesForSinglyImplementedInterfaces(); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | public function registerAliasesForSinglyImplementedInterfaces(): void |
||
239 | { |
||
240 | foreach ($this->interfaces as $interface) { |
||
241 | if (!empty($this->singlyImplemented[$interface]) && !isset($this->aliases[$interface]) && !$this->container->has($interface)) { |
||
242 | $this->container->setAlias($interface, $this->singlyImplemented[$interface]); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | $this->interfaces = $this->singlyImplemented = $this->aliases = []; |
||
247 | } |
||
248 | |||
249 | final protected function loadExtensionConfig(string $namespace, array $config): void |
||
250 | { |
||
251 | if (!$this->prepend) { |
||
252 | $this->container->loadFromExtension($namespace, $config); |
||
253 | |||
254 | return; |
||
255 | } |
||
256 | |||
257 | if ($this->importing) { |
||
258 | if (!isset($this->extensionConfigs[$namespace])) { |
||
259 | $this->extensionConfigs[$namespace] = []; |
||
260 | } |
||
261 | array_unshift($this->extensionConfigs[$namespace], $config); |
||
262 | |||
263 | return; |
||
264 | } |
||
265 | |||
266 | $this->container->prependExtensionConfig($namespace, $config); |
||
267 | } |
||
268 | |||
269 | final protected function loadExtensionConfigs(): void |
||
270 | { |
||
271 | if ($this->importing || !$this->extensionConfigs) { |
||
272 | return; |
||
273 | } |
||
274 | |||
275 | foreach ($this->extensionConfigs as $namespace => $configs) { |
||
276 | foreach ($configs as $config) { |
||
277 | $this->container->prependExtensionConfig($namespace, $config); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $this->extensionConfigs = []; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Registers a definition in the container with its instanceof-conditionals. |
||
286 | */ |
||
287 | protected function setDefinition(string $id, Definition $definition): void |
||
288 | { |
||
289 | $this->container->removeBindings($id); |
||
290 | |||
291 | foreach ($definition->getTag('container.error') as $error) { |
||
292 | if (isset($error['message'])) { |
||
293 | $definition->addError($error['message']); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | if ($this->isLoadingInstanceof) { |
||
298 | if (!$definition instanceof ChildDefinition) { |
||
299 | throw new InvalidArgumentException(\sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition))); |
||
300 | } |
||
301 | $this->instanceof[$id] = $definition; |
||
302 | } else { |
||
303 | $this->container->setDefinition($id, $definition->setInstanceofConditionals($this->instanceof)); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes, ?string $source): array |
||
308 | { |
||
309 | $parameterBag = $this->container->getParameterBag(); |
||
310 | |||
311 | $excludePaths = []; |
||
312 | $excludePrefix = null; |
||
313 | $excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns)); |
||
314 | foreach ($excludePatterns as $excludePattern) { |
||
315 | foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) { |
||
316 | $excludePrefix ??= $resource->getPrefix(); |
||
317 | |||
318 | // normalize Windows slashes and remove trailing slashes |
||
319 | $excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true; |
||
320 | } |
||
321 | } |
||
322 | |||
323 | $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern)); |
||
324 | $classes = []; |
||
325 | $prefixLen = null; |
||
326 | foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) { |
||
327 | if (null === $prefixLen) { |
||
328 | $prefixLen = \strlen($resource->getPrefix()); |
||
329 | |||
330 | if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) { |
||
331 | throw new InvalidArgumentException(\sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern)); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | if (isset($excludePaths[str_replace('\\', '/', $path)])) { |
||
336 | continue; |
||
337 | } |
||
338 | |||
339 | if (!str_ends_with($path, '.php')) { |
||
340 | continue; |
||
341 | } |
||
342 | $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -4)), '\\'); |
||
343 | |||
344 | if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) { |
||
345 | continue; |
||
346 | } |
||
347 | |||
348 | try { |
||
349 | $r = $this->container->getReflectionClass($class); |
||
350 | } catch (\ReflectionException $e) { |
||
351 | $classes[$class] = $e->getMessage(); |
||
352 | continue; |
||
353 | } |
||
354 | // check to make sure the expected class exists |
||
355 | if (!$r) { |
||
356 | throw new InvalidArgumentException(\sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern)); |
||
357 | } |
||
358 | |||
359 | if ($r->isInstantiable() || $r->isInterface()) { |
||
360 | $classes[$class] = null; |
||
361 | } |
||
362 | |||
363 | if ($autoconfigureAttributes && !$r->isInstantiable()) { |
||
364 | $autoconfigureAttributes->processClass($this->container, $r); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | // track only for new & removed files |
||
369 | if ($resource instanceof GlobResource) { |
||
370 | $this->container->addResource($resource); |
||
371 | } else { |
||
372 | foreach ($resource as $path) { |
||
373 | $this->container->fileExists($path, false); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | if (null !== $prefixLen) { |
||
378 | foreach ($excludePaths as $path => $_) { |
||
379 | $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\'); |
||
380 | $this->addContainerExcludedTag($class, $source); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | return $classes; |
||
385 | } |
||
386 | |||
387 | private function addContainerExcludedTag(string $class, ?string $source): void |
||
402 | } |
||
403 | } |
||
404 |