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 |
||
| 24 | class Token |
||
| 25 | { |
||
| 26 | const NAME = null; |
||
| 27 | const START = 0x1; |
||
| 28 | const END = 0x2; |
||
| 29 | |||
| 30 | protected static $_id = 0; |
||
| 31 | |||
| 32 | public $pos; |
||
| 33 | public $name; |
||
| 34 | public $closedBy; |
||
| 35 | public $index = 1; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Token|null|false |
||
| 39 | */ |
||
| 40 | protected $_end; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Token|null|false |
||
| 44 | */ |
||
| 45 | protected $_start; |
||
| 46 | |||
| 47 | /** @var Rule */ |
||
| 48 | protected $_rule; |
||
| 49 | |||
| 50 | protected $_valid; |
||
| 51 | protected $_length; |
||
| 52 | |||
| 53 | public $id; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Token constructor. |
||
| 57 | * |
||
| 58 | * @param array $options |
||
| 59 | */ |
||
| 60 | 52 | public function __construct(array $options) |
|
| 96 | |||
| 97 | 13 | public static function compare(Token $a, Token $b) |
|
| 117 | |||
| 118 | 35 | public function isStart() |
|
| 122 | |||
| 123 | 33 | public function isEnd() |
|
| 127 | |||
| 128 | 16 | public function isValid(Language $language, $context = null) |
|
| 136 | |||
| 137 | 11 | protected function validate(Language $language, $context) |
|
| 144 | |||
| 145 | 15 | public function setValid($valid = true) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | 29 | public function getStart() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param Token|null|false $start |
||
| 166 | */ |
||
| 167 | 3 | View Code Duplication | public function setStart($start = null) |
| 177 | |||
| 178 | /** |
||
| 179 | * @return Token|null |
||
| 180 | */ |
||
| 181 | 40 | public function getEnd() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param Token|null|false $end |
||
| 188 | */ |
||
| 189 | 42 | View Code Duplication | public function setEnd($end = null) |
| 199 | |||
| 200 | /** |
||
| 201 | * @return Rule |
||
| 202 | */ |
||
| 203 | 16 | public function getRule() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param Rule $rule |
||
| 210 | */ |
||
| 211 | 52 | public function setRule(Rule $rule) |
|
| 215 | |||
| 216 | 2 | public function getLength() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @codeCoverageIgnore |
||
| 227 | */ |
||
| 228 | public function dump($text = null) |
||
| 244 | |||
| 245 | 11 | public function __get($name) |
|
| 249 | } |
||
| 250 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.