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 DomainPart 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 DomainPart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class DomainPart extends Parser |
||
39 | { |
||
40 | const DOMAIN_MAX_LENGTH = 254; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $domainPart = ''; |
||
46 | |||
47 | public function parse($domainPart) |
||
48 | { |
||
49 | $this->lexer->moveNext(); |
||
50 | |||
51 | $domainChecks = $this->performDomainStartChecks(); |
||
52 | if ($domainChecks->isInvalid()) { |
||
53 | return $domainChecks; |
||
54 | } |
||
55 | |||
56 | $domain = $this->doParseDomainPart(); |
||
57 | if ($domain->isInvalid()) { |
||
58 | return $domain; |
||
59 | } |
||
60 | |||
61 | $prev = $this->lexer->getPrevious(); |
||
62 | $length = strlen($this->domainPart); |
||
63 | |||
64 | if ($prev['type'] === EmailLexer::S_DOT) { |
||
65 | throw new DotAtEnd(); |
||
66 | } |
||
67 | if ($prev['type'] === EmailLexer::S_HYPHEN) { |
||
68 | throw new DomainHyphened(); |
||
69 | } |
||
70 | if ($length > self::DOMAIN_MAX_LENGTH) { |
||
71 | $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
||
72 | } |
||
73 | if ($prev['type'] === EmailLexer::S_CR) { |
||
74 | throw new CRLFAtTheEnd(); |
||
75 | } |
||
76 | |||
77 | return new ValidEmail(); |
||
78 | } |
||
79 | |||
80 | private function performDomainStartChecks() : Result |
||
81 | { |
||
82 | $invalidTokens = $this->checkInvalidTokensAfterAT(); |
||
83 | if ($invalidTokens->isInvalid()) { |
||
84 | return $invalidTokens; |
||
85 | } |
||
86 | |||
87 | $missingDomain = $this->checkEmptyDomain(); |
||
88 | if ($missingDomain->isInvalid()) { |
||
89 | return $missingDomain; |
||
90 | } |
||
91 | |||
92 | if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
||
93 | $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
||
94 | } |
||
95 | return new ValidEmail(); |
||
96 | } |
||
97 | |||
98 | private function checkEmptyDomain() : Result |
||
99 | { |
||
100 | $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY || |
||
101 | ($this->lexer->token['type'] === EmailLexer::S_SP && |
||
102 | !$this->lexer->isNextToken(EmailLexer::GENERIC)); |
||
103 | |||
104 | if ($thereIsNoDomain) { |
||
105 | return new InvalidEmail(new ReasonNoDomainPart(), $this->lexer->token['value']); |
||
106 | } |
||
107 | |||
108 | return new ValidEmail(); |
||
109 | } |
||
110 | |||
111 | private function checkInvalidTokensAfterAT() : Result |
||
112 | { |
||
113 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
|
|||
114 | return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']); |
||
115 | } |
||
116 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) { |
|
117 | return new InvalidEmail(new ReasonDomainHyphened('After AT'), $this->lexer->token['value']); |
||
118 | } |
||
119 | return new ValidEmail(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getDomainPart() |
||
126 | { |
||
127 | return $this->domainPart; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $addressLiteral |
||
132 | * @param int $maxGroups |
||
133 | */ |
||
134 | public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
||
135 | { |
||
136 | $prev = $this->lexer->getPrevious(); |
||
137 | if ($prev['type'] === EmailLexer::S_COLON) { |
||
138 | $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
||
139 | } |
||
140 | |||
141 | $IPv6 = substr($addressLiteral, 5); |
||
142 | //Daniel Marschall's new IPv6 testing strategy |
||
143 | $matchesIP = explode(':', $IPv6); |
||
144 | $groupCount = count($matchesIP); |
||
145 | $colons = strpos($IPv6, '::'); |
||
146 | |||
147 | if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
||
148 | $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
||
149 | } |
||
150 | |||
151 | if ($colons === false) { |
||
152 | // We need exactly the right number of groups |
||
153 | if ($groupCount !== $maxGroups) { |
||
154 | $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
||
155 | } |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | if ($colons !== strrpos($IPv6, '::')) { |
||
160 | $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
||
161 | return; |
||
162 | } |
||
163 | |||
164 | if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
||
165 | // RFC 4291 allows :: at the start or end of an address |
||
166 | //with 7 other groups in addition |
||
167 | ++$maxGroups; |
||
168 | } |
||
169 | |||
170 | if ($groupCount > $maxGroups) { |
||
171 | $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
||
172 | } elseif ($groupCount === $maxGroups) { |
||
173 | $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | View Code Duplication | protected function parseComments() |
|
178 | { |
||
179 | $commentParser = new Comment($this->lexer, new DomainComment()); |
||
180 | $result = $commentParser->parse('remove'); |
||
181 | if($result->isInvalid()) { |
||
182 | return $result; |
||
183 | } |
||
184 | |||
185 | $this->warnings = array_merge($this->warnings, $commentParser->getWarnings()); |
||
186 | return $result; |
||
187 | } |
||
188 | |||
189 | protected function doParseDomainPart() : Result |
||
190 | { |
||
191 | $domain = ''; |
||
192 | do { |
||
193 | $prev = $this->lexer->getPrevious(); |
||
194 | |||
195 | $this->checkNotAllowedChars($this->lexer->token); |
||
196 | |||
197 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS || |
|
198 | $this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) { |
||
199 | $commentsResult = $this->parseComments(); |
||
200 | |||
201 | //Invalid comment parsing |
||
202 | if($commentsResult->isInvalid()) { |
||
203 | return $commentsResult; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $this->checkConsecutiveDots(); |
||
208 | $this->checkDomainPartExceptions($prev); |
||
209 | |||
210 | if ($this->hasBrackets()) { |
||
211 | $this->parseDomainLiteral(); |
||
212 | } |
||
213 | |||
214 | $this->checkLabelLength($prev); |
||
215 | |||
216 | if ($this->isFWS()) { |
||
217 | $this->parseFWS(); |
||
218 | } |
||
219 | |||
220 | $domain .= $this->lexer->token['value']; |
||
221 | $this->lexer->moveNext(); |
||
222 | } while (null !== $this->lexer->token['type']); |
||
223 | |||
224 | $this->domainPart = $domain; |
||
225 | return new ValidEmail(); |
||
226 | } |
||
227 | |||
228 | private function checkNotAllowedChars(array $token) |
||
235 | |||
236 | /** |
||
237 | * @return string|false |
||
238 | */ |
||
239 | protected function parseDomainLiteral() |
||
254 | |||
255 | /** |
||
256 | * @return string|false |
||
257 | */ |
||
258 | protected function doParseDomainLiteral() |
||
324 | |||
325 | /** |
||
326 | * @param string $addressLiteral |
||
327 | * |
||
328 | * @return string|false |
||
329 | */ |
||
330 | protected function checkIPV4Tag($addressLiteral) |
||
352 | |||
353 | protected function checkDomainPartExceptions(array $prev) |
||
387 | |||
388 | /** |
||
389 | * @return bool |
||
390 | */ |
||
391 | protected function hasBrackets() |
||
405 | |||
406 | protected function checkLabelLength(array $prev) |
||
415 | |||
416 | protected function addTLDWarnings() |
||
417 | { |
||
418 | if ($this->warnings[DomainLiteral::CODE]) { |
||
419 | $this->warnings[TLD::CODE] = new TLD(); |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 |
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.