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 |
||
22 | abstract class Parser |
||
23 | { |
||
24 | protected $warnings = []; |
||
25 | protected $lexer; |
||
26 | protected $openedParenthesis = 0; |
||
27 | |||
28 | 130 | public function __construct(EmailLexer $lexer) |
|
29 | { |
||
30 | 130 | $this->lexer = $lexer; |
|
31 | 130 | } |
|
32 | |||
33 | 52 | public function getWarnings() |
|
34 | { |
||
35 | 52 | return $this->warnings; |
|
36 | } |
||
37 | |||
38 | abstract public function parse($str); |
||
39 | |||
40 | /** @return int */ |
||
41 | 6 | public function getOpenedParenthesis() |
|
45 | |||
46 | /** |
||
47 | * validateQuotedPair |
||
48 | */ |
||
49 | protected function validateQuotedPair() |
||
61 | |||
62 | 10 | protected function parseComments() |
|
84 | |||
85 | 13 | protected function isUnclosedComment() |
|
94 | |||
95 | 16 | protected function parseFWS() |
|
119 | |||
120 | 114 | protected function checkConsecutiveDots() |
|
126 | |||
127 | 108 | protected function isFWS() |
|
144 | |||
145 | 115 | protected function escaped() |
|
158 | |||
159 | 114 | protected function warnEscaping() |
|
180 | |||
181 | 126 | protected function checkDQUOTE($hasClosingQuote) |
|
182 | { |
||
183 | 126 | if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { |
|
184 | 105 | return $hasClosingQuote; |
|
185 | } |
||
186 | 22 | if ($hasClosingQuote) { |
|
187 | return $hasClosingQuote; |
||
188 | } |
||
189 | 22 | $previous = $this->lexer->getPrevious() ?: ['type' => null, 'value' => '']; |
|
190 | 22 | View Code Duplication | if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) { |
191 | 1 | throw new ExpectingATEXT(); |
|
192 | } |
||
193 | |||
194 | try { |
||
195 | 21 | $this->lexer->find(EmailLexer::S_DQUOTE); |
|
196 | 20 | $hasClosingQuote = true; |
|
197 | 21 | } catch (\Exception $e) { |
|
198 | 1 | throw new UnclosedQuotedString(); |
|
199 | } |
||
200 | 20 | $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']); |
|
201 | |||
202 | 20 | return $hasClosingQuote; |
|
203 | } |
||
204 | |||
205 | 16 | protected function checkCRLFInFWS() |
|
219 | } |
||
220 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.