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 |
||
| 34 | class Manager implements IManager { |
||
| 35 | /** @var IValidator */ |
||
| 36 | protected $validator; |
||
| 37 | |||
| 38 | /** @var IApp[] */ |
||
| 39 | protected $apps; |
||
| 40 | |||
| 41 | /** @var INotifier[] */ |
||
| 42 | protected $notifiers; |
||
| 43 | |||
| 44 | /** @var array[] */ |
||
| 45 | protected $notifiersInfo; |
||
| 46 | |||
| 47 | /** @var \Closure[] */ |
||
| 48 | protected $appsClosures; |
||
| 49 | |||
| 50 | /** @var \Closure[] */ |
||
| 51 | protected $notifiersClosures; |
||
| 52 | |||
| 53 | /** @var \Closure[] */ |
||
| 54 | protected $notifiersInfoClosures; |
||
| 55 | |||
| 56 | /** @var bool */ |
||
| 57 | protected $preparingPushNotification; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Manager constructor. |
||
| 61 | * |
||
| 62 | * @param IValidator $validator |
||
| 63 | */ |
||
| 64 | public function __construct(IValidator $validator) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \Closure $service The service must implement IApp, otherwise a |
||
| 77 | * \InvalidArgumentException is thrown later |
||
| 78 | * @since 8.2.0 |
||
| 79 | */ |
||
| 80 | public function registerApp(\Closure $service) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param \Closure $service The service must implement INotifier, otherwise a |
||
| 87 | * \InvalidArgumentException is thrown later |
||
| 88 | * @param \Closure $info An array with the keys 'id' and 'name' containing |
||
| 89 | * the app id and the app name |
||
| 90 | * @since 8.2.0 - Parameter $info was added in 9.0.0 |
||
| 91 | */ |
||
| 92 | public function registerNotifier(\Closure $service, \Closure $info) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return IApp[] |
||
| 101 | */ |
||
| 102 | View Code Duplication | protected function getApps(): array { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return INotifier[] |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function getNotifiers(): array { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return array[] |
||
| 141 | */ |
||
| 142 | public function listNotifiers(): array { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return INotification |
||
| 164 | * @since 8.2.0 |
||
| 165 | */ |
||
| 166 | public function createNotification(): INotification { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return bool |
||
| 172 | * @since 8.2.0 |
||
| 173 | */ |
||
| 174 | public function hasNotifiers(): bool { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param bool $preparingPushNotification |
||
| 180 | * @since 14.0.0 |
||
| 181 | */ |
||
| 182 | public function setPreparingPushNotification($preparingPushNotification) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return bool |
||
| 188 | * @since 14.0.0 |
||
| 189 | */ |
||
| 190 | public function isPreparingPushNotification(): bool { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param INotification $notification |
||
| 196 | * @throws \InvalidArgumentException When the notification is not valid |
||
| 197 | * @since 8.2.0 |
||
| 198 | */ |
||
| 199 | public function notify(INotification $notification) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param INotification $notification |
||
| 216 | * @param string $languageCode The code of the language that should be used to prepare the notification |
||
| 217 | * @return INotification |
||
| 218 | * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
||
| 219 | * @since 8.2.0 |
||
| 220 | */ |
||
| 221 | public function prepare(INotification $notification, $languageCode): INotification { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param INotification $notification |
||
| 245 | */ |
||
| 246 | public function markProcessed(INotification $notification) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param INotification $notification |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function getCount(INotification $notification): int { |
||
| 268 | } |
||
| 269 |