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()) |
||
176 | |||
177 | /** |
||
178 | * Add variable definition with array merging. |
||
179 | * |
||
180 | * @param string $name Variable name |
||
181 | * @param array $value Array of key-value items for merging it to other array |
||
182 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
183 | * @return self Chaining |
||
184 | */ |
||
185 | public function defArrayMerge($name, array $value, $arrayName = null) |
||
194 | |||
195 | /** |
||
196 | * Add variable definition. |
||
197 | * |
||
198 | * @param string $name Variable name |
||
199 | * @param string $value Variable default value |
||
200 | * @param string $after String to insert after variable definition |
||
201 | * @param string $end Closing part of variable definition |
||
202 | * @param string $quote Type of quote |
||
203 | * @return Generator Chaining |
||
204 | */ |
||
205 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
232 | |||
233 | /** |
||
234 | * Add class definition. |
||
235 | * |
||
236 | * @param string $name Class name |
||
237 | * @param string $extends Parent class name |
||
238 | * @param array $implements Interfaces names collection |
||
239 | * @return self Chaining |
||
240 | */ |
||
241 | public function defClass($name, $extends = null, array $implements = array()) |
||
271 | |||
272 | /** |
||
273 | * Close current class context. |
||
274 | * |
||
275 | * @return self Chaining |
||
276 | */ |
||
277 | public function endClass() |
||
286 | |||
287 | /** |
||
288 | * Define if statement condition. |
||
289 | * |
||
290 | * @param string $condition Condition statement |
||
291 | * @return self Chaining |
||
292 | */ |
||
293 | public function defIfCondition($condition) |
||
302 | |||
303 | /** |
||
304 | * Define elseif statement condition. |
||
305 | * |
||
306 | * @param string $condition Condition statement |
||
307 | * @return self Chaining |
||
308 | */ |
||
309 | public function defElseIfCondition($condition) |
||
317 | |||
318 | /** |
||
319 | * Define else statement. |
||
320 | * |
||
321 | * @return self Chaining |
||
322 | */ |
||
323 | public function defElseCondition() |
||
331 | |||
332 | /** |
||
333 | * Close if condition statement. |
||
334 | * |
||
335 | * @return self Chaining |
||
336 | */ |
||
337 | public function endIfCondition() |
||
346 | |||
347 | /** |
||
348 | * Add class variable definition. |
||
349 | * |
||
350 | * @param string $name Variable name |
||
351 | * @param string $visibility Variable accessibility level |
||
352 | * @param string $value Variable default value |
||
353 | * @return self Chaining |
||
354 | */ |
||
355 | public function defClassVar($name, $visibility = 'public', $value = null) |
||
363 | |||
364 | /** |
||
365 | * Add class constant definition. |
||
366 | * |
||
367 | * @param string $name Constant name |
||
368 | * @param string $value Variable default value |
||
369 | * @return self Chaining |
||
370 | */ |
||
371 | public function defClassConst($name, $value) |
||
375 | |||
376 | /** |
||
377 | * Write file to disk |
||
378 | * @param string $name Path to file |
||
379 | * @param string $format Output file format |
||
380 | */ |
||
381 | public function write($name, $format = 'php') |
||
391 | |||
392 | /** |
||
393 | * Flush internal data and return it. |
||
394 | * |
||
395 | * @return string Current generated code |
||
396 | */ |
||
397 | public function flush() |
||
408 | |||
409 | /** |
||
410 | * Add function definition. |
||
411 | * |
||
412 | * @param string $name Function name |
||
413 | * @param array $parameters Collection of parameters $typeHint => $paramName |
||
414 | * @return Generator Chaining |
||
415 | */ |
||
416 | public function defFunction($name, $parameters = array()) |
||
433 | |||
434 | /** |
||
435 | * Close current function context. |
||
436 | * |
||
437 | * @return self Chaining |
||
438 | */ |
||
439 | public function endFunction() |
||
445 | |||
446 | /** |
||
447 | * Constructor |
||
448 | * @param string $namespace Code namespace |
||
449 | */ |
||
450 | public function __construct($namespace = null) |
||
457 | |||
458 | /** |
||
459 | * Add namespace declaration |
||
460 | * @param string $name Namespace name |
||
461 | * @return self |
||
462 | */ |
||
463 | private function defnamespace($name) |
||
467 | } |
||
468 | //[PHPCOMPRESSOR(remove,end)] |
||
469 |
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: