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 |
||
| 19 | class CachingHandler implements BaseLanguageHandler |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Inner Language handler. |
||
| 23 | * |
||
| 24 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler |
||
| 25 | */ |
||
| 26 | protected $innerHandler; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Language cache. |
||
| 30 | * |
||
| 31 | * @var \eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache |
||
| 32 | */ |
||
| 33 | protected $cache; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Creates a caching handler around $innerHandler. |
||
| 37 | * |
||
| 38 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $innerHandler |
||
| 39 | * @param \eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache $cache |
||
| 40 | */ |
||
| 41 | public function __construct(BaseLanguageHandler $innerHandler, InMemoryCache $cache) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create a new language. |
||
| 49 | * |
||
| 50 | * @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct |
||
| 51 | * |
||
| 52 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 53 | */ |
||
| 54 | public function create(CreateStruct $struct) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Update language. |
||
| 64 | * |
||
| 65 | * @param \eZ\Publish\SPI\Persistence\Content\Language $language |
||
| 66 | */ |
||
| 67 | public function update(Language $language) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get language by id. |
||
| 75 | * |
||
| 76 | * @param mixed $id |
||
| 77 | * |
||
| 78 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id |
||
| 79 | * |
||
| 80 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function load($id) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function loadList(array $ids): iterable |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get language by Language Code (eg: eng-GB). |
||
| 120 | * |
||
| 121 | * @param string $languageCode |
||
| 122 | * |
||
| 123 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode |
||
| 124 | * |
||
| 125 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function loadByLanguageCode($languageCode) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function loadListByLanguageCodes(array $languageCodes): iterable |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get all languages. |
||
| 165 | * |
||
| 166 | * @return \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function loadAll() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Delete a language. |
||
| 181 | * |
||
| 182 | * @param mixed $id |
||
| 183 | */ |
||
| 184 | public function delete($id) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Clear internal in-memory cache. |
||
| 193 | */ |
||
| 194 | public function clearCache(): void |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Helper to store languages in internal in-memory cache with all needed keys. |
||
| 201 | * |
||
| 202 | * @param array $languages |
||
| 203 | * @param string|null $listIndex |
||
| 204 | */ |
||
| 205 | protected function storeCache(array $languages, string $listIndex = null): void |
||
| 218 | } |
||
| 219 |
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.