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 UploadTranslation extends AbstractApi |
||
17 | { |
||
18 | /** @var FileReader */ |
||
19 | protected $fileReader; |
||
20 | |||
21 | /** @var Translation[] */ |
||
22 | protected $translations; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $locale; |
||
26 | |||
27 | /** @var bool */ |
||
28 | protected $areDuplicatesImported = false; |
||
29 | |||
30 | /** @var bool */ |
||
31 | protected $areEqualSuggestionsImported = false; |
||
32 | |||
33 | /** @var bool */ |
||
34 | protected $areImportsAutoApproved = false; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $branch; |
||
38 | |||
39 | /** |
||
40 | * @param Client $client |
||
41 | * @param FileReader $fileReader |
||
42 | */ |
||
43 | public function __construct(Client $client, FileReader $fileReader) |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function execute() |
||
106 | |||
107 | /** |
||
108 | * @param string $localPath |
||
109 | * @param string $crowdinPath |
||
110 | * @param string $exportPattern |
||
111 | * @param string $title |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | View Code Duplication | public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
125 | |||
126 | /** |
||
127 | * @param Translation[] $translations |
||
128 | * @return UploadTranslation |
||
129 | */ |
||
130 | public function setTranslations(array $translations) |
||
136 | |||
137 | /** |
||
138 | * @return Translation[] |
||
139 | */ |
||
140 | public function getTranslations() |
||
144 | |||
145 | /** |
||
146 | * @param bool $importsAutoApproved |
||
147 | * |
||
148 | * @throws InvalidArgumentException |
||
149 | * |
||
150 | * @return UploadTranslation |
||
151 | */ |
||
152 | public function setImportsAutoApproved($importsAutoApproved) |
||
162 | |||
163 | /** |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function areImportsAutoApproved() |
||
170 | |||
171 | /** |
||
172 | * @param bool $duplicatesImported |
||
173 | * |
||
174 | * @throws InvalidArgumentException |
||
175 | * |
||
176 | * @return UploadTranslation |
||
177 | */ |
||
178 | public function setDuplicatesImported($duplicatesImported) |
||
188 | |||
189 | /** |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function areDuplicatesImported() |
||
196 | |||
197 | /** |
||
198 | * @param bool $equalSuggestionsImported |
||
199 | * |
||
200 | * @throws InvalidArgumentException |
||
201 | * |
||
202 | * @return UploadTranslation |
||
203 | */ |
||
204 | public function setEqualSuggestionsImported($equalSuggestionsImported) |
||
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function areEqualSuggestionsImported() |
||
222 | |||
223 | /** |
||
224 | * @param string $locale |
||
225 | * |
||
226 | * @return UploadTranslation |
||
227 | */ |
||
228 | public function setLocale($locale) |
||
234 | |||
235 | /** |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getLocale() |
||
242 | |||
243 | /** |
||
244 | * @return string|null |
||
245 | */ |
||
246 | public function getBranch() |
||
250 | |||
251 | /** |
||
252 | * @param string $branch |
||
253 | * |
||
254 | * @return UploadTranslation |
||
255 | */ |
||
256 | public function setBranch($branch) |
||
262 | } |
||
263 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.