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 Cache |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Maps IDs to Language objects. |
||
| 21 | * |
||
| 22 | * @var \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 23 | */ |
||
| 24 | protected $mapById = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Maps locales to Language objects. |
||
| 28 | * |
||
| 29 | * @var \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 30 | */ |
||
| 31 | protected $mapByLocale = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Stores the $language into the cache. |
||
| 35 | * |
||
| 36 | * @param \eZ\Publish\SPI\Persistence\Content\Language $language |
||
| 37 | */ |
||
| 38 | public function store(Language $language) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Removes the language with $id from the cache. |
||
| 46 | * |
||
| 47 | * @param mixed $id |
||
| 48 | */ |
||
| 49 | public function remove($id) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns the Language with $id from the cache. |
||
| 61 | * |
||
| 62 | * @param mixed $id |
||
| 63 | * |
||
| 64 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 65 | * |
||
| 66 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 67 | * if the Language could not be found |
||
| 68 | */ |
||
| 69 | public function getById($id) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns Languages with $ids from the cache. |
||
| 80 | * |
||
| 81 | * @param int[] $ids |
||
| 82 | * |
||
| 83 | * @return \eZ\Publish\SPI\Persistence\Content\Language[]|iterable |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function getListById(array $ids): iterable |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the Language with $languageCode from the cache. |
||
| 99 | * |
||
| 100 | * @param string $languageCode |
||
| 101 | * |
||
| 102 | * @return \eZ\Publish\SPI\Persistence\Content\Language |
||
| 103 | * |
||
| 104 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 105 | * if the Language could not be found |
||
| 106 | */ |
||
| 107 | public function getByLocale($languageCode) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns Languages with $languageCodes from the cache. |
||
| 118 | * |
||
| 119 | * @param string[] $languageCodes |
||
| 120 | * |
||
| 121 | * @return \eZ\Publish\SPI\Persistence\Content\Language[]|iterable |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function getListByLocale(array $languageCodes): iterable |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Returns all languages in the cache with locale as key. |
||
| 137 | * |
||
| 138 | * @return \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 139 | */ |
||
| 140 | public function getAll() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * CLear language cache. |
||
| 147 | */ |
||
| 148 | public function clearCache() |
||
| 152 | } |
||
| 153 |