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 | 28 | public function __construct(array $options) |
|
| 87 | |||
| 88 | 1 | public static function compare(Token $a, Token $b) |
|
| 89 | { |
||
| 90 | 1 | if ($a->pos === $b->pos) { |
|
| 91 | $multiplier = $a->isEnd() ? -1 : 1; |
||
| 92 | |||
| 93 | if (($a->isStart() && $b->isEnd()) || ($a->isEnd() && $b->isStart())) { |
||
| 94 | return $multiplier; |
||
| 95 | } elseif (($rule = Helper::cmp($b->_rule->priority, $a->_rule->priority)) !== 0) { |
||
| 96 | return $multiplier*$rule; |
||
| 97 | } elseif (($rule = Helper::cmp($b->index, $a->index)) !== 0) { |
||
| 98 | return $multiplier*$rule; |
||
| 99 | } else { |
||
| 100 | return $multiplier*($a->id < $b->id ? -1 : 1); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | 1 | return ($a->pos > $b->pos) ? 1 : -1; |
|
| 105 | } |
||
| 106 | |||
| 107 | 16 | public function isStart() |
|
| 111 | |||
| 112 | 16 | public function isEnd() |
|
| 116 | |||
| 117 | public function isValid(Language $language, $context = null) |
||
| 125 | |||
| 126 | protected function validate(Language $language, $context) |
||
| 133 | |||
| 134 | public function setValid($valid = true) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | 3 | public function getStart() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param Token $start |
||
| 155 | */ |
||
| 156 | 17 | View Code Duplication | public function setStart(Token $start = null) |
| 165 | |||
| 166 | /** |
||
| 167 | * @return Token|null |
||
| 168 | */ |
||
| 169 | 17 | public function getEnd() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param Token $end |
||
| 176 | */ |
||
| 177 | 3 | View Code Duplication | public function setEnd(Token $end = null) |
| 187 | |||
| 188 | /** |
||
| 189 | * @return Rule |
||
| 190 | */ |
||
| 191 | 17 | public function getRule() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param Rule $rule |
||
| 198 | */ |
||
| 199 | 6 | public function setRule(Rule $rule) |
|
| 203 | |||
| 204 | 1 | public function getLength() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @codeCoverageIgnore |
||
| 215 | */ |
||
| 216 | public function dump($text = null) |
||
| 232 | } |