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 |
||
12 | class Parser implements ParserInterface |
||
13 | { |
||
14 | /** @var ValueParserInterface[] */ |
||
15 | private $valueParsers = []; |
||
16 | |||
17 | /** |
||
18 | * Parser constructor. |
||
19 | * |
||
20 | * @param ValueParserInterface[] $valueParsers |
||
21 | */ |
||
22 | 12 | public function __construct(array $valueParsers = []) |
|
26 | |||
27 | /** |
||
28 | * @param Iterator $tokens |
||
29 | * |
||
30 | * @return Iterator Iterator of csv line Iterators |
||
31 | */ |
||
32 | 8 | public function parse(Iterator $tokens) |
|
33 | { |
||
34 | 8 | $value = new Value($this->valueParsers); |
|
35 | 8 | $row = new ArrayIterator(); |
|
36 | |||
37 | 8 | $tokens->rewind(); |
|
38 | /** @var Token $token */ |
||
39 | 8 | $token = $tokens->current(); |
|
40 | 8 | while (!is_null($token)) { |
|
41 | 7 | switch (true) { |
|
42 | 7 | case $token->getType() == Token::T_ESCAPE: |
|
43 | 3 | $tokens->next(); |
|
44 | 3 | $token = $tokens->current(); |
|
45 | 3 | if (is_null($token)) { |
|
46 | 1 | throw new RuntimeException( |
|
47 | "Invalid CSV: The csv strings final character is an escape character" |
||
48 | 1 | ); |
|
49 | } |
||
50 | 2 | $value->addContent($token->getContent()); |
|
51 | 2 | break; |
|
52 | |||
53 | 7 | case ($value->isInQuotes() && $token->getType() == Token::T_DOUBLE_QUOTE): |
|
54 | 1 | $value->addContent(substr($token->getContent(), 0, $token->getLength() / 2)); |
|
55 | 1 | break; |
|
56 | |||
57 | 7 | case $token->getType() == Token::T_QUOTE: |
|
58 | 6 | $value->setInQuotes(!$value->isInQuotes()); |
|
59 | 6 | break; |
|
60 | |||
61 | 7 | case $value->isInQuotes(): |
|
62 | 6 | $value->addContent($token->getContent()); |
|
63 | 6 | break; |
|
64 | |||
65 | 7 | case ($value->isEmpty() |
|
66 | 7 | && !$value->isInQuotes() |
|
67 | 7 | && !$value->wasQuoted() |
|
68 | 7 | && $token->getType() == Token::T_NULL): |
|
69 | 4 | $value->addContent($token->getContent()); |
|
70 | 4 | $value->setIsNull(); |
|
71 | 4 | break; |
|
72 | |||
73 | 7 | View Code Duplication | case (!$value->isInQuotes() && $token->getType() == Token::T_DELIMITER): |
|
|||
74 | 6 | $row->append($value->getValue()); |
|
75 | 6 | $value->reset(); |
|
76 | 6 | break; |
|
77 | |||
78 | 6 | View Code Duplication | case (!$value->isInQuotes() && $token->getType() == Token::T_NEW_LINE): |
79 | 2 | $row->append($value->getValue()); |
|
80 | 2 | $value->reset(); |
|
81 | 2 | yield $row; |
|
82 | 2 | $row = new ArrayIterator(); |
|
83 | 2 | break; |
|
84 | |||
85 | 6 | default: |
|
86 | 6 | $value->addContent($token->getContent()); |
|
87 | 5 | break; |
|
88 | 6 | } |
|
89 | |||
90 | 7 | $tokens->next(); |
|
91 | 7 | $token = $tokens->current(); |
|
92 | 7 | } |
|
93 | |||
94 | 6 | if (!$value->isEmpty()) { |
|
95 | 5 | $row->append($value->getValue()); |
|
96 | 5 | } |
|
97 | |||
98 | 6 | yield new ArrayIterator($row); |
|
99 | 6 | } |
|
100 | |||
101 | /** |
||
102 | * @param ValueParserInterface $valueParser |
||
103 | */ |
||
104 | 4 | public function addValueParser(ValueParserInterface $valueParser) |
|
108 | |||
109 | /** |
||
110 | * @param ValueParserInterface $valueParser |
||
111 | */ |
||
112 | 1 | public function removeValueParser(ValueParserInterface $valueParser) |
|
119 | |||
120 | /** |
||
121 | * @param ValueParserInterface $valueParser |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 3 | public function hasValueParser(ValueParserInterface $valueParser) |
|
129 | } |
||
130 |
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.