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:
1 | <?php |
||
15 | class LocalPart extends Parser |
||
16 | { |
||
17 | 130 | public function parse($localPart) |
|
18 | { |
||
19 | 130 | $parseDQuote = true; |
|
20 | 130 | $closingQuote = false; |
|
21 | 130 | $openedParenthesis = 0; |
|
22 | 130 | $totalLength = 0; |
|
23 | |||
24 | 130 | while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
|
25 | 130 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { |
|
|||
26 | 2 | throw new DotAtStart(); |
|
27 | } |
||
28 | |||
29 | 128 | $closingQuote = $this->checkDQUOTE($closingQuote); |
|
30 | 127 | if ($closingQuote && $parseDQuote) { |
|
31 | 20 | $parseDQuote = $this->parseDoubleQuote(); |
|
32 | 12 | } |
|
33 | |||
34 | 119 | if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
35 | 7 | $this->parseComments(); |
|
36 | 3 | $openedParenthesis += $this->getOpenedParenthesis(); |
|
37 | 3 | } |
|
38 | |||
39 | 116 | if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
40 | 4 | if ($openedParenthesis === 0) { |
|
41 | 2 | throw new UnopenedComment(); |
|
42 | } |
||
43 | |||
44 | 3 | $openedParenthesis--; |
|
45 | 3 | } |
|
46 | |||
47 | 116 | $this->checkConsecutiveDots(); |
|
48 | |||
49 | 116 | View Code Duplication | if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
50 | 3 | $this->lexer->isNextToken(EmailLexer::S_AT) |
|
51 | 116 | ) { |
|
52 | 2 | throw new DotAtEnd(); |
|
53 | } |
||
54 | |||
55 | 116 | $this->warnEscaping(); |
|
56 | 111 | $this->isInvalidToken($this->lexer->token, $closingQuote); |
|
57 | |||
58 | 110 | if ($this->isFWS()) { |
|
59 | 7 | $this->parseFWS(); |
|
60 | 3 | } |
|
61 | |||
62 | 110 | $totalLength += strlen($this->lexer->token['value']); |
|
63 | 110 | $this->lexer->moveNext(); |
|
64 | 110 | } |
|
65 | |||
66 | 93 | if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
|
67 | 3 | $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
|
68 | 3 | } |
|
69 | 93 | } |
|
70 | |||
71 | /** |
||
72 | * @return bool |
||
73 | */ |
||
74 | 20 | protected function parseDoubleQuote() |
|
124 | |||
125 | /** |
||
126 | * @param bool $closingQuote |
||
127 | */ |
||
128 | 111 | protected function isInvalidToken(array $token, $closingQuote) |
|
145 | } |
||
146 |
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.