Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Parser |
||
20 | { |
||
21 | use \PHPDaemon\Traits\ClassWatchdog; |
||
22 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
23 | |||
24 | /** |
||
25 | * State: standby |
||
26 | */ |
||
27 | const T_ALL = 1; |
||
28 | /** |
||
29 | * State: comment |
||
30 | */ |
||
31 | const T_COMMENT = 2; |
||
32 | /** |
||
33 | * State: variable definition block |
||
34 | */ |
||
35 | const T_VAR = 3; |
||
36 | /** |
||
37 | * Single-quoted string |
||
38 | */ |
||
39 | const T_STRING = 4; |
||
40 | |||
41 | /** |
||
42 | * Double-quoted |
||
43 | */ |
||
44 | const T_STRING_DOUBLE = 5; |
||
45 | |||
46 | /** |
||
47 | * Block |
||
48 | */ |
||
49 | const T_BLOCK = 6; |
||
50 | |||
51 | /** |
||
52 | * Value defined by constant (keyword) or number |
||
53 | */ |
||
54 | const T_CVALUE = 7; |
||
55 | |||
56 | /** |
||
57 | * Config file path |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $file; |
||
61 | |||
62 | /** |
||
63 | * Current line number |
||
64 | * @var number |
||
65 | */ |
||
66 | protected $line = 1; |
||
67 | |||
68 | /** |
||
69 | * Current column number |
||
70 | * @var number |
||
71 | */ |
||
72 | protected $col = 1; |
||
73 | |||
74 | /** |
||
75 | * Pointer (current offset) |
||
76 | * @var integer |
||
77 | */ |
||
78 | protected $p = 0; |
||
79 | |||
80 | /** |
||
81 | * State stack |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $state = []; |
||
85 | |||
86 | /** |
||
87 | * Target object |
||
88 | * @var object |
||
89 | */ |
||
90 | protected $target; |
||
91 | |||
92 | /** |
||
93 | * Erroneous? |
||
94 | * @var boolean |
||
95 | */ |
||
96 | protected $erroneous = false; |
||
97 | |||
98 | /** |
||
99 | * Callbacks |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $tokens; |
||
103 | |||
104 | /** |
||
105 | * File length |
||
106 | * @var integer |
||
107 | */ |
||
108 | protected $length; |
||
109 | |||
110 | /** |
||
111 | * Revision |
||
112 | * @var integer |
||
113 | */ |
||
114 | protected $revision; |
||
115 | |||
116 | /** |
||
117 | * Contents of config file |
||
118 | * @var string |
||
119 | */ |
||
120 | protected $data; |
||
121 | |||
122 | /** |
||
123 | * Parse stack |
||
124 | * @var array |
||
125 | */ |
||
126 | protected static $stack = []; |
||
127 | |||
128 | /** |
||
129 | * Erroneous? |
||
130 | * @return boolean |
||
131 | */ |
||
132 | public function isErroneous() |
||
136 | |||
137 | /** |
||
138 | * Parse config file |
||
139 | * @param string File path |
||
140 | * @param object Target |
||
141 | * @param boolean Included? Default is false |
||
142 | * @return \PHPDaemon\Config\Parser |
||
143 | */ |
||
144 | public static function parse($file, $target, $included = false) |
||
155 | |||
156 | /** |
||
157 | * Constructor |
||
158 | * @return void |
||
|
|||
159 | */ |
||
160 | protected function __construct($file, $target, $included = false) |
||
453 | |||
454 | /** |
||
455 | * Removes old config parts after updating. |
||
456 | * @return void |
||
457 | */ |
||
458 | protected function purgeScope($scope) |
||
478 | |||
479 | /** |
||
480 | * Returns current variable scope |
||
481 | * @return object Scope. |
||
482 | */ |
||
483 | public function getCurrentScope() |
||
489 | |||
490 | /** |
||
491 | * Raises error message. |
||
492 | * @param string Message. |
||
493 | * @param string Level. |
||
494 | * @param string $msg |
||
495 | * @return void |
||
496 | */ |
||
497 | public function raiseError($msg, $level = 'emerg', $line = null, $col = null) |
||
511 | |||
512 | /** |
||
513 | * Executes token server. |
||
514 | * @param string $c |
||
515 | * @return mixed|void |
||
516 | */ |
||
517 | protected function token($token, $c) |
||
521 | |||
522 | /** |
||
523 | * Current character. |
||
524 | * @return string Character. |
||
525 | */ |
||
526 | protected function getCurrentChar() |
||
539 | |||
540 | /** |
||
541 | * Returns next character. |
||
542 | * @return string Character. |
||
543 | */ |
||
544 | protected function getNextChar() |
||
548 | |||
549 | /** |
||
550 | * Rewinds the pointer back. |
||
551 | * @param integer Number of characters to rewind back. |
||
552 | * @return void |
||
553 | */ |
||
554 | protected function rewind($n) |
||
558 | } |
||
559 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.