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 MaskGenerator |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Language lookup. |
||
| 20 | * |
||
| 21 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
||
| 22 | */ |
||
| 23 | protected $languageHandler; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Creates a new Language MaskGenerator. |
||
| 27 | * |
||
| 28 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 29 | */ |
||
| 30 | public function __construct(LanguageHandler $languageHandler) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Generates a language mask from the keys of $languages. |
||
| 37 | * |
||
| 38 | * @deprecated Move towards using {@see generateLanguageMaskFromLanguageMap()} or the other specific methods. |
||
| 39 | * |
||
| 40 | * @param array $languages |
||
| 41 | * |
||
| 42 | * @return int |
||
| 43 | */ |
||
| 44 | public function generateLanguageMask(array $languages) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generates a language mask from pre-loaded Language Ids. |
||
| 51 | * |
||
| 52 | * @param int[] $languageIds |
||
| 53 | * @param bool $alwaysAvailable |
||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable): int |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Generates a language mask from the keys of $languageMap. |
||
| 71 | * |
||
| 72 | * Typically used for ->name values to get language mask directly from such structure. |
||
| 73 | * |
||
| 74 | * @param array $languageMap Key values are language code, value is ignored. |
||
| 75 | * Exception if 'always-available' key with language value is set, then always available flag is added. |
||
| 76 | * |
||
| 77 | * @return int |
||
| 78 | */ |
||
| 79 | public function generateLanguageMaskFromLanguageMap(array $languageMap): int |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Generates a language mask from plain array of language codes and always available flag. |
||
| 97 | * |
||
| 98 | * @param string[] $languageCodes |
||
| 99 | * @param bool $isAlwaysAvailable |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable = false): int |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Generates a language indicator from $languageCode and $alwaysAvailable. |
||
| 117 | * |
||
| 118 | * @param string $languageCode |
||
| 119 | * @param bool $alwaysAvailable |
||
| 120 | * |
||
| 121 | * @return int |
||
| 122 | */ |
||
| 123 | public function generateLanguageIndicator($languageCode, $alwaysAvailable) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Checks if $language is always available in $languages;. |
||
| 130 | * |
||
| 131 | * @param string $language |
||
| 132 | * @param array $languages |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function isLanguageAlwaysAvailable($language, array $languages): bool |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Checks if $languageMask contains the alwaysAvailable bit field. |
||
| 145 | * |
||
| 146 | * @param int $languageMask |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isAlwaysAvailable($languageMask): bool |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId. |
||
| 157 | * |
||
| 158 | * @param int $languageId |
||
| 159 | * |
||
| 160 | * @return int |
||
| 161 | */ |
||
| 162 | public function removeAlwaysAvailableFlag($languageId): int |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Extracts every language Ids contained in $languageMask. |
||
| 169 | * |
||
| 170 | * @param int $languageMask |
||
| 171 | * |
||
| 172 | * @return array Array of language Id |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function extractLanguageIdsFromMask($languageMask): array |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Extracts Language codes contained in given $languageMask. |
||
| 193 | * |
||
| 194 | * @param int $languageMask |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function extractLanguageCodesFromMask($languageMask): array |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Checks if given $languageMask consists of multiple languages. |
||
| 213 | * |
||
| 214 | * @param int $languageMask |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function isLanguageMaskComposite($languageMask): bool |
||
| 231 | } |
||
| 232 |
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.