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 WebDav 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 WebDav, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait WebDav { |
||
| 11 | use Sharing; |
||
| 12 | |||
| 13 | /** @var string*/ |
||
| 14 | private $davPath = "remote.php/webdav"; |
||
| 15 | /** @var ResponseInterface */ |
||
| 16 | private $response; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @Given /^using dav path "([^"]*)"$/ |
||
| 20 | */ |
||
| 21 | public function usingDavPath($davPath) { |
||
| 24 | |||
| 25 | public function makeDavRequest($user, $method, $path, $headers, $body = null){ |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @Given /^User "([^"]*)" moved file "([^"]*)" to "([^"]*)"$/ |
||
| 50 | * @param string $user |
||
| 51 | * @param string $fileSource |
||
| 52 | * @param string $fileDestination |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function userMovedFile($user, $fileSource, $fileDestination){ |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @When /^User "([^"]*)" moves file "([^"]*)" to "([^"]*)"$/ |
||
| 63 | * @param string $user |
||
| 64 | * @param string $fileSource |
||
| 65 | * @param string $fileDestination |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function userMovesFile($user, $fileSource, $fileDestination){ |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @When /^User "([^"]*)" copies file "([^"]*)" to "([^"]*)"$/ |
||
| 79 | * @param string $user |
||
| 80 | * @param string $fileSource |
||
| 81 | * @param string $fileDestination |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function userCopiesFile($user, $fileSource, $fileDestination){ |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @When /^Downloading file "([^"]*)" with range "([^"]*)"$/ |
||
| 95 | * @param string $fileSource |
||
| 96 | * @param string $range |
||
| 97 | */ |
||
| 98 | public function downloadFileWithRange($fileSource, $range){ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @When /^Downloading last public shared file with range "([^"]*)"$/ |
||
| 105 | * @param string $range |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function downloadPublicFileWithRange($range){ |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @When /^Downloading last public shared file inside a folder "([^"]*)" with range "([^"]*)"$/ |
||
| 124 | * @param string $range |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function downloadPublicFileInsideAFolderWithRange($path, $range){ |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @Then /^Downloaded content should be "([^"]*)"$/ |
||
| 143 | * @param string $content |
||
| 144 | */ |
||
| 145 | public function downloadedContentShouldBe($content){ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @Then /^Downloaded content when downloading file "([^"]*)" with range "([^"]*)" should be "([^"]*)"$/ |
||
| 151 | * @param string $fileSource |
||
| 152 | * @param string $range |
||
| 153 | * @param string $content |
||
| 154 | */ |
||
| 155 | public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content){ |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @When Downloading file :fileName |
||
| 162 | * @param string $fileName |
||
| 163 | */ |
||
| 164 | public function downloadingFile($fileName) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @Then The following headers should be set |
||
| 174 | * @param \Behat\Gherkin\Node\TableNode $table |
||
| 175 | * @throws \Exception |
||
| 176 | */ |
||
| 177 | public function theFollowingHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @Then Downloaded content should start with :start |
||
| 197 | * @param int $start |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | public function downloadedContentShouldStartWith($start) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @Then /^as "([^"]*)" gets properties of folder "([^"]*)" with$/ |
||
| 214 | * @param string $user |
||
| 215 | * @param string $path |
||
| 216 | * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable |
||
| 217 | */ |
||
| 218 | public function asGetsPropertiesOfFolderWith($user, $path, $propertiesTable) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Then the single response should contain a property :key with value :value |
||
| 230 | * @param string $key |
||
| 231 | * @param string $expectedValue |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Then the response should contain a share-types property with |
||
| 248 | */ |
||
| 249 | public function theResponseShouldContainAShareTypesPropertyWith($table) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @Then the response should contain an empty property :property |
||
| 282 | * @param string $property |
||
| 283 | * @throws \Exception |
||
| 284 | */ |
||
| 285 | public function theResponseShouldContainAnEmptyProperty($property) { |
||
| 295 | |||
| 296 | |||
| 297 | /*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/ |
||
| 298 | View Code Duplication | public function listFolder($user, $path, $folderDepth, $properties = null){ |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @Then /^user "([^"]*)" should see following elements$/ |
||
| 327 | * @param string $user |
||
| 328 | * @param \Behat\Gherkin\Node\TableNode|null $expectedElements |
||
| 329 | */ |
||
| 330 | public function checkElementList($user, $expectedElements){ |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @When User :user uploads file :source to :destination |
||
| 346 | * @param string $user |
||
| 347 | * @param string $source |
||
| 348 | * @param string $destination |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function userUploadsAFileTo($user, $source, $destination) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @When User :user uploads file with content :content to :destination |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function userUploadsAFileWithContentTo($user, $content, $destination) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @When User :user deletes file :file |
||
| 377 | * @param string $user |
||
| 378 | * @param string $file |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function userDeletesFile($user, $file) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @Given User :user created a folder :destination |
||
| 391 | * @param string $user |
||
| 392 | * @param string $destination |
||
| 393 | */ |
||
| 394 | View Code Duplication | public function userCreatedAFolder($user, $destination){ |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @Given user :user uploads chunk file :num of :total with :data to :destination |
||
| 405 | * @param string $user |
||
| 406 | * @param int $num |
||
| 407 | * @param int $total |
||
| 408 | * @param string $data |
||
| 409 | * @param string $destination |
||
| 410 | */ |
||
| 411 | public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Given user :user creates a new chunking upload with id :id |
||
| 421 | */ |
||
| 422 | public function userCreatesANewChunkingUploadWithId($user, $id) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Given user :user uploads new chunk file :num with :data to id :id |
||
| 430 | */ |
||
| 431 | public function userUploadsNewChunkFileOfWithToId($user, $num, $data, $id) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @Given user :user moves new chunk file with id :id to :dest |
||
| 440 | */ |
||
| 441 | public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest) |
||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * @Given /^Downloading file "([^"]*)" as "([^"]*)"$/ |
||
| 453 | */ |
||
| 454 | public function downloadingFileAs($fileName, $user) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @When user :user favorites element :path |
||
| 464 | */ |
||
| 465 | public function userFavoritesElement($user, $path){ |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @When user :user unfavorites element :path |
||
| 471 | */ |
||
| 472 | public function userUnfavoritesElement($user, $path){ |
||
| 475 | |||
| 476 | /*Set the elements of a proppatch, $folderDepth requires 1 to see elements without children*/ |
||
| 477 | View Code Duplication | public function changeFavStateOfAnElement($user, $path, $favOrUnfav, $folderDepth, $properties = null){ |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @Then /^as "([^"]*)" gets properties of file "([^"]*)" with$/ |
||
| 501 | * @param string $user |
||
| 502 | * @param string $path |
||
| 503 | * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable |
||
| 504 | */ |
||
| 505 | public function asGetsPropertiesOfFileWith($user, $path, $propertiesTable) { |
||
| 508 | } |
||
| 509 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: