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 |
||
| 17 | class MaskGenerator |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Language lookup. |
||
| 21 | * |
||
| 22 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
||
| 23 | */ |
||
| 24 | protected $languageHandler; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Creates a new Language MaskGenerator. |
||
| 28 | * |
||
| 29 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 30 | */ |
||
| 31 | public function __construct(LanguageHandler $languageHandler) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Generates a language mask from the keys of $languages. |
||
| 38 | * |
||
| 39 | * @deprecated Move towards using {@see generateLanguageMaskFromLanguageMap()} or the other specific methods. |
||
| 40 | * |
||
| 41 | * @param array $languages |
||
| 42 | * |
||
| 43 | * @return int |
||
| 44 | */ |
||
| 45 | public function generateLanguageMask(array $languages) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Generates a language mask from pre-loaded Language Ids. |
||
| 52 | * |
||
| 53 | * @param int[] $languageIds |
||
| 54 | * @param bool $alwaysAvailable |
||
| 55 | * |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable): int |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Generates a language mask from the keys of $languageMap. |
||
| 72 | * |
||
| 73 | * Typically used for ->name values to get language mask directly from such structure. |
||
| 74 | * |
||
| 75 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language(s) in $languageMap was not be found |
||
| 76 | * |
||
| 77 | * @param array $languageMap Key values are language code, value is ignored. |
||
| 78 | * Exception if 'always-available' key with language value is set, then always available flag is added. |
||
| 79 | * |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function generateLanguageMaskFromLanguageMap(array $languageMap): int |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Generates a language mask from plain array of language codes and always available flag. |
||
| 105 | * |
||
| 106 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language(s) in $languageCodes was not be found |
||
| 107 | * |
||
| 108 | * @param string[] $languageCodes |
||
| 109 | * @param bool $isAlwaysAvailable |
||
| 110 | * |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | public function generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable = false): int |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Generates a language indicator from $languageCode and $alwaysAvailable. |
||
| 132 | * |
||
| 133 | * @param string $languageCode |
||
| 134 | * @param bool $alwaysAvailable |
||
| 135 | * |
||
| 136 | * @return int |
||
| 137 | * |
||
| 138 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 139 | */ |
||
| 140 | public function generateLanguageIndicator($languageCode, $alwaysAvailable) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Checks if $language is always available in $languages;. |
||
| 147 | * |
||
| 148 | * @param string $language |
||
| 149 | * @param array $languages |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function isLanguageAlwaysAvailable($language, array $languages): bool |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Checks if $languageMask contains the alwaysAvailable bit field. |
||
| 162 | * |
||
| 163 | * @param int $languageMask |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function isAlwaysAvailable($languageMask): bool |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId. |
||
| 174 | * |
||
| 175 | * @param int $languageId |
||
| 176 | * |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | public function removeAlwaysAvailableFlag($languageId): int |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Extracts every language Ids contained in $languageMask. |
||
| 186 | * |
||
| 187 | * @param int $languageMask |
||
| 188 | * |
||
| 189 | * @return array Array of language Id |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function extractLanguageIdsFromMask($languageMask): array |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Extracts Language codes contained in given $languageMask. |
||
| 210 | * |
||
| 211 | * @param int $languageMask |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function extractLanguageCodesFromMask($languageMask): array |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Checks if given $languageMask consists of multiple languages. |
||
| 230 | * |
||
| 231 | * @param int $languageMask |
||
| 232 | * |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function isLanguageMaskComposite($languageMask): bool |
||
| 248 | } |
||
| 249 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.