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 |
||
9 | class TokenStream extends \Symfony\Component\ExpressionLanguage\TokenStream |
||
10 | { |
||
11 | /** @var Token[] */ |
||
12 | private $tokens; |
||
13 | |||
14 | /** @var int */ |
||
15 | private $position = 0; |
||
16 | |||
17 | /** |
||
18 | 42 | * Overrides parent constructor because of private properties. |
|
19 | * |
||
20 | 42 | * {@inheritdoc} |
|
21 | */ |
||
22 | 42 | public function __construct(array $tokens) |
|
29 | |||
30 | /** |
||
31 | * Overrides parent method because of private properties. |
||
32 | * |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function __toString() |
||
39 | |||
40 | /** |
||
41 | 30 | * Overrides parent method because of private properties. |
|
42 | * |
||
43 | 30 | * {@inheritdoc} |
|
44 | */ |
||
45 | View Code Duplication | public function next(): void |
|
55 | 42 | ||
56 | /** |
||
57 | 42 | * Move stream pointer to the beginning. |
|
58 | 42 | */ |
|
59 | 42 | public function rewind(): void |
|
64 | |||
65 | /** |
||
66 | * Move to a particular position in the stream. |
||
67 | 4 | * |
|
68 | * @param int $offset The offset relative to $whence |
||
69 | * @param int $whence One of SEEK_SET, SEEK_CUR or SEEK_END constants |
||
70 | 4 | */ |
|
71 | 1 | public function seek($offset, $whence): void |
|
99 | 4 | ||
100 | /** |
||
101 | 4 | * Sets the pointer to the previous token. |
|
102 | */ |
||
103 | View Code Duplication | public function prev(): void |
|
113 | |||
114 | /** |
||
115 | * Tests a token and moves to previous one. |
||
116 | * |
||
117 | 4 | * @param array|int $type The type to test |
|
118 | * @param string|null $value The token value |
||
119 | 4 | * @param string|null $message The syntax error message |
|
120 | 4 | */ |
|
121 | public function expectPrev($type, $value = null, $message = null): void |
||
139 | |||
140 | /** |
||
141 | * Returns new TokenStream with tokens replaced by some others. |
||
142 | * |
||
143 | * @param int $offset |
||
144 | * @param int $length |
||
145 | 4 | * @param Token[] $replacements |
|
146 | * |
||
147 | 4 | * @return static |
|
148 | 4 | */ |
|
149 | public function splice($offset, $length, $replacements): self |
||
156 | |||
157 | /** |
||
158 | 6 | * Returns the current position. |
|
159 | * |
||
160 | 6 | * @return int |
|
161 | */ |
||
162 | public function position(): int |
||
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.