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 generateLanguageMaskFromLanguageCodes()} or the other generate* methods. |
||
| 40 | * |
||
| 41 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language(s) in $languageCodes was not be found |
||
| 42 | * |
||
| 43 | * @param array $languages |
||
| 44 | * |
||
| 45 | * @return int |
||
| 46 | */ |
||
| 47 | public function generateLanguageMask(array $languages) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Generates a language mask from pre-loaded Language Ids. |
||
| 70 | * |
||
| 71 | * @param int[] $languageIds |
||
| 72 | * @param bool $alwaysAvailable |
||
| 73 | * |
||
| 74 | * @return int |
||
| 75 | */ |
||
| 76 | public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable): int |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Generates a language indicator from $languageCode and $alwaysAvailable. |
||
| 90 | * |
||
| 91 | * @param string $languageCode |
||
| 92 | * @param bool $alwaysAvailable |
||
| 93 | * |
||
| 94 | * @return int |
||
| 95 | * |
||
| 96 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 97 | */ |
||
| 98 | public function generateLanguageIndicator($languageCode, $alwaysAvailable) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Checks if $language is always available in $languages;. |
||
| 105 | * |
||
| 106 | * @param string $language |
||
| 107 | * @param array $languages |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isLanguageAlwaysAvailable($language, array $languages): bool |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Checks if $languageMask contains the alwaysAvailable bit field. |
||
| 120 | * |
||
| 121 | * @param int $languageMask |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function isAlwaysAvailable($languageMask): bool |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId. |
||
| 132 | * |
||
| 133 | * @param int $languageId |
||
| 134 | * |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | public function removeAlwaysAvailableFlag($languageId): int |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Extracts every language Ids contained in $languageMask. |
||
| 144 | * |
||
| 145 | * @param int $languageMask |
||
| 146 | * |
||
| 147 | * @return array Array of language Id |
||
| 148 | */ |
||
| 149 | public function extractLanguageIdsFromMask($languageMask): array |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Extracts Language codes contained in given $languageMask. |
||
| 168 | * |
||
| 169 | * @param int $languageMask |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function extractLanguageCodesFromMask($languageMask): array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Checks if given $languageMask consists of multiple languages. |
||
| 188 | * |
||
| 189 | * @param int $languageMask |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isLanguageMaskComposite($languageMask): bool |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Generates a language mask from plain array of language codes and always available flag. |
||
| 209 | * |
||
| 210 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language(s) in $languageCodes was not be found |
||
| 211 | * |
||
| 212 | * @param string[] $languageCodes |
||
| 213 | * @param bool $isAlwaysAvailable |
||
| 214 | * |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | public function generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable = false): int |
||
| 232 | } |
||
| 233 |
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.