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 |
||
| 33 | class Activity implements IProvider { |
||
| 34 | |||
| 35 | /** @var IL10N */ |
||
| 36 | protected $l; |
||
| 37 | |||
| 38 | /** @var IURLGenerator */ |
||
| 39 | protected $url; |
||
| 40 | |||
| 41 | /** @var IManager */ |
||
| 42 | protected $activityManager; |
||
| 43 | |||
| 44 | /** @var IUserManager */ |
||
| 45 | protected $userManager; |
||
| 46 | /** @var IContactsManager */ |
||
| 47 | protected $contactsManager; |
||
| 48 | |||
| 49 | /** @var array */ |
||
| 50 | protected $displayNames = []; |
||
| 51 | |||
| 52 | /** @var array */ |
||
| 53 | protected $contactNames = []; |
||
| 54 | |||
| 55 | const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self'; |
||
| 56 | const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param IL10N $l |
||
| 60 | * @param IURLGenerator $url |
||
| 61 | * @param IManager $activityManager |
||
| 62 | * @param IUserManager $userManager |
||
| 63 | * @param IContactsManager $contactsManager |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) { |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param IEvent $event |
||
| 75 | * @param IEvent|null $previousEvent |
||
| 76 | * @return IEvent |
||
| 77 | * @throws \InvalidArgumentException |
||
| 78 | * @since 11.0.0 |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function parse(IEvent $event, IEvent $previousEvent = null) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param IEvent $event |
||
| 98 | * @return IEvent |
||
| 99 | * @throws \InvalidArgumentException |
||
| 100 | * @since 11.0.0 |
||
| 101 | */ |
||
| 102 | public function parseShortVersion(IEvent $event) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param IEvent $event |
||
| 133 | * @return IEvent |
||
| 134 | * @throws \InvalidArgumentException |
||
| 135 | * @since 11.0.0 |
||
| 136 | */ |
||
| 137 | public function parseLongVersion(IEvent $event) { |
||
| 161 | |||
| 162 | protected function getParsedParameters(IEvent $event) { |
||
| 163 | $subject = $event->getSubject(); |
||
| 164 | $parameters = $event->getSubjectParameters(); |
||
| 165 | |||
| 166 | switch ($subject) { |
||
| 167 | View Code Duplication | case self::SUBJECT_SHARED_EMAIL_SELF: |
|
| 168 | return [ |
||
| 169 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
||
| 170 | 'email' => $this->generateEmailParameter($parameters[1]), |
||
| 171 | ]; |
||
| 172 | View Code Duplication | case self::SUBJECT_SHARED_EMAIL_BY: |
|
| 173 | return [ |
||
| 174 | 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
||
| 175 | 'email' => $this->generateEmailParameter($parameters[1]), |
||
| 176 | 'actor' => $this->generateUserParameter($parameters[2]), |
||
| 177 | ]; |
||
| 178 | } |
||
| 179 | throw new \InvalidArgumentException(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $id |
||
| 184 | * @param string $path |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | View Code Duplication | protected function generateFileParameter($id, $path) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $email |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | protected function generateEmailParameter($email) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $uid |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | View Code Duplication | protected function generateUserParameter($uid) { |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $email |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | protected function getContactName($email) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $uid |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | protected function getDisplayName($uid) { |
||
| 261 | } |
||
| 262 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.