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 Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Parser |
||
| 18 | { |
||
| 19 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 20 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * State: standby |
||
| 24 | */ |
||
| 25 | const T_ALL = 1; |
||
| 26 | /** |
||
| 27 | * State: comment |
||
| 28 | */ |
||
| 29 | const T_COMMENT = 2; |
||
| 30 | /** |
||
| 31 | * State: variable definition block |
||
| 32 | */ |
||
| 33 | const T_VAR = 3; |
||
| 34 | /** |
||
| 35 | * Single-quoted string |
||
| 36 | */ |
||
| 37 | const T_STRING = 4; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Double-quoted |
||
| 41 | */ |
||
| 42 | const T_STRING_DOUBLE = 5; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Block |
||
| 46 | */ |
||
| 47 | const T_BLOCK = 6; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Value defined by constant (keyword) or number |
||
| 51 | */ |
||
| 52 | const T_CVALUE = 7; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Config file path |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $file; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Current line number |
||
| 62 | * @var number |
||
| 63 | */ |
||
| 64 | protected $line = 1; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Current column number |
||
| 68 | * @var number |
||
| 69 | */ |
||
| 70 | protected $col = 1; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Pointer (current offset) |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | protected $p = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * State stack |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $state = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Target object |
||
| 86 | * @var object |
||
| 87 | */ |
||
| 88 | protected $target; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Erroneous? |
||
| 92 | * @var boolean |
||
| 93 | */ |
||
| 94 | protected $erroneous = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Callbacks |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $tokens; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * File length |
||
| 104 | * @var integer |
||
| 105 | */ |
||
| 106 | protected $length; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Revision |
||
| 110 | * @var integer |
||
| 111 | */ |
||
| 112 | protected $revision; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Contents of config file |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | protected $data; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Parse stack |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected static $stack = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Erroneous? |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | public function isErroneous() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Parse config file |
||
| 137 | * @param string File path |
||
| 138 | * @param object Target |
||
| 139 | * @param boolean Included? Default is false |
||
| 140 | * @return \PHPDaemon\Config\Parser |
||
| 141 | */ |
||
| 142 | public static function parse($file, $target, $included = false) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Constructor |
||
| 156 | * @return void |
||
|
|
|||
| 157 | */ |
||
| 158 | protected function __construct($file, $target, $included = false) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Removes old config parts after updating. |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | protected function purgeScope($scope) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns current variable scope |
||
| 480 | * @return object Scope. |
||
| 481 | */ |
||
| 482 | public function getCurrentScope() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Raises error message. |
||
| 491 | * @param string Message. |
||
| 492 | * @param string Level. |
||
| 493 | * @param string $msg |
||
| 494 | * @return void |
||
| 495 | */ |
||
| 496 | public function raiseError($msg, $level = 'emerg', $line = null, $col = null) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Executes token server. |
||
| 513 | * @param string $c |
||
| 514 | * @return mixed|void |
||
| 515 | */ |
||
| 516 | protected function token($token, $c) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Current character. |
||
| 523 | * @return string Character. |
||
| 524 | */ |
||
| 525 | protected function getCurrentChar() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Returns next character. |
||
| 541 | * @return string Character. |
||
| 542 | */ |
||
| 543 | protected function getNextChar() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Rewinds the pointer back. |
||
| 550 | * @param integer Number of characters to rewind back. |
||
| 551 | * @return void |
||
| 552 | */ |
||
| 553 | protected function rewind($n) |
||
| 557 | } |
||
| 558 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.