Complex classes like ExtensionCompilerPass 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ExtensionCompilerPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ExtensionCompilerPass implements CompilerPassInterface |
||
27 | { |
||
28 | public function process(ContainerBuilder $container) |
||
29 | { |
||
30 | $universalExtensions = []; |
||
31 | $targets = []; |
||
32 | |||
33 | foreach ($container->findTaggedServiceIds('sonata.admin.extension') as $id => $tags) { |
||
34 | foreach ($tags as $attributes) { |
||
35 | $target = false; |
||
36 | |||
37 | if (isset($attributes['target'])) { |
||
38 | $target = $attributes['target']; |
||
39 | } |
||
40 | |||
41 | if (isset($attributes['global']) && $attributes['global']) { |
||
42 | $universalExtensions[$id] = $attributes; |
||
43 | } |
||
44 | |||
45 | if (!$target || !$container->hasDefinition($target)) { |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | $this->addExtension($targets, $target, $id, $attributes); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $extensionConfig = $container->getParameter('sonata.admin.extension.map'); |
||
54 | $extensionMap = $this->flattenExtensionConfiguration($extensionConfig); |
||
55 | |||
56 | foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) { |
||
57 | $admin = $container->getDefinition($id); |
||
58 | |||
59 | if (!isset($targets[$id])) { |
||
60 | $targets[$id] = new \SplPriorityQueue(); |
||
61 | } |
||
62 | |||
63 | foreach ($universalExtensions as $extension => $extensionAttributes) { |
||
64 | $this->addExtension($targets, $id, $extension, $extensionAttributes); |
||
65 | } |
||
66 | |||
67 | $extensions = $this->getExtensionsForAdmin($id, $admin, $container, $extensionMap); |
||
68 | |||
69 | foreach ($extensions as $extension => $attributes) { |
||
70 | if (!$container->has($extension)) { |
||
71 | throw new \InvalidArgumentException( |
||
72 | sprintf('Unable to find extension service for id %s', $extension) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | $this->addExtension($targets, $id, $extension, $attributes); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | foreach ($targets as $target => $extensions) { |
||
81 | $extensions = iterator_to_array($extensions); |
||
82 | krsort($extensions); |
||
83 | $admin = $container->getDefinition($target); |
||
84 | |||
85 | foreach (array_values($extensions) as $extension) { |
||
86 | $admin->addMethodCall('addExtension', [$extension]); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $id |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | protected function getExtensionsForAdmin($id, Definition $admin, ContainerBuilder $container, array $extensionMap) |
||
97 | { |
||
98 | $extensions = []; |
||
99 | |||
100 | $excludes = $extensionMap['excludes']; |
||
101 | unset($extensionMap['excludes']); |
||
102 | |||
103 | foreach ($extensionMap as $type => $subjects) { |
||
104 | foreach ($subjects as $subject => $extensionList) { |
||
105 | $class = null; |
||
106 | |||
107 | if ('admins' === $type) { |
||
108 | if ($id === $subject) { |
||
109 | $extensions = array_merge($extensions, $extensionList); |
||
110 | } |
||
111 | } else { |
||
112 | $class = $this->getManagedClass($admin, $container); |
||
113 | } |
||
114 | |||
115 | if (null === $class || !class_exists($class)) { |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | if ($this->isExtensionClass($type, $class, $subject)) { |
||
120 | $extensions = array_merge($extensions, $extensionList); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | if (isset($excludes[$id])) { |
||
126 | $extensions = array_diff_key($extensions, $excludes[$id]); |
||
127 | } |
||
128 | |||
129 | return $extensions; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Resolves the class argument of the admin to an actual class (in case of %parameter%). |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | protected function getManagedClass(Definition $admin, ContainerBuilder $container) |
||
138 | { |
||
139 | $argument = $admin->getArgument(1); |
||
140 | $class = $container->getParameterBag()->resolveValue($argument); |
||
141 | |||
142 | if (null === $class) { |
||
143 | throw new \DomainException(sprintf('The admin "%s" does not have a valid manager.', $admin->getClass())); |
||
144 | } |
||
145 | |||
146 | if (!\is_string($class)) { |
||
147 | throw new \TypeError(sprintf( |
||
|
|||
148 | 'Argument "%s" for admin class "%s" must be of type string, %s given.', |
||
149 | $argument, |
||
150 | $admin->getClass(), |
||
151 | \is_object($class) ? \get_class($class) : \gettype($class) |
||
152 | )); |
||
153 | } |
||
154 | |||
155 | return $class; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array an array with the following structure. |
||
160 | * |
||
161 | * [ |
||
162 | * 'excludes' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]], |
||
163 | * 'admins' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]], |
||
164 | * 'implements' => ['<interface>' => ['<extension_id>' => ['priority' => <int>]]], |
||
165 | * 'extends' => ['<class>' => ['<extension_id>' => ['priority' => <int>]]], |
||
166 | * 'instanceof' => ['<class>' => ['<extension_id>' => ['priority' => <int>]]], |
||
167 | * 'uses' => ['<trait>' => ['<extension_id>' => ['priority' => <int>]]], |
||
168 | * ] |
||
169 | */ |
||
170 | protected function flattenExtensionConfiguration(array $config) |
||
196 | |||
197 | /** |
||
198 | * @return bool |
||
199 | */ |
||
200 | protected function hasTrait(\ReflectionClass $class, $traitName) |
||
212 | |||
213 | private function isExtensionClass(string $type, string $class, string $subject): bool |
||
214 | { |
||
215 | $classReflection = new \ReflectionClass($class); |
||
231 | |||
232 | /** |
||
233 | * Add extension configuration to the targets array. |
||
234 | */ |
||
235 | private function addExtension( |
||
248 | } |
||
249 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.