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 |
||
| 24 | abstract class Parser |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \Egulias\EmailValidator\Warning\Warning[] |
||
| 28 | */ |
||
| 29 | protected $warnings = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var EmailLexer |
||
| 33 | */ |
||
| 34 | protected $lexer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $openedParenthesis = 0; |
||
| 40 | |||
| 41 | public function __construct(EmailLexer $lexer) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return \Egulias\EmailValidator\Warning\Warning[] |
||
| 48 | */ |
||
| 49 | public function getWarnings() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $str |
||
| 56 | */ |
||
| 57 | abstract public function parse($str); |
||
| 58 | |||
| 59 | /** @return int */ |
||
| 60 | public function getOpenedParenthesis() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * validateQuotedPair |
||
| 67 | */ |
||
| 68 | protected function validateQuotedPair() |
||
| 78 | |||
| 79 | protected function parseComments() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | protected function isUnclosedComment() |
||
| 114 | |||
| 115 | protected function parseFWS() |
||
| 116 | { |
||
| 117 | $previous = $this->lexer->getPrevious(); |
||
| 118 | |||
| 119 | $this->checkCRLFInFWS(); |
||
| 120 | |||
| 121 | if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
||
| 122 | throw new CRNoLF(); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
||
| 126 | throw new AtextAfterCFWS(); |
||
| 127 | } |
||
| 128 | |||
| 129 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
| 130 | throw new ExpectingCTEXT(); |
||
| 131 | } |
||
| 132 | |||
| 133 | View Code Duplication | if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
| 134 | $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
||
| 135 | } else { |
||
| 136 | $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | protected function checkConsecutiveDots() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | View Code Duplication | protected function isFWS() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | View Code Duplication | protected function escaped() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | protected function warnEscaping() : bool |
||
| 198 | |||
| 199 | protected function validateEscaping() : Result |
||
| 220 | |||
| 221 | protected function checkCRLFInFWS() |
||
| 235 | } |
||
| 236 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: