Total Complexity | 57 |
Total Lines | 361 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Generator extends GeneratorAbstract |
||
8 | { |
||
9 | /** |
||
10 | * creates class for generator |
||
11 | * |
||
12 | * @param array $replacement |
||
13 | * @return Generator |
||
14 | * |
||
15 | * @throws FileNotFoundException |
||
16 | */ |
||
17 | public function createClass($replacement=array()) |
||
18 | { |
||
19 | if(file_exists($this->path)){ |
||
|
|||
20 | $content = $this->fileSystem->get($this->getStubFile()); |
||
21 | $this->loaded['createClass'] = true; |
||
22 | |||
23 | if($this->fileSystem->put($this->file,$content)!==FALSE){ |
||
24 | $this->replaceFileContent($replacement,$this->file); |
||
25 | } |
||
26 | } |
||
27 | |||
28 | return $this; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * creates class document for generator |
||
33 | * |
||
34 | * @param array $documents |
||
35 | * |
||
36 | * @throws FileNotFoundException |
||
37 | */ |
||
38 | public function createClassDocument($documents=array()) |
||
39 | { |
||
40 | if(isset($this->loaded['createClass']) && count($documents)){ |
||
41 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassDocument')); |
||
42 | eval("?>$content"); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * create class extend object for generator |
||
48 | * |
||
49 | * @param $namespace |
||
50 | * @param $alias |
||
51 | * |
||
52 | * @throws FileNotFoundException |
||
53 | */ |
||
54 | public function createClassExtend($namespace, $alias) |
||
55 | { |
||
56 | if(!is_null($namespace) && !is_null($alias) && !preg_match('@extends@',$this->getClassString())){ |
||
57 | if(preg_match('@class\s(.*?).*@',$this->getClassString(),$class)){ |
||
58 | $statements = explode(' ',$class[0]); |
||
59 | $className = $statements[1]; |
||
60 | |||
61 | if(count($statements)>2){ |
||
62 | $implements = implode(' ',array_slice($statements,2)); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if(isset($className)){ |
||
67 | $this->createClassUse([ |
||
68 | $namespace |
||
69 | ]); |
||
70 | |||
71 | if(!isset($implements)){ |
||
72 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtend')); |
||
73 | eval("?>$content"); |
||
74 | } |
||
75 | else{ |
||
76 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassExtendWithImplements')); |
||
77 | eval("?>$content"); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * create class interface object for generator |
||
85 | * |
||
86 | * @param array $implements |
||
87 | * |
||
88 | * @throws FileNotFoundException |
||
89 | */ |
||
90 | public function createClassImplements($implements=array()) |
||
91 | { |
||
92 | if(!is_null($this->getClassString())){ |
||
93 | |||
94 | $implementList = []; |
||
95 | $implementUseList = []; |
||
96 | |||
97 | foreach($implements as $namespace=>$alias) { |
||
98 | $implementUseList[] = $namespace; |
||
99 | $implementList[] = $alias; |
||
100 | } |
||
101 | |||
102 | $this->createClassUse($implementUseList); |
||
103 | |||
104 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassImplements')); |
||
105 | eval("?>$content"); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * creates class property for generator |
||
111 | * |
||
112 | * @param array $properties |
||
113 | * @param bool $loadedMethod |
||
114 | * |
||
115 | * @throws FileNotFoundException |
||
116 | */ |
||
117 | public function createClassProperty($properties=array(),$loadedMethod=false) |
||
118 | { |
||
119 | if(is_null($this->classProperties)){ |
||
120 | $this->classProperties = $properties; |
||
121 | } |
||
122 | |||
123 | if(isset($this->loaded['createMethod'])){ |
||
124 | $this->classProperties = $properties; |
||
125 | $loadedMethod = true; |
||
126 | } |
||
127 | |||
128 | if($loadedMethod){ |
||
129 | foreach ($this->classProperties as $property) { |
||
130 | if(!preg_match('@'.$this->regexEscape($property).'@',$this->fileSystem->get($this->file))){ |
||
131 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassProperty')); |
||
132 | eval("?>$content"); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | if(isset($this->loaded['createClassPropertyDocument'])){ |
||
137 | $this->createClassPropertyDocument($this->loaded['createClassPropertyDocument']); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * creates class property document for generator |
||
144 | * |
||
145 | * @param array $properties |
||
146 | * |
||
147 | * @throws FileNotFoundException |
||
148 | */ |
||
149 | public function createClassPropertyDocument($properties=array()) |
||
150 | { |
||
151 | $this->loaded['createClassPropertyDocument'] = $properties; |
||
152 | |||
153 | foreach ($properties as $property=>$documents){ |
||
154 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassPropertyDocument')); |
||
155 | eval("?>$content"); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * creates class trait for generator |
||
161 | * |
||
162 | * @param $trait |
||
163 | * |
||
164 | * @throws FileNotFoundException |
||
165 | */ |
||
166 | public function createClassTrait($trait) |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * creates class use statements for generator |
||
176 | * |
||
177 | * @param array $uses |
||
178 | * |
||
179 | * @throws FileNotFoundException |
||
180 | */ |
||
181 | public function createClassUse($uses=array()) |
||
182 | { |
||
183 | if(!is_null($this->getClassString()) && count($uses)){ |
||
184 | $content = '<?php'.$this->fileSystem->get($this->getEval('createClassUse')); |
||
185 | eval("?>$content"); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * creates method for generator |
||
191 | * |
||
192 | * @param array $methods |
||
193 | * |
||
194 | * @throws FileNotFoundException |
||
195 | */ |
||
196 | public function createMethod($methods=array()) |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * creates abstract method for generator |
||
209 | * |
||
210 | * @param array $methods |
||
211 | * |
||
212 | * @throws FileNotFoundException |
||
213 | */ |
||
214 | public function createMethodAbstract($methods=array()) |
||
215 | { |
||
216 | if(preg_match('@'.$this->type.'\s.*\n{@',$this->fileSystem->get($this->file),$parse) && count($methods)){ |
||
217 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAbstract')); |
||
218 | eval("?>$content"); |
||
219 | |||
220 | $this->createClassProperty([],true); |
||
221 | $this->loaded['createMethod'] = true; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * accessible properties method for generator |
||
227 | * |
||
228 | * @param array $methods |
||
229 | * @return void|mixed |
||
230 | * |
||
231 | * @throws FileNotFoundException |
||
232 | */ |
||
233 | public function createMethodAccessibleProperty($methods=array()) |
||
234 | { |
||
235 | foreach($methods as $method=>$accessibleValue){ |
||
236 | $this->accessibleProperties[$method] = $accessibleValue; |
||
237 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAccessibleProperty')); |
||
238 | eval("?>$content"); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * creates method body for generator |
||
244 | * |
||
245 | * @param array $methods |
||
246 | * |
||
247 | * @throws FileNotFoundException |
||
248 | */ |
||
249 | public function createMethodBody($methods=array()) |
||
250 | { |
||
251 | foreach ($methods as $method=>$body){ |
||
252 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodBody')); |
||
253 | eval("?>$content"); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * creates abstract method document for generator |
||
259 | * |
||
260 | * @param array $methods |
||
261 | * |
||
262 | * @throws FileNotFoundException |
||
263 | */ |
||
264 | public function createMethodAbstractDocument($methods=array()) |
||
265 | { |
||
266 | foreach ($methods as $method=>$documents){ |
||
267 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodAbstractDocument')); |
||
268 | eval("?>$content"); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * creates method for generator |
||
274 | * |
||
275 | * @param array $methods |
||
276 | * |
||
277 | * @throws FileNotFoundException |
||
278 | */ |
||
279 | public function createMethodDocument($methods=array()) |
||
280 | { |
||
281 | foreach ($methods as $method=>$documents){ |
||
282 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodDocument')); |
||
283 | eval("?>$content"); |
||
284 | } |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * creates interface method for generator |
||
289 | * |
||
290 | * @param array $methods |
||
291 | * |
||
292 | * @throws FileNotFoundException |
||
293 | */ |
||
294 | public function createMethodImplement($methods=array()) |
||
295 | { |
||
296 | if(preg_match('@'.$this->type.'\s.*\n{@',$this->fileSystem->get($this->file),$parse) && count($methods)){ |
||
297 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodImplement')); |
||
298 | eval("?>$content"); |
||
299 | |||
300 | $this->createClassProperty([],true); |
||
301 | $this->loaded['createMethod'] = true; |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * creates interface method document for generator |
||
307 | * |
||
308 | * @param array $methods |
||
309 | * |
||
310 | * @throws FileNotFoundException |
||
311 | */ |
||
312 | public function createMethodImplementDocument($methods=array()) |
||
317 | } |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * accessible properties method for generator |
||
322 | * |
||
323 | * @param array $methods |
||
324 | * @return void|mixed |
||
325 | * |
||
326 | * @throws FileNotFoundException |
||
327 | */ |
||
328 | public function createMethodParameters($methods=array()) |
||
329 | { |
||
330 | foreach($methods as $method=>$parameter) { |
||
331 | $this->methodParameters[$method] = $parameter; |
||
332 | $content = '<?php'.$this->fileSystem->get($this->getEval('createMethodParameters')); |
||
333 | eval("?>$content"); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * accessible properties abstract for generator |
||
339 | * |
||
340 | * @param array $methods |
||
341 | * @return void|mixed |
||
342 | * |
||
343 | * @throws FileNotFoundException |
||
344 | */ |
||
345 | public function createMethodAbstractParameters($methods=array()) |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * accessible properties interface for generator |
||
356 | * |
||
357 | * @param array $methods |
||
358 | * @return void|mixed |
||
359 | * |
||
360 | * @throws FileNotFoundException |
||
361 | */ |
||
362 | public function createMethodImplementParameters($methods=array()) |
||
372 |