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:
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Manager implements IManager { |
||
| 36 | /** @var IRequest */ |
||
| 37 | protected $request; |
||
| 38 | |||
| 39 | /** @var IUserSession */ |
||
| 40 | protected $session; |
||
| 41 | |||
| 42 | /** @var IConfig */ |
||
| 43 | protected $config; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | protected $formattingObjectType; |
||
| 47 | |||
| 48 | /** @var int */ |
||
| 49 | protected $formattingObjectId; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | protected $currentUserId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * constructor of the controller |
||
| 56 | * |
||
| 57 | * @param IRequest $request |
||
| 58 | * @param IUserSession $session |
||
| 59 | * @param IConfig $config |
||
| 60 | */ |
||
| 61 | public function __construct(IRequest $request, |
||
| 68 | |||
| 69 | /** @var \Closure[] */ |
||
| 70 | private $consumersClosures = array(); |
||
| 71 | |||
| 72 | /** @var IConsumer[] */ |
||
| 73 | private $consumers = array(); |
||
| 74 | |||
| 75 | /** @var \Closure[] */ |
||
| 76 | private $extensionsClosures = array(); |
||
| 77 | |||
| 78 | /** @var IExtension[] */ |
||
| 79 | private $extensions = array(); |
||
| 80 | |||
| 81 | /** @var array list of filters "name" => "is valid" */ |
||
| 82 | protected $validFilters = array( |
||
| 83 | 'all' => true, |
||
| 84 | 'by' => true, |
||
| 85 | 'self' => true, |
||
| 86 | ); |
||
| 87 | |||
| 88 | /** @var array list of type icons "type" => "css class" */ |
||
| 89 | protected $typeIcons = array(); |
||
| 90 | |||
| 91 | /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */ |
||
| 92 | protected $specialParameters = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return \OCP\Activity\IConsumer[] |
||
| 96 | */ |
||
| 97 | View Code Duplication | protected function getConsumers() { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @return \OCP\Activity\IExtension[] |
||
| 117 | */ |
||
| 118 | View Code Duplication | protected function getExtensions() { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Generates a new IEvent object |
||
| 138 | * |
||
| 139 | * Make sure to call at least the following methods before sending it to the |
||
| 140 | * app with via the publish() method: |
||
| 141 | * - setApp() |
||
| 142 | * - setType() |
||
| 143 | * - setAffectedUser() |
||
| 144 | * - setSubject() |
||
| 145 | * |
||
| 146 | * @return IEvent |
||
| 147 | */ |
||
| 148 | public function generateEvent() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Publish an event to the activity consumers |
||
| 154 | * |
||
| 155 | * Make sure to call at least the following methods before sending an Event: |
||
| 156 | * - setApp() |
||
| 157 | * - setType() |
||
| 158 | * - setAffectedUser() |
||
| 159 | * - setSubject() |
||
| 160 | * |
||
| 161 | * @param IEvent $event |
||
| 162 | * @return null |
||
| 163 | * @throws \BadMethodCallException if required values have not been set |
||
| 164 | */ |
||
| 165 | public function publish(IEvent $event) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $app The app where this event is associated with |
||
| 196 | * @param string $subject A short description of the event |
||
| 197 | * @param array $subjectParams Array with parameters that are filled in the subject |
||
| 198 | * @param string $message A longer description of the event |
||
| 199 | * @param array $messageParams Array with parameters that are filled in the message |
||
| 200 | * @param string $file The file including path where this event is associated with |
||
| 201 | * @param string $link A link where this event is associated with |
||
| 202 | * @param string $affectedUser Recipient of the activity |
||
| 203 | * @param string $type Type of the notification |
||
| 204 | * @param int $priority Priority of the notification |
||
| 205 | * @return null |
||
| 206 | */ |
||
| 207 | public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * In order to improve lazy loading a closure can be registered which will be called in case |
||
| 222 | * activity consumers are actually requested |
||
| 223 | * |
||
| 224 | * $callable has to return an instance of OCA\Activity\IConsumer |
||
| 225 | * |
||
| 226 | * @param \Closure $callable |
||
| 227 | */ |
||
| 228 | public function registerConsumer(\Closure $callable) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * In order to improve lazy loading a closure can be registered which will be called in case |
||
| 235 | * activity consumers are actually requested |
||
| 236 | * |
||
| 237 | * $callable has to return an instance of OCA\Activity\IConsumer |
||
| 238 | * |
||
| 239 | * @param \Closure $callable |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function registerExtension(\Closure $callable) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Will return additional notification types as specified by other apps |
||
| 249 | * |
||
| 250 | * @param string $languageCode |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function getNotificationTypes($languageCode) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $method |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function getDefaultTypes($method) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $type |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getTypeIcon($type) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $type |
||
| 315 | * @param string $id |
||
| 316 | */ |
||
| 317 | public function setFormattingObject($type, $id) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function isFormattingFilteredObject() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $app |
||
| 333 | * @param string $text |
||
| 334 | * @param array $params |
||
| 335 | * @param boolean $stripPath |
||
| 336 | * @param boolean $highlightParams |
||
| 337 | * @param string $languageCode |
||
| 338 | * @return string|false |
||
| 339 | */ |
||
| 340 | public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $app |
||
| 353 | * @param string $text |
||
| 354 | * @return array|false |
||
| 355 | */ |
||
| 356 | public function getSpecialParameterList($app, $text) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param array $activity |
||
| 379 | * @return integer|false |
||
| 380 | */ |
||
| 381 | public function getGroupParameter($activity) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function getNavigation() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $filterValue |
||
| 413 | * @return boolean |
||
| 414 | */ |
||
| 415 | public function isFilterValid($filterValue) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param array $types |
||
| 433 | * @param string $filter |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function filterNotificationTypes($types, $filter) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $filter |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function getQueryForFilter($filter) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set the user we need to use |
||
| 482 | * |
||
| 483 | * @param string|null $currentUserId |
||
| 484 | * @throws \UnexpectedValueException If the user is invalid |
||
| 485 | */ |
||
| 486 | public function setCurrentUserId($currentUserId) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the user we need to use |
||
| 495 | * |
||
| 496 | * Either the user is logged in, or we try to get it from the token |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
||
| 500 | */ |
||
| 501 | public function getCurrentUserId() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Get the user for the token |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
||
| 516 | */ |
||
| 517 | protected function getUserFromToken() { |
||
| 533 | } |
||
| 534 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.