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:
| 1 | <?php |
||
| 8 | trait BasicStructure { |
||
| 9 | |||
| 10 | /** @var string */ |
||
| 11 | private $currentUser = ''; |
||
| 12 | |||
| 13 | /** @var string */ |
||
| 14 | private $currentServer = ''; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | private $baseUrl = ''; |
||
| 18 | |||
| 19 | /** @var int */ |
||
| 20 | private $apiVersion = 1; |
||
| 21 | |||
| 22 | /** @var ResponseInterface */ |
||
| 23 | private $response = null; |
||
| 24 | |||
| 25 | /** @var \GuzzleHttp\Cookie\CookieJar */ |
||
| 26 | private $cookieJar; |
||
| 27 | |||
| 28 | /** @var string */ |
||
| 29 | private $requestToken; |
||
| 30 | |||
| 31 | public function __construct($baseUrl, $admin, $regular_user_password) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @Given /^using api version "([^"]*)"$/ |
||
| 58 | * @param string $version |
||
| 59 | */ |
||
| 60 | public function usingApiVersion($version) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @Given /^As an "([^"]*)"$/ |
||
| 66 | * @param string $user |
||
| 67 | */ |
||
| 68 | public function asAn($user) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @Given /^Using server "(LOCAL|REMOTE)"$/ |
||
| 74 | * @param string $server |
||
| 75 | * @return string Previous used server |
||
| 76 | */ |
||
| 77 | public function usingServer($server) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @When /^sending "([^"]*)" to "([^"]*)"$/ |
||
| 92 | * @param string $verb |
||
| 93 | * @param string $url |
||
| 94 | */ |
||
| 95 | public function sendingTo($verb, $url) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Parses the xml answer to get ocs response which doesn't match with |
||
| 101 | * http one in v1 of the api. |
||
| 102 | * @param ResponseInterface $response |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getOCSResponse($response) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * This function is needed to use a vertical fashion in the gherkin tables. |
||
| 111 | * @param array $arrayOfArrays |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function simplifyArray($arrayOfArrays){ |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @When /^sending "([^"]*)" to "([^"]*)" with$/ |
||
| 121 | * @param string $verb |
||
| 122 | * @param string $url |
||
| 123 | * @param \Behat\Gherkin\Node\TableNode $body |
||
| 124 | */ |
||
| 125 | public function sendingToWith($verb, $url, $body) { |
||
| 145 | |||
| 146 | public function isExpectedUrl($possibleUrl, $finalPart){ |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Then /^the OCS status code should be "([^"]*)"$/ |
||
| 154 | * @param int $statusCode |
||
| 155 | */ |
||
| 156 | public function theOCSStatusCodeShouldBe($statusCode) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @Then /^the HTTP status code should be "([^"]*)"$/ |
||
| 162 | * @param int $statusCode |
||
| 163 | */ |
||
| 164 | public function theHTTPStatusCodeShouldBe($statusCode) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param ResponseInterface $response |
||
| 170 | */ |
||
| 171 | private function extracRequestTokenFromResponse(ResponseInterface $response) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @Given Logging in using web as :user |
||
| 177 | * @param string $user |
||
| 178 | */ |
||
| 179 | public function loggingInUsingWebAs($user) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @When Sending a :method to :url with requesttoken |
||
| 210 | * @param string $method |
||
| 211 | * @param string $url |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function sendingAToWithRequesttoken($method, $url) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @When Sending a :method to :url without requesttoken |
||
| 234 | * @param string $method |
||
| 235 | * @param string $url |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function sendingAToWithoutRequesttoken($method, $url) { |
|
| 254 | |||
| 255 | public static function removeFile($path, $filename){ |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @Given User :user modifies text of :filename with text :text |
||
| 263 | * @param string $user |
||
| 264 | * @param string $filename |
||
| 265 | * @param string $text |
||
| 266 | */ |
||
| 267 | public function modifyTextOfFile($user, $filename, $text) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @BeforeSuite |
||
| 274 | */ |
||
| 275 | public static function addFilesToSkeleton(){ |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @AfterSuite |
||
| 294 | */ |
||
| 295 | public static function removeFilesFromSkeleton(){ |
||
| 311 | } |
||
| 312 | |||
| 313 |
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: