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 |
||
| 29 | abstract class Base implements IProvider { |
||
| 30 | |||
| 31 | /** @var IUserManager */ |
||
| 32 | protected $userManager; |
||
| 33 | |||
| 34 | /** @var string[] cached displayNames - key is the UID and value the displayname */ |
||
| 35 | protected $displayNames = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param IUserManager $userManager |
||
| 39 | */ |
||
| 40 | public function __construct(IUserManager $userManager) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param IEvent $event |
||
| 46 | * @param string $subject |
||
| 47 | * @param array $parameters |
||
| 48 | */ |
||
| 49 | protected function setSubjects(IEvent $event, $subject, array $parameters) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param array $eventData |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | protected function generateObjectParameter($eventData) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param int $id |
||
| 78 | * @param string $name |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | protected function generateCalendarParameter($id, $name) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $id |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function generateGroupParameter($id) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $uid |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | View Code Duplication | protected function generateUserParameter($uid) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $uid |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | protected function getDisplayName($uid) { |
||
| 129 | } |
||
| 130 |
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.