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 Token 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 Token, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Token |
||
| 24 | { |
||
| 25 | const NAME = null; |
||
| 26 | |||
| 27 | protected static $_id = 0; |
||
| 28 | |||
| 29 | public $pos; |
||
| 30 | public $name; |
||
| 31 | public $index = 1; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Token |
||
| 35 | */ |
||
| 36 | protected $_end; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Token |
||
| 40 | */ |
||
| 41 | protected $_start; |
||
| 42 | |||
| 43 | /** @var Rule */ |
||
| 44 | protected $_rule; |
||
| 45 | |||
| 46 | protected $_valid; |
||
| 47 | protected $_length; |
||
| 48 | |||
| 49 | public $id; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Token constructor. |
||
| 53 | */ |
||
| 54 | 51 | public function __construct(array $options) |
|
| 89 | |||
| 90 | 17 | public static function compare(Token $a, Token $b) |
|
| 114 | |||
| 115 | 36 | public function isStart() |
|
| 119 | |||
| 120 | 32 | public function isEnd() |
|
| 124 | |||
| 125 | 15 | public function isValid(Language $language, $context = null) |
|
| 133 | |||
| 134 | 11 | protected function validate(Language $language, $context) |
|
| 141 | |||
| 142 | 14 | public function setValid($valid = true) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | 15 | public function getStart() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param Token $start |
||
| 163 | */ |
||
| 164 | 39 | View Code Duplication | public function setStart(Token $start = null) |
| 173 | |||
| 174 | /** |
||
| 175 | * @return Token|null |
||
| 176 | */ |
||
| 177 | 38 | public function getEnd() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param Token $end |
||
| 184 | */ |
||
| 185 | 14 | View Code Duplication | public function setEnd(Token $end = null) |
| 195 | |||
| 196 | /** |
||
| 197 | * @return Rule |
||
| 198 | */ |
||
| 199 | 39 | public function getRule() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param Rule $rule |
||
| 206 | */ |
||
| 207 | 51 | public function setRule(Rule $rule) |
|
| 211 | |||
| 212 | 2 | public function getLength() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @codeCoverageIgnore |
||
| 223 | */ |
||
| 224 | public function dump($text = null) |
||
| 240 | |||
| 241 | 15 | public static function className() { |
|
| 244 | } |