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 Provider implements IProvider { |
||
| 34 | |||
| 35 | const PASSWORD_CHANGED_BY = 'password_changed_by'; |
||
| 36 | const PASSWORD_CHANGED_SELF = 'password_changed_self'; |
||
| 37 | const PASSWORD_RESET = 'password_changed'; |
||
| 38 | const EMAIL_CHANGED_BY = 'email_changed_by'; |
||
| 39 | const EMAIL_CHANGED_SELF = 'email_changed_self'; |
||
| 40 | const EMAIL_CHANGED = 'email_changed'; |
||
| 41 | |||
| 42 | /** @var IFactory */ |
||
| 43 | protected $languageFactory; |
||
| 44 | |||
| 45 | /** @var IL10N */ |
||
| 46 | protected $l; |
||
| 47 | |||
| 48 | /** @var IURLGenerator */ |
||
| 49 | protected $url; |
||
| 50 | |||
| 51 | /** @var IUserManager */ |
||
| 52 | protected $userManager; |
||
| 53 | |||
| 54 | /** @var IManager */ |
||
| 55 | private $activityManager; |
||
| 56 | |||
| 57 | /** @var string[] cached displayNames - key is the UID and value the displayname */ |
||
| 58 | protected $displayNames = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param IFactory $languageFactory |
||
| 62 | * @param IURLGenerator $url |
||
| 63 | * @param IUserManager $userManager |
||
| 64 | * @param IManager $activityManager |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function __construct(IFactory $languageFactory, IURLGenerator $url, IUserManager $userManager, IManager $activityManager) { |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $language |
||
| 75 | * @param IEvent $event |
||
| 76 | * @param IEvent|null $previousEvent |
||
| 77 | * @return IEvent |
||
| 78 | * @throws \InvalidArgumentException |
||
| 79 | * @since 11.0.0 |
||
| 80 | */ |
||
| 81 | public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param IEvent $event |
||
| 120 | * @return array |
||
| 121 | * @throws \InvalidArgumentException |
||
| 122 | */ |
||
| 123 | protected function getParameters(IEvent $event) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param IEvent $event |
||
| 145 | * @param string $subject |
||
| 146 | * @param array $parameters |
||
| 147 | * @throws \InvalidArgumentException |
||
| 148 | */ |
||
| 149 | View Code Duplication | protected function setSubjects(IEvent $event, $subject, array $parameters) { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $uid |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | View Code Duplication | protected function generateUserParameter($uid) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $uid |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | protected function getDisplayName($uid) { |
||
| 188 | } |
||
| 189 |
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.