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 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $warnings = []; |
||
28 | |||
29 | /** |
||
30 | * @var EmailLexer |
||
31 | */ |
||
32 | protected $lexer; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $openedParenthesis = 0; |
||
38 | |||
39 | 132 | public function __construct(EmailLexer $lexer) |
|
43 | |||
44 | /** |
||
45 | * @return \Egulias\EmailValidator\Warning\Warning[] |
||
46 | */ |
||
47 | 51 | public function getWarnings() |
|
51 | |||
52 | /** |
||
53 | * @param string $str |
||
54 | */ |
||
55 | abstract public function parse($str); |
||
56 | |||
57 | /** @return int */ |
||
58 | 6 | public function getOpenedParenthesis() |
|
62 | |||
63 | /** |
||
64 | * validateQuotedPair |
||
65 | */ |
||
66 | protected function validateQuotedPair() |
||
76 | |||
77 | 10 | protected function parseComments() |
|
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | */ |
||
103 | 13 | protected function isUnclosedComment() |
|
112 | |||
113 | 6 | protected function parseFWS() |
|
114 | { |
||
115 | 6 | $previous = $this->lexer->getPrevious(); |
|
116 | |||
117 | 6 | $this->checkCRLFInFWS(); |
|
118 | |||
119 | 6 | if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
120 | 2 | throw new CRNoLF(); |
|
121 | } |
||
122 | |||
123 | 4 | View Code Duplication | if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
124 | 2 | throw new AtextAfterCFWS(); |
|
125 | } |
||
126 | |||
127 | 2 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
128 | throw new ExpectingCTEXT(); |
||
129 | } |
||
130 | |||
131 | 2 | if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
132 | 2 | $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
133 | 2 | } else { |
|
134 | $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
||
135 | } |
||
136 | 2 | } |
|
137 | |||
138 | 117 | protected function checkConsecutiveDots() |
|
144 | |||
145 | /** |
||
146 | * @return bool |
||
147 | */ |
||
148 | 111 | protected function isFWS() |
|
149 | { |
||
150 | 111 | if ($this->escaped()) { |
|
151 | return false; |
||
152 | } |
||
153 | |||
154 | 111 | if ($this->lexer->token['type'] === EmailLexer::S_SP || |
|
155 | 111 | $this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
156 | 111 | $this->lexer->token['type'] === EmailLexer::S_CR || |
|
157 | 111 | $this->lexer->token['type'] === EmailLexer::S_LF || |
|
158 | 111 | $this->lexer->token['type'] === EmailLexer::CRLF |
|
159 | 111 | ) { |
|
160 | 6 | return true; |
|
161 | } |
||
162 | |||
163 | 111 | return false; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return bool |
||
168 | */ |
||
169 | 118 | protected function escaped() |
|
182 | |||
183 | /** |
||
184 | * @return bool |
||
185 | */ |
||
186 | 117 | protected function warnEscaping() |
|
205 | |||
206 | /** |
||
207 | * @param bool $hasClosingQuote |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | 129 | protected function checkDQUOTE($hasClosingQuote) |
|
234 | |||
235 | 6 | protected function checkCRLFInFWS() |
|
249 | } |
||
250 |
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.