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 |
||
| 30 | class Downloads implements IProvider { |
||
| 31 | |||
| 32 | /** @var IL10N */ |
||
| 33 | protected $l; |
||
| 34 | |||
| 35 | /** @var IURLGenerator */ |
||
| 36 | protected $url; |
||
| 37 | |||
| 38 | /** @var IManager */ |
||
| 39 | protected $activityManager; |
||
| 40 | |||
| 41 | const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded'; |
||
| 42 | const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded'; |
||
| 43 | |||
| 44 | const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded'; |
||
| 45 | const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param IL10N $l |
||
| 49 | * @param IURLGenerator $url |
||
| 50 | * @param IManager $activityManager |
||
| 51 | */ |
||
| 52 | public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param IEvent $event |
||
| 60 | * @param IEvent|null $previousEvent |
||
| 61 | * @return IEvent |
||
| 62 | * @throws \InvalidArgumentException |
||
| 63 | * @since 11.0.0 |
||
| 64 | */ |
||
| 65 | public function parse(IEvent $event, IEvent $previousEvent = null) { |
||
| 66 | if ($event->getApp() !== 'files_sharing') { |
||
| 67 | throw new \InvalidArgumentException(); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($this->activityManager->isFormattingFilteredObject()) { |
||
| 71 | try { |
||
| 72 | return $this->parseShortVersion($event); |
||
| 73 | } catch (\InvalidArgumentException $e) { |
||
| 74 | // Ignore and simply use the long version... |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return $this->parseLongVersion($event); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param IEvent $event |
||
| 83 | * @return IEvent |
||
| 84 | * @throws \InvalidArgumentException |
||
| 85 | * @since 11.0.0 |
||
| 86 | */ |
||
| 87 | public function parseShortVersion(IEvent $event) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param IEvent $event |
||
| 110 | * @return IEvent |
||
| 111 | * @throws \InvalidArgumentException |
||
| 112 | * @since 11.0.0 |
||
| 113 | */ |
||
| 114 | public function parseLongVersion(IEvent $event) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param IEvent $event |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | protected function getParsedParameters(IEvent $event) { |
||
| 149 | $subject = $event->getSubject(); |
||
| 150 | $parameters = $event->getSubjectParameters(); |
||
| 151 | |||
| 152 | switch ($subject) { |
||
| 153 | case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED: |
||
| 154 | case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED: |
||
| 155 | return [ |
||
| 156 | 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]), |
||
| 157 | ]; |
||
| 158 | case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED: |
||
| 159 | case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED: |
||
| 160 | return [ |
||
| 161 | 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]), |
||
| 162 | 'email' => [ |
||
| 163 | 'type' => 'email', |
||
| 164 | 'id' => $parameters[1], |
||
| 165 | 'name' => $parameters[1], |
||
| 166 | ], |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | |||
| 170 | throw new \InvalidArgumentException(); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $id |
||
| 175 | * @param string $path |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | View Code Duplication | protected function generateFileParameter($id, $path) { |
|
| 187 | } |
||
| 188 |
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.