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 FilesReportPlugin 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 FilesReportPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class FilesReportPlugin extends ServerPlugin { |
||
| 45 | |||
| 46 | // namespace |
||
| 47 | const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
||
| 48 | const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; |
||
| 49 | const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Reference to main server object |
||
| 53 | * |
||
| 54 | * @var \Sabre\DAV\Server |
||
| 55 | */ |
||
| 56 | private $server; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Tree |
||
| 60 | */ |
||
| 61 | private $tree; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var View |
||
| 65 | */ |
||
| 66 | private $fileView; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var ISystemTagManager |
||
| 70 | */ |
||
| 71 | private $tagManager; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var ISystemTagObjectMapper |
||
| 75 | */ |
||
| 76 | private $tagMapper; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Manager for private tags |
||
| 80 | * |
||
| 81 | * @var ITagManager |
||
| 82 | */ |
||
| 83 | private $fileTagger; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var IUserSession |
||
| 87 | */ |
||
| 88 | private $userSession; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var IGroupManager |
||
| 92 | */ |
||
| 93 | private $groupManager; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Folder |
||
| 97 | */ |
||
| 98 | private $userFolder; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param Tree $tree |
||
| 102 | * @param View $view |
||
| 103 | * @param ISystemTagManager $tagManager |
||
| 104 | * @param ISystemTagObjectMapper $tagMapper |
||
| 105 | * @param ITagManager $fileTagger manager for private tags |
||
| 106 | * @param IUserSession $userSession |
||
| 107 | * @param IGroupManager $groupManager |
||
| 108 | * @param Folder $userfolder |
||
|
|
|||
| 109 | */ |
||
| 110 | public function __construct(Tree $tree, |
||
| 128 | |||
| 129 | /** |
||
| 130 | * This initializes the plugin. |
||
| 131 | * |
||
| 132 | * This function is called by \Sabre\DAV\Server, after |
||
| 133 | * addPlugin is called. |
||
| 134 | * |
||
| 135 | * This method should set up the required event subscriptions. |
||
| 136 | * |
||
| 137 | * @param \Sabre\DAV\Server $server |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function initialize(\Sabre\DAV\Server $server) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns a list of reports this plugin supports. |
||
| 150 | * |
||
| 151 | * This will be used in the {DAV:}supported-report-set property. |
||
| 152 | * |
||
| 153 | * @param string $uri |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getSupportedReportSet($uri) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * REPORT operations to look for files |
||
| 162 | * |
||
| 163 | * @param string $reportName |
||
| 164 | * @param [] $report |
||
| 165 | * @param string $uri |
||
| 166 | * @return bool |
||
| 167 | * @throws NotFound |
||
| 168 | * @throws ReportNotSupported |
||
| 169 | */ |
||
| 170 | public function onReport($reportName, $report, $uri) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Find file ids matching the given filter rules |
||
| 224 | * |
||
| 225 | * @param array $filterRules |
||
| 226 | * @return array array of unique file id results |
||
| 227 | * |
||
| 228 | * @throws TagNotFoundException whenever a tag was not found |
||
| 229 | */ |
||
| 230 | protected function processFilterRules($filterRules) { |
||
| 262 | |||
| 263 | private function getSystemTagFileIds($systemTagIds) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Prepare propfind response for the given nodes |
||
| 308 | * |
||
| 309 | * @param string[] $requestedProps requested properties |
||
| 310 | * @param Node[] nodes nodes for which to fetch and prepare responses |
||
| 311 | * @return Response[] |
||
| 312 | */ |
||
| 313 | public function prepareResponses($requestedProps, $nodes) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Find Sabre nodes by file ids |
||
| 339 | * |
||
| 340 | * @param Node $rootNode root node for search |
||
| 341 | * @param array $fileIds file ids |
||
| 342 | * @return Node[] array of Sabre nodes |
||
| 343 | */ |
||
| 344 | public function findNodesByFileIds($rootNode, $fileIds) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns whether the currently logged in user is an administrator |
||
| 368 | */ |
||
| 369 | View Code Duplication | private function isAdmin() { |
|
| 376 | } |
||
| 377 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.