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 |
||
| 26 | class Token |
||
| 27 | { |
||
| 28 | const NAME = null; |
||
| 29 | const START = 0x1; |
||
| 30 | const END = 0x2; |
||
| 31 | |||
| 32 | protected static $_id = 0; |
||
| 33 | |||
| 34 | public $pos; |
||
| 35 | public $name; |
||
| 36 | public $closedBy; |
||
| 37 | public $index = 1; |
||
| 38 | public $id; |
||
| 39 | public $rule; |
||
| 40 | |||
| 41 | # region >>> cache |
||
| 42 | /** |
||
| 43 | * @var static|null|false |
||
| 44 | */ |
||
| 45 | protected $_end; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var static|null|false |
||
| 49 | */ |
||
| 50 | protected $_start; |
||
| 51 | |||
| 52 | protected $_valid; |
||
| 53 | protected $_length; |
||
| 54 | # endregion |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Token constructor. |
||
| 58 | * |
||
| 59 | * @param array $options |
||
| 60 | */ |
||
| 61 | 52 | public function __construct(array $options) |
|
| 92 | |||
| 93 | 35 | public function isStart() |
|
| 97 | |||
| 98 | 33 | public function isEnd() |
|
| 102 | |||
| 103 | 16 | public function isValid(Language $language, $context = null) |
|
| 111 | |||
| 112 | 11 | protected function validate(Language $language, $context) |
|
| 119 | |||
| 120 | 15 | public function setValid($valid = true) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return Token|null|false |
||
| 133 | */ |
||
| 134 | 29 | public function getStart() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param Token|null|false $start |
||
| 141 | */ |
||
| 142 | 13 | View Code Duplication | public function setStart($start = null) |
| 152 | |||
| 153 | /** |
||
| 154 | * @return Token|null|false |
||
| 155 | */ |
||
| 156 | 40 | public function getEnd() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param Token|null|false $end |
||
| 163 | */ |
||
| 164 | 42 | View Code Duplication | public function setEnd($end = null) |
| 174 | |||
| 175 | 2 | public function getLength() |
|
| 183 | |||
| 184 | 11 | public function __get($name) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param array $context |
||
| 191 | * @param Language $language |
||
| 192 | * @param Result $result |
||
| 193 | * @param TokenIterator $tokens |
||
| 194 | * |
||
| 195 | * todo: Documentation |
||
| 196 | * |
||
| 197 | * @return bool Continue? |
||
| 198 | */ |
||
| 199 | 10 | public function process(array &$context, Language $language, Result $result, TokenIterator $tokens) { |
|
| 229 | |||
| 230 | 13 | public static function compare(Token $a, Token $b) |
|
| 250 | } |
||
| 251 |