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 $index = 1; |
||
| 37 | public $id; |
||
| 38 | public $rule; |
||
| 39 | |||
| 40 | # region >>> cache |
||
| 41 | /** |
||
| 42 | * @var static|null|false |
||
| 43 | */ |
||
| 44 | protected $_end; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var static|null|false |
||
| 48 | */ |
||
| 49 | protected $_start; |
||
| 50 | |||
| 51 | protected $_valid; |
||
| 52 | protected $_length; |
||
| 53 | # endregion |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Token constructor. |
||
| 57 | * |
||
| 58 | * @param null $name |
||
| 59 | * @param array $options |
||
| 60 | */ |
||
| 61 | 48 | public function __construct($name = null, array $options = []) |
|
| 77 | |||
| 78 | 33 | public function isStart() |
|
| 82 | |||
| 83 | 31 | public function isEnd() |
|
| 87 | |||
| 88 | 14 | public function isValid(Context $context) |
|
| 96 | |||
| 97 | 11 | protected function validate(Context $context) |
|
| 104 | |||
| 105 | 14 | public function setValid($valid = true) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return Token|null|false |
||
| 118 | */ |
||
| 119 | 29 | public function getStart() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param Token|null|false $start |
||
| 126 | */ |
||
| 127 | 11 | View Code Duplication | public function setStart($start = null) |
| 137 | |||
| 138 | /** |
||
| 139 | * @return Token|null|false |
||
| 140 | */ |
||
| 141 | 38 | public function getEnd() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param Token|null|false $end |
||
| 148 | */ |
||
| 149 | 46 | View Code Duplication | public function setEnd($end = null) |
| 159 | |||
| 160 | 2 | public function getLength() |
|
| 168 | |||
| 169 | 11 | public function __get($name) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param Context $context |
||
| 176 | * @param Language $language |
||
| 177 | * @param Result $result |
||
| 178 | * @param TokenIterator $tokens |
||
| 179 | * |
||
| 180 | * todo: Documentation |
||
| 181 | * |
||
| 182 | * @return bool Return true to continue processing, false to return already processed tokens. |
||
| 183 | */ |
||
| 184 | 11 | public function process(Context $context, Language $language, Result $result, TokenIterator $tokens) { |
|
| 193 | |||
| 194 | 10 | protected function processStart(Context $context, Language $language, Result $result, TokenIterator $tokens) { |
|
| 200 | |||
| 201 | 9 | protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens) { |
|
| 218 | |||
| 219 | 13 | public static function compare(Token $a, Token $b) |
|
| 239 | } |
||
| 240 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.