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 |
||
16 | class AddFile extends AbstractApi |
||
17 | { |
||
18 | /** @var FileReader */ |
||
19 | protected $fileReader; |
||
20 | |||
21 | /** @var Translation[] */ |
||
22 | protected $translations; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $type; |
||
26 | |||
27 | /** @var string */ |
||
28 | protected $branch; |
||
29 | |||
30 | /** |
||
31 | * @param Client $client |
||
32 | * @param FileReader $fileReader |
||
33 | */ |
||
34 | public function __construct(Client $client, FileReader $fileReader) |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function execute() |
||
92 | |||
93 | /** |
||
94 | * @param string $localPath |
||
95 | * @param string $crowdinPath |
||
96 | * @param string $exportPattern |
||
97 | * @param string $title |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | View Code Duplication | public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
111 | |||
112 | /** |
||
113 | * @return Translation[] |
||
114 | */ |
||
115 | public function getTranslations() |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getType() |
||
127 | |||
128 | /** |
||
129 | * @param string $type |
||
130 | * |
||
131 | * @return AddFile |
||
132 | */ |
||
133 | public function setType($type) |
||
139 | |||
140 | /** |
||
141 | * @return string|null |
||
142 | */ |
||
143 | public function getBranch() |
||
147 | |||
148 | /** |
||
149 | * @param string $branch |
||
150 | * |
||
151 | * @return AddFile |
||
152 | */ |
||
153 | public function setBranch($branch) |
||
159 | } |
||
160 |
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.