Conditions | 18 |
Paths | 92 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function process(ContainerBuilder $container) |
||
23 | { |
||
24 | $resolvers = $container->findTaggedServiceIds('doctrine.orm.entity_listener'); |
||
25 | |||
26 | $lazyServiceReferencesByResolver = []; |
||
27 | |||
28 | foreach ($resolvers as $id => $tagAttributes) { |
||
29 | foreach ($tagAttributes as $attributes) { |
||
30 | $name = isset($attributes['entity_manager']) ? $attributes['entity_manager'] : $container->getParameter('doctrine.default_entity_manager'); |
||
31 | $entityManager = sprintf('doctrine.orm.%s_entity_manager', $name); |
||
32 | |||
33 | if (! $container->hasDefinition($entityManager)) { |
||
34 | continue; |
||
35 | } |
||
36 | |||
37 | $resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name); |
||
38 | |||
39 | if (! $container->has($resolverId)) { |
||
40 | continue; |
||
41 | } |
||
42 | |||
43 | $resolver = $container->findDefinition($resolverId); |
||
44 | $resolver->setPublic(true); |
||
45 | |||
46 | if (isset($attributes['entity']) && isset($attributes['event'])) { |
||
47 | $this->attachToListener($container, $name, $id, $attributes); |
||
48 | } |
||
49 | |||
50 | $resolverClass = $this->getResolverClass($resolver, $container); |
||
51 | $resolverSupportsLazyListeners = is_a($resolverClass, EntityListenerServiceResolver::class, true); |
||
52 | |||
53 | $lazyByAttribute = isset($attributes['lazy']) && $attributes['lazy']; |
||
54 | if ($lazyByAttribute && ! $resolverSupportsLazyListeners) { |
||
55 | throw new InvalidArgumentException(sprintf( |
||
56 | 'Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', |
||
57 | EntityListenerServiceResolver::class |
||
58 | )); |
||
59 | } |
||
60 | |||
61 | if (! isset($attributes['lazy']) && $resolverSupportsLazyListeners || $lazyByAttribute) { |
||
62 | $listener = $container->findDefinition($id); |
||
63 | |||
64 | if ($listener->isAbstract()) { |
||
65 | throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as this entity listener is lazy-loaded.', $id)); |
||
66 | } |
||
67 | |||
68 | $resolver->addMethodCall('registerService', [$listener->getClass(), $id]); |
||
69 | |||
70 | // if the resolver uses the default class we will use a service locator for all listeners |
||
71 | if ($resolverClass === ContainerEntityListenerResolver::class) { |
||
72 | if (! isset($lazyServiceReferencesByResolver[$resolverId])) { |
||
73 | $lazyServiceReferencesByResolver[$resolverId] = []; |
||
74 | } |
||
75 | $lazyServiceReferencesByResolver[$resolverId][$id] = new Reference($id); |
||
76 | } else { |
||
77 | $listener->setPublic(true); |
||
78 | } |
||
79 | } else { |
||
80 | $resolver->addMethodCall('register', [new Reference($id)]); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | foreach ($lazyServiceReferencesByResolver as $resolverId => $listenerReferences) { |
||
86 | $container->findDefinition($resolverId)->setArgument(0, ServiceLocatorTagPass::register($container, $listenerReferences)); |
||
87 | } |
||
88 | } |
||
89 | |||
125 |