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() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $localPath |
||
| 107 | * @param string $crowdinPath |
||
| 108 | * @param string $exportPattern |
||
| 109 | * @param string $title |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param Translation[] $translations |
||
| 126 | * @return UploadTranslation |
||
| 127 | */ |
||
| 128 | public function setTranslations(array $translations) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return Translation[] |
||
| 137 | */ |
||
| 138 | public function getTranslations() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param bool $importsAutoApproved |
||
| 145 | * |
||
| 146 | * @throws InvalidArgumentException |
||
| 147 | * |
||
| 148 | * @return UploadTranslation |
||
| 149 | */ |
||
| 150 | public function setImportsAutoApproved($importsAutoApproved) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function areImportsAutoApproved() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param bool $duplicatesImported |
||
| 171 | * |
||
| 172 | * @throws InvalidArgumentException |
||
| 173 | * |
||
| 174 | * @return UploadTranslation |
||
| 175 | */ |
||
| 176 | public function setDuplicatesImported($duplicatesImported) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function areDuplicatesImported() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param bool $equalSuggestionsImported |
||
| 197 | * |
||
| 198 | * @throws InvalidArgumentException |
||
| 199 | * |
||
| 200 | * @return UploadTranslation |
||
| 201 | */ |
||
| 202 | public function setEqualSuggestionsImported($equalSuggestionsImported) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function areEqualSuggestionsImported() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $locale |
||
| 223 | * |
||
| 224 | * @return UploadTranslation |
||
| 225 | */ |
||
| 226 | public function setLocale($locale) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getLocale() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string|null |
||
| 243 | */ |
||
| 244 | public function getBranch() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $branch |
||
| 251 | * |
||
| 252 | * @return UploadTranslation |
||
| 253 | */ |
||
| 254 | public function setBranch($branch) |
||
| 260 | } |
||
| 261 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.