Complex classes like PhpFileParser 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 PhpFileParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class PhpFileParser implements \Iterator, \ArrayAccess { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var string original file name |
||
| 12 | */ |
||
| 13 | private $fileName = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $tokens = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $sha1hash = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param $fileName |
||
| 27 | * @throws \CodeReview\IOException |
||
| 28 | * @throws Exception |
||
| 29 | */ |
||
| 30 | 13 | public function __construct($fileName) { |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Return fileds to serialize. |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 6 | public function __sleep() { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Verify class contents against original file to detect changes. |
||
| 62 | */ |
||
| 63 | 6 | public function __wakeup() { |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Uses SHA1 hash to determine if file contents has changed since analysis. |
||
| 69 | * |
||
| 70 | * @return bool |
||
| 71 | * @throws \CodeReview\IOException |
||
| 72 | * @throws LogicException |
||
| 73 | */ |
||
| 74 | 6 | protected function validateFileContents() { |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Checks if file exists and is readable. |
||
| 90 | * |
||
| 91 | * @param $fileName |
||
| 92 | * @return bool |
||
| 93 | * @throws \CodeReview\IOException |
||
| 94 | */ |
||
| 95 | 13 | protected function validateFilePath($fileName) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Compute parents of the tokens to easily determine containing methods and classes. |
||
| 110 | * |
||
| 111 | * @param bool $debug |
||
| 112 | */ |
||
| 113 | 11 | private function computeNestingParentTokens($debug = false) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param array $tokens |
||
| 154 | * @param int $offset |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 11 | public function isEqualToAnyToken($tokens, $offset = null) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param $token string|int individual token identifier or predefined T_* constant value for complex tokens |
||
| 168 | * @param int $offset optional offset when checking other than current |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 11 | public function isEqualToToken($token, $offset = null) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param int $offset optional offset when checking other than current |
||
| 188 | * @return mixed |
||
| 189 | */ |
||
| 190 | public function getDefiningFunctionName($offset = null) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param int $offset optional offset when checking other than current |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function getDefiningClassName($offset = null) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $fileName |
||
| 229 | * @return bool|string |
||
| 230 | * @throws \CodeReview\IOException |
||
| 231 | */ |
||
| 232 | 1 | public function exportPhp($fileName = null) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * (PHP 5 >= 5.0.0)<br/> |
||
| 256 | * Return the current element |
||
| 257 | * |
||
| 258 | * @link http://php.net/manual/en/iterator.current.php |
||
| 259 | * @return mixed Can return any type. |
||
| 260 | */ |
||
| 261 | 4 | public function current() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * (PHP 5 >= 5.0.0)<br/> |
||
| 267 | * Move forward to next element |
||
| 268 | * @link http://php.net/manual/en/iterator.next.php |
||
| 269 | * @return void Any returned value is ignored. |
||
| 270 | */ |
||
| 271 | 4 | public function next() { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * (PHP 5 >= 5.0.0)<br/> |
||
| 277 | * Return the key of the current element |
||
| 278 | * @link http://php.net/manual/en/iterator.key.php |
||
| 279 | * @return mixed scalar on success, or null on failure. |
||
| 280 | */ |
||
| 281 | 4 | public function key() { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * (PHP 5 >= 5.0.0)<br/> |
||
| 287 | * Checks if current position is valid |
||
| 288 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 289 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
| 290 | * Returns true on success or false on failure. |
||
| 291 | */ |
||
| 292 | 4 | public function valid() { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * (PHP 5 >= 5.0.0)<br/> |
||
| 300 | * Rewind the Iterator to the first element |
||
| 301 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 302 | * @return void Any returned value is ignored. |
||
| 303 | */ |
||
| 304 | 4 | public function rewind() { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * (PHP 5 >= 5.0.0)<br/> |
||
| 310 | * Whether a offset exists |
||
| 311 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 312 | * @param mixed $offset <p> |
||
| 313 | * An offset to check for. |
||
| 314 | * </p> |
||
| 315 | * @return boolean true on success or false on failure. |
||
| 316 | * </p> |
||
| 317 | * <p> |
||
| 318 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 319 | */ |
||
| 320 | 11 | public function offsetExists($offset) { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * (PHP 5 >= 5.0.0)<br/> |
||
| 326 | * Offset to retrieve |
||
| 327 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 328 | * @param mixed $offset <p> |
||
| 329 | * The offset to retrieve. |
||
| 330 | * </p> |
||
| 331 | * @return mixed Can return all value types. |
||
| 332 | */ |
||
| 333 | 11 | public function offsetGet($offset) { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * (PHP 5 >= 5.0.0)<br/> |
||
| 339 | * Offset to set |
||
| 340 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 341 | * @param mixed $offset <p> |
||
| 342 | * The offset to assign the value to. |
||
| 343 | * </p> |
||
| 344 | * @param mixed $value <p> |
||
| 345 | * The value to set. |
||
| 346 | * </p> |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function offsetSet($offset, $value) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * (PHP 5 >= 5.0.0)<br/> |
||
| 355 | * Offset to unset |
||
| 356 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 357 | * @param mixed $offset <p> |
||
| 358 | * The offset to unset. |
||
| 359 | * </p> |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function offsetUnset($offset) { |
||
| 365 | } |