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 | * @param array $options  | 
            ||
| 55 | */  | 
            ||
| 56 | 49 | public function __construct(array $options)  | 
            |
| 86 | |||
| 87 | 13 | public static function compare(Token $a, Token $b)  | 
            |
| 107 | |||
| 108 | 33 | public function isStart()  | 
            |
| 112 | |||
| 113 | 31 | public function isEnd()  | 
            |
| 117 | |||
| 118 | 15 | public function isValid(Language $language, $context = null)  | 
            |
| 126 | |||
| 127 | 11 | protected function validate(Language $language, $context)  | 
            |
| 134 | |||
| 135 | 14 | public function setValid($valid = true)  | 
            |
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * @return mixed  | 
            ||
| 148 | */  | 
            ||
| 149 | 27 | public function getStart()  | 
            |
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * @param Token|null|false $start  | 
            ||
| 156 | */  | 
            ||
| 157 | 3 | public function setStart($start = null)  | 
            |
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * @return Token|null  | 
            ||
| 169 | */  | 
            ||
| 170 | 37 | public function getEnd()  | 
            |
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * @param Token|null|false $end  | 
            ||
| 177 | */  | 
            ||
| 178 | 39 | public function setEnd($end = null)  | 
            |
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * @return Rule  | 
            ||
| 191 | */  | 
            ||
| 192 | 15 | public function getRule()  | 
            |
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @param Rule $rule  | 
            ||
| 199 | */  | 
            ||
| 200 | 49 | public function setRule(Rule $rule)  | 
            |
| 204 | |||
| 205 | 2 | public function getLength()  | 
            |
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * @codeCoverageIgnore  | 
            ||
| 216 | */  | 
            ||
| 217 | public function dump($text = null)  | 
            ||
| 233 | }  | 
            
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.