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. 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Generator |
||
6 | { |
||
7 | /** Single quote for string value **/ |
||
8 | const QUOTE_SINGLE = "'"; |
||
9 | |||
10 | /** Double quote for string value **/ |
||
11 | const QUOTE_DOUBLE = '"'; |
||
12 | |||
13 | /** @var string Generated code */ |
||
14 | public $code = ''; |
||
15 | |||
16 | /** @var integer Level of code line tabbing for new lines */ |
||
17 | public $tabs = 0; |
||
18 | |||
19 | /** @var string Current class name */ |
||
20 | public $class; |
||
21 | |||
22 | /** @var int Current conditions nesting level */ |
||
23 | public $ifConditionLevel = 0; |
||
24 | |||
25 | /** |
||
26 | * Add simple text to current code position |
||
27 | * @param string $text Text to add |
||
28 | * @return self |
||
29 | */ |
||
30 | public function text($text = '') |
||
36 | |||
37 | /** |
||
38 | * Add current tabbing level to current line. |
||
39 | * |
||
40 | * @param string $endText Text to add after tabs |
||
41 | * @param integer $tabs Amount of tabs to add |
||
42 | * @param string $startText Text to add before tabs |
||
43 | * @return Generator Chaining |
||
44 | */ |
||
45 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
55 | |||
56 | /** |
||
57 | * Add new line to code. |
||
58 | * |
||
59 | * @param string $text Code to add to new line |
||
60 | * @param integer $tabs Tabs count |
||
61 | * @return self |
||
62 | */ |
||
63 | public function newLine($text = '', $tabs = null) |
||
72 | |||
73 | /** |
||
74 | * Add single line comment to code |
||
75 | * @param string $text Comment text |
||
76 | * @return self Chaining |
||
77 | */ |
||
78 | public function comment($text = '') |
||
82 | |||
83 | /** |
||
84 | * Add multi-line comment. If array with one line is passed |
||
85 | * we create special syntax comment in one line, usually |
||
86 | * used for class variable definition in more compact form. |
||
87 | * |
||
88 | * @param array $lines Array of comments lines |
||
89 | * @return self Chaining |
||
90 | */ |
||
91 | public function multiComment(array $lines = array()) |
||
114 | |||
115 | /** |
||
116 | * Add one line variable definition comment. |
||
117 | * |
||
118 | * @param string $type Variable type |
||
119 | * @param string $description Variable description |
||
120 | * @param string $name Variable name |
||
121 | * @return self Chaining |
||
122 | */ |
||
123 | public function commentVar($type, $description, $name = '') |
||
129 | |||
130 | /** |
||
131 | * Add string value definition. |
||
132 | * |
||
133 | * @param string $value String value to add |
||
134 | * @param string $tabs Tabs count |
||
135 | * @param string $quote Type of quote |
||
136 | * @return self Chaining |
||
137 | */ |
||
138 | public function stringValue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
142 | |||
143 | /** |
||
144 | * Add array values definition. |
||
145 | * |
||
146 | * @param array $items Array key-value pairs collection |
||
147 | * @return self Chaining |
||
148 | */ |
||
149 | public function arrayValue(array $items = array()) |
||
172 | |||
173 | /** |
||
174 | * Add variable definition with array merging. |
||
175 | * |
||
176 | * @param string $name Variable name |
||
177 | * @param array $value Array of key-value items for merging it to other array |
||
178 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
179 | * @return self Chaining |
||
180 | */ |
||
181 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
190 | |||
191 | /** |
||
192 | * Add variable definition. |
||
193 | * |
||
194 | * @param string $name Variable name |
||
195 | * @param string $value Variable default value |
||
196 | * @param string $after String to insert after variable definition |
||
197 | * @param string $end Closing part of variable definition |
||
198 | * @param string $quote Type of quote |
||
199 | * @return Generator Chaining |
||
200 | */ |
||
201 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
228 | |||
229 | /** |
||
230 | * Add class definition. |
||
231 | * |
||
232 | * @param string $name Class name |
||
233 | * @param string $extends Parent class name |
||
234 | * @param array $implements Interfaces names collection |
||
235 | * @return self Chaining |
||
236 | */ |
||
237 | public function defClass($name, $extends = null, array $implements = array()) |
||
267 | |||
268 | /** |
||
269 | * Close current class context. |
||
270 | * |
||
271 | * @return self Chaining |
||
272 | */ |
||
273 | public function endClass() |
||
282 | |||
283 | /** |
||
284 | * Define if statement condition. |
||
285 | * |
||
286 | * @param string $condition Condition statement |
||
287 | * @return self Chaining |
||
288 | */ |
||
289 | public function defIfCondition($condition) |
||
298 | |||
299 | /** |
||
300 | * Define elseif statement condition. |
||
301 | * |
||
302 | * @param string $condition Condition statement |
||
303 | * @return self Chaining |
||
304 | */ |
||
305 | public function defElseIfCondition($condition) |
||
313 | |||
314 | /** |
||
315 | * Define else statement. |
||
316 | * |
||
317 | * @return self Chaining |
||
318 | */ |
||
319 | public function defElseCondition() |
||
327 | |||
328 | /** |
||
329 | * Close if condition statement. |
||
330 | * |
||
331 | * @return self Chaining |
||
332 | */ |
||
333 | public function endIfCondition() |
||
342 | |||
343 | /** |
||
344 | * Add class variable definition. |
||
345 | * |
||
346 | * @param string $name Variable name |
||
347 | * @param string $visibility Variable accessibility level |
||
348 | * @param string $value Variable default value |
||
349 | * @return self Chaining |
||
350 | */ |
||
351 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
359 | |||
360 | /** |
||
361 | * Add class constant definition. |
||
362 | * |
||
363 | * @param string $name Constant name |
||
364 | * @param string $value Variable default value |
||
365 | * @return self Chaining |
||
366 | */ |
||
367 | public function defClassConst($name, $value) |
||
371 | |||
372 | /** |
||
373 | * Write file to disk |
||
374 | * @param string $name Path to file |
||
375 | * @param string $format Output file format |
||
376 | */ |
||
377 | public function write($name, $format = 'php') |
||
387 | |||
388 | /** |
||
389 | * Flush internal data and return it. |
||
390 | * |
||
391 | * @return string Current generated code |
||
392 | */ |
||
393 | public function flush() |
||
404 | |||
405 | /** |
||
406 | * Add function definition. |
||
407 | * |
||
408 | * @param string $name Function name |
||
409 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
410 | * @return Generator Chaining |
||
411 | */ |
||
412 | public function defFunction($name, $parameters = array()) |
||
413 | { |
||
414 | // Convert parameters to string |
||
415 | $parameterList = array(); |
||
416 | foreach ($parameters as $type => $parameter) { |
||
417 | $parameterList[] = (is_string($type) ? $type.' ' : '') . $parameter; |
||
418 | } |
||
419 | $parameterList = sizeof($parameterList) ? implode(', ', $parameterList) : ''; |
||
420 | |||
421 | $this->newLine('function ' . $name . '('.$parameterList.')') |
||
422 | ->newLine('{') |
||
423 | ->tabs(''); |
||
424 | |||
425 | $this->tabs++; |
||
426 | |||
427 | return $this; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Close current function context. |
||
432 | * |
||
433 | * @return self Chaining |
||
434 | */ |
||
435 | public function endFunction() |
||
441 | |||
442 | /** |
||
443 | * Constructor |
||
444 | * @param string $namespace Code namespace |
||
445 | */ |
||
446 | public function __construct($namespace = null) |
||
453 | |||
454 | /** |
||
455 | * Add namespace declaration |
||
456 | * @param string $name Namespace name |
||
457 | * @return self |
||
458 | */ |
||
459 | private function defnamespace($name) |
||
463 | } |
||
464 | //[PHPCOMPRESSOR(remove,end)] |
||
465 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: