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 | * @param array $languages |
||
| 40 | * |
||
| 41 | * @return int |
||
| 42 | */ |
||
| 43 | public function generateLanguageMask(array $languages) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Generates a language mask from pre-loaded Language Ids. |
||
| 65 | * |
||
| 66 | * @param array $languageIds |
||
| 67 | * @param bool $alwaysAvailable |
||
| 68 | * |
||
| 69 | * @return int |
||
| 70 | */ |
||
| 71 | public function generateLanguageMaskFromLanguageIds(array $languageIds, $alwaysAvailable) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Generates a language indicator from $languageCode and $alwaysAvailable. |
||
| 85 | * |
||
| 86 | * @param string $languageCode |
||
| 87 | * @param bool $alwaysAvailable |
||
| 88 | * |
||
| 89 | * @return int |
||
| 90 | */ |
||
| 91 | public function generateLanguageIndicator($languageCode, $alwaysAvailable) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Checks if $language is always available in $languages;. |
||
| 98 | * |
||
| 99 | * @param string $language |
||
| 100 | * @param array $languages |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function isLanguageAlwaysAvailable($language, array $languages) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Checks if $languageMask contains the alwaysAvailable bit field. |
||
| 113 | * |
||
| 114 | * @param int $languageMask |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function isAlwaysAvailable($languageMask) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Removes the alwaysAvailable flag from $languageId and returns cleaned up $languageId. |
||
| 125 | * |
||
| 126 | * @param int $languageId |
||
| 127 | * |
||
| 128 | * @return int |
||
| 129 | */ |
||
| 130 | public function removeAlwaysAvailableFlag($languageId) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Extracts every language Ids contained in $languageMask. |
||
| 137 | * |
||
| 138 | * @param int $languageMask |
||
| 139 | * |
||
| 140 | * @return array Array of language Id |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function extractLanguageIdsFromMask($languageMask) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Extracts Language codes contained in given $languageMask. |
||
| 161 | * |
||
| 162 | * @param int $languageMask |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | public function extractLanguageCodesFromMask($languageMask) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Checks if given $languageMask consists of multiple languages. |
||
| 179 | * |
||
| 180 | * @param int $languageMask |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function isLanguageMaskComposite($languageMask) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $languageCodes |
||
| 200 | * @param bool $isAlwaysAvailable |
||
| 201 | * |
||
| 202 | * @return int |
||
| 203 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 204 | */ |
||
| 205 | public function generateLanguageMaskFromLanguageCodes(array $languageCodes, bool $isAlwaysAvailable = false): int |
||
| 215 | } |
||
| 216 |
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.