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 |
||
| 43 | class FilesReportPlugin extends ServerPlugin { |
||
| 44 | |||
| 45 | // namespace |
||
| 46 | const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
||
| 47 | const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; |
||
| 48 | const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Reference to main server object |
||
| 52 | * |
||
| 53 | * @var \Sabre\DAV\Server |
||
| 54 | */ |
||
| 55 | private $server; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Tree |
||
| 59 | */ |
||
| 60 | private $tree; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var View |
||
| 64 | */ |
||
| 65 | private $fileView; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ISystemTagManager |
||
| 69 | */ |
||
| 70 | private $tagManager; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ISystemTagObjectMapper |
||
| 74 | */ |
||
| 75 | private $tagMapper; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Manager for private tags |
||
| 79 | * |
||
| 80 | * @var ITagManager |
||
| 81 | */ |
||
| 82 | private $fileTagger; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var IUserSession |
||
| 86 | */ |
||
| 87 | private $userSession; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var IGroupManager |
||
| 91 | */ |
||
| 92 | private $groupManager; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Folder |
||
| 96 | */ |
||
| 97 | private $userFolder; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Tree $tree |
||
| 101 | * @param View $view |
||
| 102 | * @param ISystemTagManager $tagManager |
||
| 103 | * @param ISystemTagObjectMapper $tagMapper |
||
| 104 | * @param ITagManager $fileTagger manager for private tags |
||
| 105 | * @param IUserSession $userSession |
||
| 106 | * @param IGroupManager $groupManager |
||
| 107 | * @param Folder $userfolder |
||
|
|
|||
| 108 | */ |
||
| 109 | public function __construct(Tree $tree, |
||
| 127 | |||
| 128 | /** |
||
| 129 | * This initializes the plugin. |
||
| 130 | * |
||
| 131 | * This function is called by \Sabre\DAV\Server, after |
||
| 132 | * addPlugin is called. |
||
| 133 | * |
||
| 134 | * This method should set up the required event subscriptions. |
||
| 135 | * |
||
| 136 | * @param \Sabre\DAV\Server $server |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function initialize(\Sabre\DAV\Server $server) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns a list of reports this plugin supports. |
||
| 149 | * |
||
| 150 | * This will be used in the {DAV:}supported-report-set property. |
||
| 151 | * |
||
| 152 | * @param string $uri |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getSupportedReportSet($uri) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * REPORT operations to look for files |
||
| 161 | * |
||
| 162 | * @param string $reportName |
||
| 163 | * @param [] $report |
||
| 164 | * @param string $uri |
||
| 165 | * @return bool |
||
| 166 | * @throws NotFound |
||
| 167 | * @throws ReportNotSupported |
||
| 168 | */ |
||
| 169 | public function onReport($reportName, $report, $uri) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the base uri of the files root by removing |
||
| 224 | * the subpath from the URI |
||
| 225 | * |
||
| 226 | * @param string $uri URI from this request |
||
| 227 | * @param string $subPath subpath to remove from the URI |
||
| 228 | * |
||
| 229 | * @return string files base uri |
||
| 230 | */ |
||
| 231 | private function getFilesBaseUri($uri, $subPath) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Find file ids matching the given filter rules |
||
| 249 | * |
||
| 250 | * @param array $filterRules |
||
| 251 | * @return array array of unique file id results |
||
| 252 | * |
||
| 253 | * @throws TagNotFoundException whenever a tag was not found |
||
| 254 | */ |
||
| 255 | protected function processFilterRules($filterRules) { |
||
| 287 | |||
| 288 | private function getSystemTagFileIds($systemTagIds) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Prepare propfind response for the given nodes |
||
| 333 | * |
||
| 334 | * @param string $filesUri $filesUri URI leading to root of the files URI, |
||
| 335 | * with a leading slash but no trailing slash |
||
| 336 | * @param string[] $requestedProps requested properties |
||
| 337 | * @param Node[] nodes nodes for which to fetch and prepare responses |
||
| 338 | * @return Response[] |
||
| 339 | */ |
||
| 340 | public function prepareResponses($filesUri, $requestedProps, $nodes) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Find Sabre nodes by file ids |
||
| 366 | * |
||
| 367 | * @param Node $rootNode root node for search |
||
| 368 | * @param array $fileIds file ids |
||
| 369 | * @return Node[] array of Sabre nodes |
||
| 370 | */ |
||
| 371 | public function findNodesByFileIds($rootNode, $fileIds) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns whether the currently logged in user is an administrator |
||
| 395 | */ |
||
| 396 | View Code Duplication | private function isAdmin() { |
|
| 403 | } |
||
| 404 |
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.