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 | /** @var IFactory */ |
||
| 36 | protected $languageFactory; |
||
| 37 | |||
| 38 | /** @var IURLGenerator */ |
||
| 39 | protected $url; |
||
| 40 | |||
| 41 | /** @var IManager */ |
||
| 42 | protected $activityManager; |
||
| 43 | |||
| 44 | /** @var IUserManager */ |
||
| 45 | protected $userManager; |
||
| 46 | |||
| 47 | /** @var Manager */ |
||
| 48 | protected $manager; |
||
| 49 | |||
| 50 | /** @var string[] */ |
||
| 51 | protected $displayNames = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param IFactory $languageFactory |
||
| 55 | * @param IURLGenerator $url |
||
| 56 | * @param IManager $activityManager |
||
| 57 | * @param IUserManager $userManager |
||
| 58 | * @param Manager $manager |
||
| 59 | */ |
||
| 60 | 1 | View Code Duplication | public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, Manager $manager) { |
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $language |
||
| 70 | * @param IEvent $event |
||
| 71 | * @param IEvent|null $previousEvent |
||
| 72 | * @return IEvent |
||
| 73 | * @throws \InvalidArgumentException |
||
| 74 | * @since 11.0.0 |
||
| 75 | */ |
||
| 76 | public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
||
| 77 | if ($event->getApp() !== 'announcementcenter' || ( |
||
| 78 | $event->getSubject() !== 'announcementsubject' && // 3.1 and later |
||
| 79 | strpos($event->getSubject(), 'announcementsubject#') !== 0) // 3.0 and before |
||
| 80 | ) { |
||
| 81 | throw new \InvalidArgumentException(); |
||
| 82 | } |
||
| 83 | |||
| 84 | $l = $this->languageFactory->get('announcementcenter', $language); |
||
| 85 | |||
| 86 | $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.svg'))); |
||
| 87 | |||
| 88 | try { |
||
| 89 | $announcement = $this->manager->getAnnouncement($event->getObjectId(), true, false, false); |
||
| 90 | |||
| 91 | $parsedParameters = $this->getParameters($event, $announcement); |
||
| 92 | View Code Duplication | if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) { |
|
| 93 | $subject = $l->t('You announced {announcement}'); |
||
| 94 | unset($parsedParameters['actor']); |
||
| 95 | } else { |
||
| 96 | $subject = $l->t('{actor} announced {announcement}'); |
||
| 97 | } |
||
| 98 | $event->setParsedMessage($announcement['message']); |
||
| 99 | } catch (\InvalidArgumentException $e) { |
||
| 100 | $parsedParameters = $this->getParameters($event, []); |
||
| 101 | View Code Duplication | if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) { |
|
| 102 | $subject = $l->t('You posted an announcement'); |
||
| 103 | unset($parsedParameters['actor']); |
||
| 104 | } else { |
||
| 105 | $subject = $l->t('{actor} posted an announcement'); |
||
| 106 | } |
||
| 107 | |||
| 108 | $event->setParsedMessage($l->t('Announcement does not exist anymore')); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | $this->setSubjects($event, $subject, $parsedParameters); |
||
| 113 | |||
| 114 | return $event; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param IEvent $event |
||
| 119 | * @param array $announcement |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | protected function getParameters(IEvent $event, array $announcement) { |
||
| 123 | $parameters = $event->getSubjectParameters(); |
||
| 124 | |||
| 125 | if (!empty($announcement)) { |
||
| 126 | return [ |
||
| 127 | 'actor' => $this->generateUserParameter($parameters[0]), |
||
| 128 | 'announcement' => $this->generateAnnouncementParameter($announcement), |
||
| 129 | ]; |
||
| 130 | } else { |
||
| 131 | return [ |
||
| 132 | 'actor' => $this->generateUserParameter($parameters[0]), |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param IEvent $event |
||
| 139 | * @param string $subject |
||
| 140 | * @param array $parameters |
||
| 141 | */ |
||
| 142 | protected function setSubjects(IEvent $event, $subject, array $parameters) { |
||
| 143 | $placeholders = $replacements = []; |
||
| 144 | foreach ($parameters as $placeholder => $parameter) { |
||
| 145 | $placeholders[] = '{' . $placeholder . '}'; |
||
| 146 | $replacements[] = $parameter['name']; |
||
| 147 | } |
||
| 148 | |||
| 149 | $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
||
| 150 | ->setRichSubject($subject, $parameters); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param array $announcement |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | protected function generateAnnouncementParameter(array $announcement) { |
||
| 158 | return [ |
||
| 159 | 'type' => 'announcement', |
||
| 160 | 'id' => $announcement['id'], |
||
| 161 | 'name' => $announcement['subject'], |
||
| 162 | 'link' => $this->url->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $announcement['id'], |
||
| 163 | ]; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $uid |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function generateUserParameter($uid) { |
||
| 171 | if (!isset($this->displayNames[$uid])) { |
||
| 172 | $this->displayNames[$uid] = $this->getDisplayName($uid); |
||
| 173 | } |
||
| 174 | |||
| 175 | return [ |
||
| 176 | 'type' => 'user', |
||
| 177 | 'id' => $uid, |
||
| 178 | 'name' => $this->displayNames[$uid], |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $uid |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | protected function getDisplayName($uid) { |
||
| 194 | } |
||
| 195 |
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.