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 | const START = 0x1; |
||
| 27 | const END = 0x2; |
||
| 28 | |||
| 29 | protected static $_id = 0; |
||
| 30 | |||
| 31 | public $pos; |
||
| 32 | public $name; |
||
| 33 | public $index = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Token|null|false |
||
| 37 | */ |
||
| 38 | protected $_end; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Token|null|false |
||
| 42 | */ |
||
| 43 | protected $_start; |
||
| 44 | |||
| 45 | /** @var Rule */ |
||
| 46 | protected $_rule; |
||
| 47 | |||
| 48 | protected $_valid; |
||
| 49 | protected $_length; |
||
| 50 | |||
| 51 | public $id; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Token constructor. |
||
| 55 | * |
||
| 56 | * @param array $options |
||
| 57 | */ |
||
| 58 | 51 | public function __construct(array $options) |
|
| 88 | |||
| 89 | 13 | public static function compare(Token $a, Token $b) |
|
| 109 | |||
| 110 | 34 | public function isStart() |
|
| 114 | |||
| 115 | 32 | public function isEnd() |
|
| 119 | |||
| 120 | 16 | public function isValid(Language $language, $context = null) |
|
| 128 | |||
| 129 | 11 | protected function validate(Language $language, $context) |
|
| 136 | |||
| 137 | 15 | public function setValid($valid = true) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | 28 | public function getStart() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param Token|null|false $start |
||
| 158 | */ |
||
| 159 | 3 | public function setStart($start = null) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return Token|null |
||
| 171 | */ |
||
| 172 | 39 | public function getEnd() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param Token|null|false $end |
||
| 179 | */ |
||
| 180 | 41 | public function setEnd($end = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return Rule |
||
| 193 | */ |
||
| 194 | 16 | public function getRule() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param Rule $rule |
||
| 201 | */ |
||
| 202 | 51 | public function setRule(Rule $rule) |
|
| 206 | |||
| 207 | 2 | public function getLength() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @codeCoverageIgnore |
||
| 218 | */ |
||
| 219 | public function dump($text = null) |
||
| 235 | } |