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 |
||
13 | class SimpleReader implements ReaderInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var WalkerInterface |
||
17 | */ |
||
18 | protected $walker; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function __construct(WalkerInterface $walker) |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 3 | public function getLinesRange(\SplFileObject $file, $from, $to) |
|
32 | { |
||
33 | 3 | if ($from < 0 || $to > $this->walker->countLines($file)) { |
|
34 | 2 | throw new OutOfBoundsException(); |
|
35 | } |
||
36 | |||
37 | 1 | return new \LimitIterator($file, $from, $to); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 3 | View Code Duplication | public function getNextLineContent(\SplFileObject $file) |
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 2 | View Code Duplication | public function getPreviousLineContent(\SplFileObject $file) |
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 1 | public function getCurrentLineContent(\SplFileObject $file) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 3 | public function getLineContent(\SplFileObject $file, $lineNumber) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 1 | public function getNextCharacterContent(\SplFileObject $file) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 1 | View Code Duplication | public function getPreviousCharacterContent(\SplFileObject $file) |
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | View Code Duplication | public function getCharacterContent(\SplFileObject $file, $characterNumber) |
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 1 | public function cleanLineContent($content) |
|
161 | } |
||
162 |
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.