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() |
||
| 53 | { |
||
| 54 | if (0 === count($this->translations)) { |
||
| 55 | throw new InvalidArgumentException('There are no translations to upload.'); |
||
| 56 | } |
||
| 57 | |||
| 58 | if (null === $this->locale) { |
||
| 59 | throw new InvalidArgumentException('Locale is not set.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->addUrlParameter('key', $this->client->getProjectApiKey()); |
||
| 63 | |||
| 64 | $path = sprintf( |
||
| 65 | "project/%s/upload-translation?%s", |
||
| 66 | $this->client->getProjectIdentifier(), |
||
| 67 | $this->getUrlQueryString() |
||
| 68 | ); |
||
| 69 | |||
| 70 | $data[] = [ |
||
|
|
|||
| 71 | 'name' => 'import_duplicates', |
||
| 72 | 'contents' => (int)$this->areDuplicatesImported |
||
| 73 | ]; |
||
| 74 | $data[] = [ |
||
| 75 | 'name' => 'import_eq_suggestions', |
||
| 76 | 'contents' => (int)$this->areEqualSuggestionsImported |
||
| 77 | ]; |
||
| 78 | $data[] = [ |
||
| 79 | 'name' => 'auto_approve_imported', |
||
| 80 | 'contents' => (int)$this->areImportsAutoApproved |
||
| 81 | ]; |
||
| 82 | $data[] = [ |
||
| 83 | 'name' => 'language', |
||
| 84 | 'contents' => $this->locale |
||
| 85 | ]; |
||
| 86 | |||
| 87 | View Code Duplication | if (null !== $this->branch) { |
|
| 88 | $data[] = [ |
||
| 89 | 'name' => 'branch', |
||
| 90 | 'contents' => $this->branch |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | |||
| 94 | foreach ($this->translations as $translation) { |
||
| 95 | $data[] = [ |
||
| 96 | 'name' => 'files['.$translation->getCrowdinPath().']', |
||
| 97 | 'contents' => $this->fileReader->readTranslation($translation) |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | $data = ['multipart' => $data]; |
||
| 102 | $response = $this->client->getHttpClient()->post($path, $data); |
||
| 103 | |||
| 104 | return $response->getBody(); |
||
| 105 | } |
||
| 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
$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.