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 | /** |
||
| 37 | * @param Client $client |
||
| 38 | * @param FileReader $fileReader |
||
| 39 | */ |
||
| 40 | public function __construct(Client $client, FileReader $fileReader) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | public function execute() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $localPath |
||
| 97 | * @param string $crowdinPath |
||
| 98 | * @param string $exportPattern |
||
| 99 | * @param string $title |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param Translation[] $translations |
||
| 116 | * @return UploadTranslation |
||
| 117 | */ |
||
| 118 | public function setTranslations(array $translations) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return Translation[] |
||
| 127 | */ |
||
| 128 | public function getTranslations() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param bool $importsAutoApproved |
||
| 135 | * |
||
| 136 | * @throws InvalidArgumentException |
||
| 137 | * |
||
| 138 | * @return UploadTranslation |
||
| 139 | */ |
||
| 140 | public function setImportsAutoApproved($importsAutoApproved) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function areImportsAutoApproved() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param bool $duplicatesImported |
||
| 161 | * |
||
| 162 | * @throws InvalidArgumentException |
||
| 163 | * |
||
| 164 | * @return UploadTranslation |
||
| 165 | */ |
||
| 166 | public function setDuplicatesImported($duplicatesImported) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function areDuplicatesImported() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param bool $equalSuggestionsImported |
||
| 187 | * |
||
| 188 | * @throws InvalidArgumentException |
||
| 189 | * |
||
| 190 | * @return UploadTranslation |
||
| 191 | */ |
||
| 192 | public function setEqualSuggestionsImported($equalSuggestionsImported) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function areEqualSuggestionsImported() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $locale |
||
| 213 | * |
||
| 214 | * @return UploadTranslation |
||
| 215 | */ |
||
| 216 | public function setLocale($locale) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getLocale() |
||
| 230 | } |
||
| 231 |
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.