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 FilesPlugin 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 FilesPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class FilesPlugin extends ServerPlugin { |
||
| 48 | |||
| 49 | // namespace |
||
| 50 | const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
||
| 51 | const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
||
| 52 | const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid'; |
||
| 53 | const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions'; |
||
| 54 | const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions'; |
||
| 55 | const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL'; |
||
| 56 | const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size'; |
||
| 57 | const GETETAG_PROPERTYNAME = '{DAV:}getetag'; |
||
| 58 | const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified'; |
||
| 59 | const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id'; |
||
| 60 | const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name'; |
||
| 61 | const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums'; |
||
| 62 | const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Reference to main server object |
||
| 66 | * |
||
| 67 | * @var \Sabre\DAV\Server |
||
| 68 | */ |
||
| 69 | private $server; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Tree |
||
| 73 | */ |
||
| 74 | private $tree; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Whether this is public webdav. |
||
| 78 | * If true, some returned information will be stripped off. |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $isPublic; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var View |
||
| 86 | */ |
||
| 87 | private $fileView; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $downloadAttachment; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var IConfig |
||
| 96 | */ |
||
| 97 | private $config; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var IRequest |
||
| 101 | */ |
||
| 102 | private $request; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Tree $tree |
||
| 106 | * @param View $view |
||
| 107 | * @param IConfig $config |
||
| 108 | * @param IRequest $request |
||
| 109 | * @param bool $isPublic |
||
| 110 | * @param bool $downloadAttachment |
||
| 111 | */ |
||
| 112 | public function __construct(Tree $tree, |
||
| 113 | View $view, |
||
| 114 | IConfig $config, |
||
| 115 | IRequest $request, |
||
| 116 | $isPublic = false, |
||
| 117 | $downloadAttachment = true) { |
||
| 118 | $this->tree = $tree; |
||
| 119 | $this->fileView = $view; |
||
| 120 | $this->config = $config; |
||
| 121 | $this->request = $request; |
||
| 122 | $this->isPublic = $isPublic; |
||
| 123 | $this->downloadAttachment = $downloadAttachment; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This initializes the plugin. |
||
| 128 | * |
||
| 129 | * This function is called by \Sabre\DAV\Server, after |
||
| 130 | * addPlugin is called. |
||
| 131 | * |
||
| 132 | * This method should set up the required event subscriptions. |
||
| 133 | * |
||
| 134 | * @param \Sabre\DAV\Server $server |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function initialize(\Sabre\DAV\Server $server) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Plugin that checks if a move can actually be performed. |
||
| 173 | * |
||
| 174 | * @param string $source source path |
||
| 175 | * @param string $destination destination path |
||
| 176 | * @throws Forbidden |
||
| 177 | * @throws NotFound |
||
| 178 | */ |
||
| 179 | function checkMove($source, $destination) { |
||
| 180 | $sourceNode = $this->tree->getNodeForPath($source); |
||
| 181 | if (!$sourceNode instanceof Node) { |
||
| 182 | return; |
||
| 183 | } |
||
| 184 | list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($source); |
||
| 185 | list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destination); |
||
| 186 | |||
| 187 | if ($sourceDir !== $destinationDir) { |
||
| 188 | $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
||
| 189 | if (is_null($sourceNodeFileInfo)) { |
||
| 190 | throw new NotFound($source . ' does not exist'); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!$sourceNodeFileInfo->isDeletable()) { |
||
| 194 | throw new Forbidden($source . " cannot be deleted"); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * This sets a cookie to be able to recognize the start of the download |
||
| 201 | * the content must not be longer than 32 characters and must only contain |
||
| 202 | * alphanumeric characters |
||
| 203 | * |
||
| 204 | * @param RequestInterface $request |
||
| 205 | * @param ResponseInterface $response |
||
| 206 | */ |
||
| 207 | function handleDownloadToken(RequestInterface $request, ResponseInterface $response) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add headers to file download |
||
| 227 | * |
||
| 228 | * @param RequestInterface $request |
||
| 229 | * @param ResponseInterface $response |
||
| 230 | */ |
||
| 231 | function httpGet(RequestInterface $request, ResponseInterface $response) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Adds all ownCloud-specific properties |
||
| 264 | * |
||
| 265 | * @param PropFind $propFind |
||
| 266 | * @param \Sabre\DAV\INode $node |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Update ownCloud-specific properties |
||
| 356 | * |
||
| 357 | * @param string $path |
||
| 358 | * @param PropPatch $propPatch |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function handleUpdateProperties($path, PropPatch $propPatch) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $filePath |
||
| 391 | * @param \Sabre\DAV\INode $node |
||
| 392 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 393 | */ |
||
| 394 | public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) { |
||
| 416 | |||
| 417 | } |
||
| 418 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.