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 |
||
| 21 | class Handler implements BaseSectionHandler |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Section Gateway. |
||
| 25 | * |
||
| 26 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Section\Gateway |
||
| 27 | */ |
||
| 28 | protected $sectionGateway; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new Section Handler. |
||
| 32 | * |
||
| 33 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Section\Gateway $sectionGateway |
||
| 34 | */ |
||
| 35 | public function __construct(Gateway $sectionGateway) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a new section. |
||
| 42 | * |
||
| 43 | * @param string $name |
||
| 44 | * @param string $identifier |
||
| 45 | * |
||
| 46 | * @return \eZ\Publish\SPI\Persistence\Content\Section |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function create($name, $identifier) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Update name and identifier of a section. |
||
| 62 | * |
||
| 63 | * @param mixed $id |
||
| 64 | * @param string $name |
||
| 65 | * @param string $identifier |
||
| 66 | * |
||
| 67 | * @return \eZ\Publish\SPI\Persistence\Content\Section |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function update($id, $name, $identifier) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get section data. |
||
| 83 | * |
||
| 84 | * @param mixed $id |
||
| 85 | * |
||
| 86 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If section is not found |
||
| 87 | * |
||
| 88 | * @return \eZ\Publish\SPI\Persistence\Content\Section |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function load($id) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Get all section data. |
||
| 103 | * |
||
| 104 | * @return \eZ\Publish\SPI\Persistence\Content\Section[] |
||
| 105 | */ |
||
| 106 | public function loadAll() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get section data by identifier. |
||
| 115 | * |
||
| 116 | * @param string $identifier |
||
| 117 | * |
||
| 118 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If section is not found |
||
| 119 | * |
||
| 120 | * @return \eZ\Publish\SPI\Persistence\Content\Section |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function loadByIdentifier($identifier) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Creates a Section from the given $data. |
||
| 135 | * |
||
| 136 | * @param array $data |
||
| 137 | * |
||
| 138 | * @return \eZ\Publish\SPI\Persistence\Content\Section |
||
| 139 | */ |
||
| 140 | protected function createSectionFromArray(array $data) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Creates a Section from the given $data. |
||
| 153 | * |
||
| 154 | * @param array $data |
||
| 155 | * |
||
| 156 | * @return \eZ\Publish\SPI\Persistence\Content\Section[] |
||
| 157 | */ |
||
| 158 | protected function createSectionsFromArray(array $data) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Delete a section. |
||
| 170 | * |
||
| 171 | * Might throw an exception if the section is still associated with some |
||
| 172 | * content objects. Make sure that no content objects are associated with |
||
| 173 | * the section any more *before* calling this method. |
||
| 174 | * |
||
| 175 | * @param mixed $id |
||
| 176 | */ |
||
| 177 | public function delete($id) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Assigns section to single content object. |
||
| 191 | * |
||
| 192 | * @param mixed $sectionId |
||
| 193 | * @param mixed $contentId |
||
| 194 | */ |
||
| 195 | public function assign($sectionId, $contentId) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Number of content assignments a Section has. |
||
| 202 | * |
||
| 203 | * @param mixed $sectionId |
||
| 204 | * |
||
| 205 | * @return int |
||
| 206 | */ |
||
| 207 | public function assignmentsCount($sectionId) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Number of role policies using a Section in limitations. |
||
| 214 | * |
||
| 215 | * @param mixed $sectionId |
||
| 216 | * |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function policiesCount($sectionId) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Counts the number of role assignments using section with $sectionId in their limitations. |
||
| 226 | * |
||
| 227 | * @param int $sectionId |
||
| 228 | * |
||
| 229 | * @return int |
||
| 230 | */ |
||
| 231 | public function countRoleAssignmentsUsingSection($sectionId) |
||
| 235 | } |
||
| 236 |