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 | /** |
||
23 | * Add simple text to current code position |
||
24 | * @param string $text Text to add |
||
25 | * @return self |
||
26 | */ |
||
27 | public function text($text = '') |
||
33 | |||
34 | /** |
||
35 | * Add current tabbing level to current line. |
||
36 | * |
||
37 | * @param string $endText Text to add after tabs |
||
38 | * @param integer $tabs Amount of tabs to add |
||
39 | * @param string $startText Text to add before tabs |
||
40 | * @return Generator Chaining |
||
41 | */ |
||
42 | public function tabs($endText = '', $tabs = null, $startText = '') |
||
52 | |||
53 | /** |
||
54 | * Add new line to code. |
||
55 | * |
||
56 | * @param string $text Code to add to new line |
||
57 | * @param integer $tabs Tabs count |
||
58 | * @return self |
||
59 | */ |
||
60 | public function newline($text = '', $tabs = null) |
||
69 | |||
70 | /** |
||
71 | * Add single line comment to code |
||
72 | * @param string $text Comment text |
||
73 | * @return self Chaining |
||
74 | */ |
||
75 | public function comment($text = '') |
||
79 | |||
80 | /** |
||
81 | * Add multi-line comment. If array with one line is passed |
||
82 | * we create special syntax comment in one line, usually |
||
83 | * used for class variable definition in more compact form. |
||
84 | * |
||
85 | * @param array $lines Array of comments lines |
||
86 | * @return self Chaining |
||
87 | */ |
||
88 | public function multicomment(array $lines = array()) |
||
111 | |||
112 | /** |
||
113 | * Add string value definition |
||
114 | * @param string $value String value to add |
||
115 | * @param string $tabs Tabs count |
||
116 | * @param string $quote Type of quote |
||
117 | * @return self |
||
118 | */ |
||
119 | public function stringvalue($value, $tabs = null, $quote = self::QUOTE_SINGLE) |
||
123 | |||
124 | /** |
||
125 | * Add array values definition |
||
126 | * @param array $items Array key-value pairs collection |
||
127 | * @return self Chaining |
||
128 | */ |
||
129 | public function arrayvalue(array $items = array()) |
||
152 | |||
153 | /** |
||
154 | * Add variable definition with array merging. |
||
155 | * |
||
156 | * @param string $name Variable name |
||
157 | * @param array $value Array of key-value items for merging it to other array |
||
158 | * @param string $arrayName Name of array to merge to, if no is specified - $name is used |
||
159 | * @return self Chaining |
||
160 | */ |
||
161 | public function defarraymerge($name, array $value, $arrayName = null) |
||
170 | |||
171 | /** |
||
172 | * Add variable definition. |
||
173 | * |
||
174 | * @param string $name Variable name |
||
175 | * @param string $value Variable default value |
||
176 | * @param string $after String to insert after variable definition |
||
177 | * @param string $end Closing part of variable definition |
||
178 | * @param string $quote Type of quote |
||
179 | * @return Generator Chaining |
||
180 | */ |
||
181 | public function defVar($name, $value = null, $after = ' = ', $end = ';', $quote = self::QUOTE_SINGLE) |
||
208 | |||
209 | /** |
||
210 | * Add class definition. |
||
211 | * |
||
212 | * @param string $name Class name |
||
213 | * @param string $extends Parent class name |
||
214 | * @param array $implements Interfaces names collection |
||
215 | * @return self Chaining |
||
216 | */ |
||
217 | public function defClass($name, $extends = null, array $implements = array()) |
||
247 | |||
248 | /** |
||
249 | * Close current class context. |
||
250 | * |
||
251 | * @return self Chaining |
||
252 | */ |
||
253 | public function endclass() |
||
264 | |||
265 | /** |
||
266 | * Add class variable definition. |
||
267 | * |
||
268 | * @param string $name Variable name |
||
269 | * @param string $visibility Variable accessibility level |
||
270 | * @param string|null $comment Variable description |
||
271 | * @param string $value Variable default value |
||
272 | * @return self Chaining |
||
273 | */ |
||
274 | public function defClassVar($name, $visibility = 'public', $comment = null, $value = null) |
||
282 | |||
283 | /** |
||
284 | * Add class constant definition. |
||
285 | * |
||
286 | * @param string $name Constant name |
||
287 | * @param string $value Variable default value |
||
288 | * @param string|null $comment Variable description |
||
289 | * @return self Chaining |
||
290 | */ |
||
291 | public function defClassConst($name, $value, $comment = null) |
||
295 | |||
296 | /** |
||
297 | * Write file to disk |
||
298 | * @param string $name Path to file |
||
299 | * @param string $format Output file format |
||
300 | */ |
||
301 | public function write($name, $format = 'php') |
||
311 | |||
312 | /** |
||
313 | * Flush internal data and return it. |
||
314 | * |
||
315 | * @return string Current generated code |
||
316 | */ |
||
317 | public function flush() |
||
328 | |||
329 | /** |
||
330 | * Add function definition |
||
331 | * @param string $name Function name |
||
332 | * @return self Chaining |
||
333 | */ |
||
334 | public function deffunction($name) |
||
340 | |||
341 | /** |
||
342 | * Close current function context |
||
343 | * @return\samson\activerecord\Generator |
||
344 | */ |
||
345 | public function endfunction() |
||
349 | |||
350 | /** |
||
351 | * Constructor |
||
352 | * @param string $namespace Code namespace |
||
353 | */ |
||
354 | public function __construct($namespace = null) |
||
361 | |||
362 | /** |
||
363 | * Add namespace declaration |
||
364 | * @param string $name Namespace name |
||
365 | * @return self |
||
366 | */ |
||
367 | private function defnamespace($name) |
||
371 | } |
||
372 | //[PHPCOMPRESSOR(remove,end)] |
||
373 |
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: