Total Complexity | 81 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CodeGeneratorEventDispatcher 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 CodeGeneratorEventDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class CodeGeneratorEventDispatcher implements CodeGeneratorListenerInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var CodeGeneratorListenerInterface[] |
||
15 | */ |
||
16 | private $listeners; |
||
17 | |||
18 | /** |
||
19 | * @param CodeGeneratorListenerInterface[] $listeners |
||
20 | */ |
||
21 | public function __construct(array $listeners) |
||
22 | { |
||
23 | $this->listeners = $listeners; |
||
24 | } |
||
25 | |||
26 | public function onBaseBeanGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator |
||
27 | { |
||
28 | foreach ($this->listeners as $listener) { |
||
29 | $fileGenerator = $listener->onBaseBeanGenerated($fileGenerator, $beanDescriptor, $configuration); |
||
30 | if ($fileGenerator === null) { |
||
31 | break; |
||
32 | } |
||
33 | } |
||
34 | return $fileGenerator; |
||
35 | } |
||
36 | |||
37 | public function onBaseBeanConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
38 | { |
||
39 | foreach ($this->listeners as $listener) { |
||
40 | $methodGenerator = $listener->onBaseBeanConstructorGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
41 | if ($methodGenerator === null) { |
||
42 | break; |
||
43 | } |
||
44 | } |
||
45 | return $methodGenerator; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Called when a column is turned into a getter/setter. |
||
50 | * |
||
51 | * @return array<int, ?MethodGenerator> Returns an array of 2 methods to be generated for this property. You MUST return the getter (first argument) and setter (second argument) as part of these methods (if you want them to appear in the bean). Return null if you want to delete them. |
||
52 | */ |
||
53 | public function onBaseBeanPropertyGenerated(?MethodGenerator $getter, ?MethodGenerator $setter, AbstractBeanPropertyDescriptor $propertyDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array |
||
54 | { |
||
55 | foreach ($this->listeners as $listener) { |
||
56 | [$getter, $setter] = $listener->onBaseBeanPropertyGenerated($getter, $setter, $propertyDescriptor, $beanDescriptor, $configuration, $classGenerator); |
||
57 | if ($getter === null && $setter === null) { |
||
58 | break; |
||
59 | } |
||
60 | } |
||
61 | return [$getter, $setter]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Called when a foreign key from another table is turned into a "get many objects" method. |
||
66 | * |
||
67 | * @param MethodGenerator $getter |
||
68 | * @param DirectForeignKeyMethodDescriptor $directForeignKeyMethodDescriptor |
||
69 | * @param BeanDescriptor $beanDescriptor |
||
70 | * @param ConfigurationInterface $configuration |
||
71 | * @param ClassGenerator $classGenerator |
||
72 | * @return MethodGenerator|null |
||
73 | */ |
||
74 | public function onBaseBeanOneToManyGenerated(MethodGenerator $getter, DirectForeignKeyMethodDescriptor $directForeignKeyMethodDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
75 | { |
||
76 | foreach ($this->listeners as $listener) { |
||
77 | $getter = $listener->onBaseBeanOneToManyGenerated($getter, $directForeignKeyMethodDescriptor, $beanDescriptor, $configuration, $classGenerator); |
||
78 | if ($getter === null) { |
||
79 | break; |
||
80 | } |
||
81 | } |
||
82 | return $getter; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Called when a pivot table is turned into get/has/add/set/remove methods. |
||
87 | * |
||
88 | * @return array<int, ?MethodGenerator> Returns an array of methods to be generated for this property. You MUST return the get/has/add/set/remove methods as part of these methods (if you want them to appear in the bean). |
||
89 | */ |
||
90 | public function onBaseBeanManyToManyGenerated(?MethodGenerator $getter, ?MethodGenerator $adder, ?MethodGenerator $remover, ?MethodGenerator $hasser, ?MethodGenerator $setter, PivotTableMethodsDescriptor $pivotTableMethodsDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array |
||
91 | { |
||
92 | foreach ($this->listeners as $listener) { |
||
93 | [$getter, $adder, $remover, $hasser, $setter] = $listener->onBaseBeanManyToManyGenerated($getter, $adder, $remover, $hasser, $setter, $pivotTableMethodsDescriptor, $beanDescriptor, $configuration, $classGenerator); |
||
94 | if ($getter === null && $adder === null && $remover === null && $hasser === null && $setter === null) { |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | return [$getter, $adder, $remover, $hasser, $setter]; |
||
99 | } |
||
100 | |||
101 | public function onBaseBeanJsonSerializeGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
102 | { |
||
103 | foreach ($this->listeners as $listener) { |
||
104 | $methodGenerator = $listener->onBaseBeanJsonSerializeGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
105 | if ($methodGenerator === null) { |
||
106 | break; |
||
107 | } |
||
108 | } |
||
109 | return $methodGenerator; |
||
110 | } |
||
111 | |||
112 | public function onBaseBeanCloneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
113 | { |
||
114 | foreach ($this->listeners as $listener) { |
||
115 | $methodGenerator = $listener->onBaseBeanCloneGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
116 | if ($methodGenerator === null) { |
||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | return $methodGenerator; |
||
121 | } |
||
122 | |||
123 | public function onBaseDaoGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator |
||
124 | { |
||
125 | foreach ($this->listeners as $listener) { |
||
126 | $fileGenerator = $listener->onBaseDaoGenerated($fileGenerator, $beanDescriptor, $configuration); |
||
127 | if ($fileGenerator === null) { |
||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | return $fileGenerator; |
||
132 | } |
||
133 | |||
134 | public function onBaseDaoConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
135 | { |
||
136 | foreach ($this->listeners as $listener) { |
||
137 | $methodGenerator = $listener->onBaseDaoConstructorGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
138 | if ($methodGenerator === null) { |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | return $methodGenerator; |
||
143 | } |
||
144 | |||
145 | public function onBaseDaoSaveGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
146 | { |
||
147 | foreach ($this->listeners as $listener) { |
||
148 | $methodGenerator = $listener->onBaseDaoSaveGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
149 | if ($methodGenerator === null) { |
||
150 | break; |
||
151 | } |
||
152 | } |
||
153 | return $methodGenerator; |
||
154 | } |
||
155 | |||
156 | public function onBaseDaoFindAllGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
157 | { |
||
158 | foreach ($this->listeners as $listener) { |
||
159 | $methodGenerator = $listener->onBaseDaoFindAllGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
160 | if ($methodGenerator === null) { |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | return $methodGenerator; |
||
165 | } |
||
166 | |||
167 | public function onBaseDaoGetByIdGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
176 | } |
||
177 | |||
178 | public function onBaseDaoDeleteGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
179 | { |
||
180 | foreach ($this->listeners as $listener) { |
||
181 | $methodGenerator = $listener->onBaseDaoDeleteGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
182 | if ($methodGenerator === null) { |
||
183 | break; |
||
184 | } |
||
185 | } |
||
186 | return $methodGenerator; |
||
187 | } |
||
188 | |||
189 | public function onBaseDaoFindGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
190 | { |
||
191 | foreach ($this->listeners as $listener) { |
||
192 | $methodGenerator = $listener->onBaseDaoFindGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
193 | if ($methodGenerator === null) { |
||
194 | break; |
||
195 | } |
||
196 | } |
||
197 | return $methodGenerator; |
||
198 | } |
||
199 | |||
200 | public function onBaseDaoFindFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
201 | { |
||
202 | foreach ($this->listeners as $listener) { |
||
203 | $methodGenerator = $listener->onBaseDaoFindFromSqlGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
204 | if ($methodGenerator === null) { |
||
205 | break; |
||
206 | } |
||
207 | } |
||
208 | return $methodGenerator; |
||
209 | } |
||
210 | |||
211 | public function onBaseDaoFindFromRawSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
212 | { |
||
213 | foreach ($this->listeners as $listener) { |
||
214 | $methodGenerator = $listener->onBaseDaoFindFromRawSqlGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
215 | if ($methodGenerator === null) { |
||
216 | break; |
||
217 | } |
||
218 | } |
||
219 | return $methodGenerator; |
||
220 | } |
||
221 | |||
222 | public function onBaseDaoFindOneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
223 | { |
||
224 | foreach ($this->listeners as $listener) { |
||
225 | $methodGenerator = $listener->onBaseDaoFindOneGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
226 | if ($methodGenerator === null) { |
||
227 | break; |
||
228 | } |
||
229 | } |
||
230 | return $methodGenerator; |
||
231 | } |
||
232 | |||
233 | public function onBaseDaoFindOneFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
242 | } |
||
243 | |||
244 | public function onBaseDaoSetDefaultSortGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
245 | { |
||
246 | foreach ($this->listeners as $listener) { |
||
247 | $methodGenerator = $listener->onBaseDaoSetDefaultSortGenerated($methodGenerator, $beanDescriptor, $configuration, $classGenerator); |
||
248 | if ($methodGenerator === null) { |
||
249 | break; |
||
250 | } |
||
251 | } |
||
252 | return $methodGenerator; |
||
253 | } |
||
254 | |||
255 | public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator, Index $index, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
264 | } |
||
265 | |||
266 | public function onBaseResultIteratorGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator |
||
267 | { |
||
268 | foreach ($this->listeners as $listener) { |
||
269 | $fileGenerator = $listener->onBaseResultIteratorGenerated($fileGenerator, $beanDescriptor, $configuration); |
||
270 | if ($fileGenerator === null) { |
||
271 | break; |
||
272 | } |
||
273 | } |
||
274 | return $fileGenerator; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param BeanDescriptor[] $beanDescriptors |
||
279 | */ |
||
280 | public function onDaoFactoryGenerated(FileGenerator $fileGenerator, array $beanDescriptors, ConfigurationInterface $configuration): ?FileGenerator |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param BeanDescriptor[] $beanDescriptors |
||
293 | */ |
||
294 | public function onDaoFactoryConstructorGenerated(MethodGenerator $methodGenerator, array $beanDescriptors, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
295 | { |
||
296 | foreach ($this->listeners as $listener) { |
||
297 | $methodGenerator = $listener->onDaoFactoryConstructorGenerated($methodGenerator, $beanDescriptors, $configuration, $classGenerator); |
||
298 | if ($methodGenerator === null) { |
||
299 | break; |
||
303 | } |
||
304 | |||
305 | public function onDaoFactoryGetterGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
314 | } |
||
315 | |||
316 | public function onDaoFactorySetterGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
||
325 | } |
||
326 | } |
||
327 |