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 |
||
17 | class GenericConsumer extends AbstractConsumer |
||
18 | { |
||
19 | /** |
||
20 | * Returns \ZBateson\MailMimeParser\Header\Consumer\CommentConsumer and |
||
21 | * \ZBateson\MailMimeParser\Header\Consumer\QuotedStringConsumer as |
||
22 | * sub-consumers. |
||
23 | * |
||
24 | * @return AbstractConsumer[] the sub-consumers |
||
25 | */ |
||
26 | protected function getSubConsumers() |
||
27 | { |
||
28 | return [ |
||
29 | $this->consumerService->getCommentConsumer(), |
||
30 | $this->consumerService->getQuotedStringConsumer(), |
||
31 | ]; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Returns the regex '\s+' (whitespace) pattern matcher as a token marker so |
||
36 | * the header value is split along whitespace characters. GenericConsumer |
||
37 | * filters out whitespace-only tokens from getPartForToken. |
||
38 | * |
||
39 | * The whitespace character delimits mime-encoded parts for decoding. |
||
40 | * |
||
41 | * @return string[] an array of regex pattern matchers |
||
42 | */ |
||
43 | protected function getTokenSeparators() |
||
47 | |||
48 | /** |
||
49 | * GenericConsumer doesn't have start/end tokens, and so always returns |
||
50 | * false. |
||
51 | * |
||
52 | * @param string $token |
||
53 | * @return boolean false |
||
54 | */ |
||
55 | protected function isEndToken($token) |
||
59 | |||
60 | /** |
||
61 | * GenericConsumer doesn't have start/end tokens, and so always returns |
||
62 | * false. |
||
63 | * |
||
64 | * @param string $token |
||
65 | * @return boolean false |
||
66 | */ |
||
67 | protected function isStartToken($token) |
||
71 | |||
72 | /** |
||
73 | * Creates and returns a |
||
74 | * \ZBateson\MailMimeParser\Header\Part\MimeLiteralPart out of the passed |
||
75 | * string token and returns it. |
||
76 | * |
||
77 | * @param string $token |
||
78 | * @param bool $isLiteral |
||
79 | * @return \ZBateson\MailMimeParser\Header\Part\HeaderPart |
||
80 | */ |
||
81 | View Code Duplication | protected function getPartForToken($token, $isLiteral) |
|
91 | |||
92 | /** |
||
93 | * Checks if the passed space part should be added to the returned parts and |
||
94 | * adds it. |
||
95 | * |
||
96 | * Never adds a space if it's the first part, otherwise only add it if |
||
97 | * either part isn't set to ignore the space |
||
98 | * |
||
99 | * @param array $parts |
||
100 | * @param array $retParts |
||
101 | * @param ZBateson\MailMimeParser\Header\Part\HeaderPart $spacePart |
||
102 | * @param int $curIndex |
||
103 | * @return boolean true if the part was added |
||
104 | */ |
||
105 | private function checkAddFilteredSpace(array $parts, array &$retParts, &$spacePart, $curIndex) |
||
119 | |||
120 | /** |
||
121 | * Filters out ignorable spaces between parts in the passed array. |
||
122 | * |
||
123 | * Spaces with parts on either side of it that specify they can be ignored |
||
124 | * are filtered out. filterIgnoredSpaces is called from within |
||
125 | * processParts, and if needed by an implementing class that overrides |
||
126 | * processParts, must be specifically called. |
||
127 | * |
||
128 | * @param ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts |
||
129 | * @return ZBateson\MailMimeParser\Header\Part\HeaderPart[] |
||
130 | */ |
||
131 | protected function filterIgnoredSpaces(array $parts) |
||
149 | |||
150 | /** |
||
151 | * Overridden to combine all part values into a single string and return it |
||
152 | * as an array with a single element. |
||
153 | * |
||
154 | * @param ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts |
||
155 | * @return ZBateson\MailMimeParser\Header\Part\LiteralPart[] |
||
156 | */ |
||
157 | protected function processParts(array $parts) |
||
166 | } |
||
167 |
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.