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 |
||
35 | class DomainPart extends Parser |
||
36 | { |
||
37 | const DOMAIN_MAX_LENGTH = 254; |
||
38 | protected $domainPart = ''; |
||
39 | |||
40 | 92 | public function parse($domainPart) |
|
41 | { |
||
42 | 92 | $this->lexer->moveNext(); |
|
43 | |||
44 | 92 | $this->performDomainStartChecks(); |
|
45 | |||
46 | 82 | $domain = $this->doParseDomainPart(); |
|
47 | |||
48 | 55 | $prev = $this->lexer->getPrevious() ?: ['type' => null]; |
|
49 | 55 | $length = strlen($domain); |
|
50 | |||
51 | 55 | if ($prev['type'] === EmailLexer::S_DOT) { |
|
52 | 2 | throw new DotAtEnd(); |
|
53 | } |
||
54 | 53 | if ($prev['type'] === EmailLexer::S_HYPHEN) { |
|
55 | 1 | throw new DomainHyphened(); |
|
56 | } |
||
57 | 52 | if ($length > self::DOMAIN_MAX_LENGTH) { |
|
58 | 2 | $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
|
59 | 2 | } |
|
60 | 52 | if ($prev['type'] === EmailLexer::S_CR) { |
|
61 | throw new CRLFAtTheEnd(); |
||
62 | } |
||
63 | 52 | $this->domainPart = $domain; |
|
64 | 52 | } |
|
65 | |||
66 | 92 | private function performDomainStartChecks() |
|
76 | |||
77 | 91 | private function checkEmptyDomain() |
|
87 | |||
88 | 92 | private function checkInvalidTokensAfterAT() |
|
97 | |||
98 | 52 | public function getDomainPart() |
|
102 | |||
103 | 7 | public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
|
104 | { |
||
105 | 7 | $prev = $this->lexer->getPrevious() ?: ['type' => null]; |
|
106 | 7 | if ($prev['type'] === EmailLexer::S_COLON) { |
|
107 | 1 | $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
|
108 | 1 | } |
|
109 | |||
110 | 7 | $IPv6 = substr($addressLiteral, 5); |
|
111 | //Daniel Marschall's new IPv6 testing strategy |
||
112 | 7 | $matchesIP = explode(':', $IPv6); |
|
113 | 7 | $groupCount = count($matchesIP); |
|
114 | 7 | $colons = strpos($IPv6, '::'); |
|
115 | |||
116 | 7 | if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
|
117 | 1 | $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
|
118 | 1 | } |
|
119 | |||
120 | 7 | if ($colons === false) { |
|
121 | // We need exactly the right number of groups |
||
122 | 4 | if ($groupCount !== $maxGroups) { |
|
123 | 1 | $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
|
124 | 1 | } |
|
125 | 4 | return; |
|
126 | } |
||
127 | |||
128 | 3 | if ($colons !== strrpos($IPv6, '::')) { |
|
129 | 1 | $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
|
130 | 1 | return; |
|
131 | } |
||
132 | |||
133 | 2 | if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
|
134 | // RFC 4291 allows :: at the start or end of an address |
||
135 | //with 7 other groups in addition |
||
136 | 2 | ++$maxGroups; |
|
137 | 2 | } |
|
138 | |||
139 | 2 | if ($groupCount > $maxGroups) { |
|
140 | 1 | $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
|
141 | 2 | } elseif ($groupCount === $maxGroups) { |
|
142 | 1 | $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
|
143 | 1 | } |
|
144 | 2 | } |
|
145 | |||
146 | 82 | protected function doParseDomainPart() |
|
147 | { |
||
148 | 82 | $domain = ''; |
|
149 | 82 | $openedParenthesis = 0; |
|
150 | do { |
||
151 | 82 | $prev = $this->lexer->getPrevious() ?: ['type' => null]; |
|
152 | |||
153 | 82 | $this->checkNotAllowedChars($this->lexer->token); |
|
154 | |||
155 | 82 | if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
156 | 3 | $this->parseComments(); |
|
157 | 3 | $openedParenthesis += $this->getOpenedParenthesis(); |
|
158 | 3 | $this->lexer->moveNext(); |
|
159 | 3 | $tmpPrev = $this->lexer->getPrevious() ?: ['type' => null]; |
|
160 | 3 | if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
161 | 3 | $openedParenthesis--; |
|
162 | 3 | } |
|
163 | 3 | } |
|
164 | 82 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
|||
165 | 3 | if ($openedParenthesis === 0) { |
|
166 | 3 | throw new UnopenedComment(); |
|
167 | } else { |
||
168 | $openedParenthesis--; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | 81 | $this->checkConsecutiveDots(); |
|
173 | 81 | $this->checkDomainPartExceptions($prev); |
|
174 | |||
175 | 80 | if ($this->hasBrackets()) { |
|
176 | 13 | $this->parseDomainLiteral(); |
|
177 | 11 | } |
|
178 | |||
179 | 78 | $this->checkLabelLength($prev); |
|
180 | |||
181 | 78 | if ($this->isFWS()) { |
|
182 | 9 | $this->parseFWS(); |
|
183 | 7 | } |
|
184 | |||
185 | 78 | $domain .= $this->lexer->token['value']; |
|
186 | 78 | $this->lexer->moveNext(); |
|
187 | 78 | } while ($this->lexer->token); |
|
188 | |||
189 | 55 | return $domain; |
|
190 | } |
||
191 | |||
192 | 82 | private function checkNotAllowedChars($token) |
|
199 | |||
200 | 13 | protected function parseDomainLiteral() |
|
215 | |||
216 | 13 | protected function doParseDomainLiteral() |
|
282 | |||
283 | 11 | protected function checkIPV4Tag($addressLiteral) |
|
305 | |||
306 | 81 | protected function checkDomainPartExceptions($prev) |
|
340 | |||
341 | 80 | protected function hasBrackets() |
|
355 | |||
356 | 78 | protected function checkLabelLength($prev) |
|
365 | |||
366 | 3 | protected function parseDomainComments() |
|
379 | |||
380 | protected function addTLDWarnings() |
||
386 | } |
||
387 |
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.