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 |
||
| 5 | class Token |
||
| 6 | { |
||
| 7 | public function __construct($code, $symbol) |
||
| 15 | |||
| 16 | public function add($code, $symbol) |
||
| 64 | |||
| 65 | public function getSymbol() |
||
| 69 | |||
| 70 | public function getType() |
||
| 74 | |||
| 75 | public function isUnknown() |
||
| 79 | |||
| 80 | public function isReady() |
||
| 84 | |||
| 85 | public function isTerminate() |
||
| 89 | |||
| 90 | public function isObjectOperator() |
||
| 94 | |||
| 95 | public function isStaticOperator() |
||
| 99 | |||
| 100 | public function isUseOperator() |
||
| 104 | |||
| 105 | public function isNamespaceOperator() |
||
| 109 | |||
| 110 | public function isExtendsOperator() |
||
| 114 | |||
| 115 | public function isImplementsOperator() |
||
| 119 | |||
| 120 | public function isNewOperator() |
||
| 124 | |||
| 125 | public function isVar() |
||
| 129 | |||
| 130 | public function isWhitespace() |
||
| 134 | |||
| 135 | public function hasWhitespace() |
||
| 139 | |||
| 140 | public function hasString() |
||
| 144 | |||
| 145 | public function isString() |
||
| 150 | |||
| 151 | public function isMethodCall() |
||
| 155 | |||
| 156 | public function isEmpty() |
||
| 160 | |||
| 161 | protected function resetType($type = 0) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param integer $type |
||
| 172 | */ |
||
| 173 | protected function removeType($type) |
||
| 179 | |||
| 180 | const T_UNKNOWN = 0; |
||
| 181 | const T_CONTINUE_PROCESS = 1; |
||
| 182 | const T_TERMINATE = 2; |
||
| 183 | const T_OBJECT_OPERATOR = 4; |
||
| 184 | const T_STATIC_OPERATOR = 8; |
||
| 185 | const T_USE_OPERATOR = 16; |
||
| 186 | const T_NAMESPACE_OPERATOR = 32; |
||
| 187 | const T_EXTENDS_OPERATOR = 64; |
||
| 188 | const T_IMPLEMENTS_OPERATOR = 128; |
||
| 189 | const T_NEW_OPERATOR = 256; |
||
| 190 | const T_VAR = 512; |
||
| 191 | const T_WHITESPACE = 1024; |
||
| 192 | const T_METHOD_CALL = 2048; |
||
| 193 | const T_STRING = 4096; |
||
| 194 | const T_EMPTY = 8192; |
||
| 195 | |||
| 196 | protected static $MAP = [ |
||
| 197 | T_VARIABLE => Token::T_VAR, |
||
| 198 | T_OBJECT_OPERATOR => Token::T_OBJECT_OPERATOR, |
||
| 199 | T_DOUBLE_COLON => Token::T_STATIC_OPERATOR, |
||
| 200 | T_USE => Token::T_USE_OPERATOR, |
||
| 201 | T_NAMESPACE => Token::T_NAMESPACE_OPERATOR, |
||
| 202 | T_NEW => Token::T_NEW_OPERATOR, |
||
| 203 | T_EXTENDS => Token::T_EXTENDS_OPERATOR, |
||
| 204 | T_IMPLEMENTS => Token::T_IMPLEMENTS_OPERATOR, |
||
| 205 | '$' => Token::T_VAR, |
||
| 206 | '(' => Token::T_METHOD_CALL |
||
| 207 | ]; |
||
| 208 | |||
| 209 | private $symbol = ""; |
||
| 210 | private $type = 0; |
||
| 211 | |||
| 212 | } |
||
| 213 |