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 | * @return Translation[]  | 
            ||
| 126 | */  | 
            ||
| 127 | public function getTranslations()  | 
            ||
| 131 | |||
| 132 | /**  | 
            ||
| 133 | * @param bool $importsAutoApproved  | 
            ||
| 134 | *  | 
            ||
| 135 | * @throws InvalidArgumentException  | 
            ||
| 136 | *  | 
            ||
| 137 | * @return UploadTranslation  | 
            ||
| 138 | */  | 
            ||
| 139 | public function setImportsAutoApproved($importsAutoApproved)  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * @return bool  | 
            ||
| 152 | */  | 
            ||
| 153 | public function areImportsAutoApproved()  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * @param bool $duplicatesImported  | 
            ||
| 160 | *  | 
            ||
| 161 | * @throws InvalidArgumentException  | 
            ||
| 162 | *  | 
            ||
| 163 | * @return UploadTranslation  | 
            ||
| 164 | */  | 
            ||
| 165 | public function setDuplicatesImported($duplicatesImported)  | 
            ||
| 175 | |||
| 176 | /**  | 
            ||
| 177 | * @return bool  | 
            ||
| 178 | */  | 
            ||
| 179 | public function areDuplicatesImported()  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * @param bool $equalSuggestionsImported  | 
            ||
| 186 | *  | 
            ||
| 187 | * @throws InvalidArgumentException  | 
            ||
| 188 | *  | 
            ||
| 189 | * @return UploadTranslation  | 
            ||
| 190 | */  | 
            ||
| 191 | public function setEqualSuggestionsImported($equalSuggestionsImported)  | 
            ||
| 201 | |||
| 202 | /**  | 
            ||
| 203 | * @return bool  | 
            ||
| 204 | */  | 
            ||
| 205 | public function areEqualSuggestionsImported()  | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * @param string $locale  | 
            ||
| 212 | *  | 
            ||
| 213 | * @return UploadTranslation  | 
            ||
| 214 | */  | 
            ||
| 215 | public function setLocale($locale)  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * @return string  | 
            ||
| 224 | */  | 
            ||
| 225 | public function getLocale()  | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 | * @return string|null  | 
            ||
| 232 | */  | 
            ||
| 233 | public function getBranch()  | 
            ||
| 237 | |||
| 238 | /**  | 
            ||
| 239 | * @param string $branch  | 
            ||
| 240 | *  | 
            ||
| 241 | * @return UploadTranslation  | 
            ||
| 242 | */  | 
            ||
| 243 | public function setBranch($branch)  | 
            ||
| 249 | }  | 
            ||
| 250 | 
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.