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 |
||
| 42 | class CirclesService { |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $userId; |
||
| 46 | |||
| 47 | /** @var IL10N */ |
||
| 48 | private $l10n; |
||
| 49 | |||
| 50 | /** @var ConfigService */ |
||
| 51 | private $configService; |
||
| 52 | |||
| 53 | /** @var CirclesMapper */ |
||
| 54 | private $dbCircles; |
||
| 55 | |||
| 56 | /** @var MembersMapper */ |
||
| 57 | private $dbMembers; |
||
| 58 | |||
| 59 | /** @var MiscService */ |
||
| 60 | private $miscService; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * CirclesService constructor. |
||
| 65 | * |
||
| 66 | * @param $userId |
||
| 67 | * @param IL10N $l10n |
||
| 68 | * @param ConfigService $configService |
||
| 69 | * @param DatabaseService $databaseService |
||
| 70 | * @param MiscService $miscService |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Create circle using this->userId as owner |
||
| 91 | * |
||
| 92 | * @param int $type |
||
| 93 | * @param string $name |
||
| 94 | * |
||
| 95 | * @return Circle |
||
| 96 | * @throws CircleTypeDisabledException |
||
| 97 | * @throws \Exception |
||
| 98 | */ |
||
| 99 | public function createCircle($type, $name) { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 133 | * |
||
| 134 | * @param $type |
||
| 135 | * @param string $name |
||
| 136 | * @param int $level |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | * @throws CircleTypeDisabledException |
||
| 140 | */ |
||
| 141 | public function listCircles($type, $name = '', $level = 0) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * returns details on circle and its members if this->userId is a member itself. |
||
| 162 | * |
||
| 163 | * @param $circleId |
||
| 164 | * |
||
| 165 | * @return Circle |
||
| 166 | * @throws \Exception |
||
| 167 | * @internal param $circleId |
||
| 168 | * @internal param string $iError |
||
| 169 | */ |
||
| 170 | public function detailsCircle($circleId) { |
||
| 171 | |||
| 172 | try { |
||
| 173 | $circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
||
| 174 | if ($circle->getUser() |
||
| 175 | ->isLevel(Member::LEVEL_MEMBER) |
||
| 176 | ) { |
||
| 177 | $members = $this->dbMembers->getMembersFromCircle( |
||
| 178 | $circleId, $circle->getUser() |
||
| 179 | ); |
||
| 180 | $circle->setMembers($members); |
||
| 181 | } |
||
| 182 | } catch (\Exception $e) { |
||
| 183 | throw $e; |
||
| 184 | } |
||
| 185 | |||
| 186 | return $circle; |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Join a circle. |
||
| 192 | * |
||
| 193 | * @param $circleId |
||
| 194 | * |
||
| 195 | * @return null|Member |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function joinCircle($circleId) { |
|
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Leave a circle. |
||
| 224 | * |
||
| 225 | * @param $circleId |
||
| 226 | * |
||
| 227 | * @return null|Member |
||
| 228 | * @throws \Exception |
||
| 229 | */ |
||
| 230 | public function leaveCircle($circleId) { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * destroy a circle. |
||
| 255 | * |
||
| 256 | * @param int $circleId |
||
| 257 | * |
||
| 258 | * @throws MemberIsNotOwnerException |
||
| 259 | */ |
||
| 260 | public function removeCircle($circleId) { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $circleName |
||
| 277 | * |
||
| 278 | * @return Circle|null |
||
| 279 | */ |
||
| 280 | public function infoCircleByName($circleName) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Convert a Type in String to its Bit Value |
||
| 286 | * |
||
| 287 | * @param $type |
||
| 288 | * |
||
| 289 | * @return int |
||
|
|
|||
| 290 | */ |
||
| 291 | public static function convertTypeStringToBitValue(& $type) { |
||
| 308 | |||
| 309 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.