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 DefaultContext 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 DefaultContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DefaultContext extends SyliusDefaultContext |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $actions = array( |
||
| 23 | 'viewing' => 'show', |
||
| 24 | 'creation' => 'create', |
||
| 25 | 'editing' => 'update', |
||
| 26 | 'building' => 'build', |
||
| 27 | 'testing' => 'connection_test', |
||
| 28 | ); |
||
| 29 | |||
| 30 | /** {@inheritdoc} */ |
||
| 31 | protected function generatePageUrl($page, array $parameters = array()) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $baseName |
||
| 52 | */ |
||
| 53 | View Code Duplication | protected function getRepository($resource, $baseName = null) |
|
| 63 | |||
| 64 | View Code Duplication | protected function findOneBy($type, array $criteria, $repoPrefix = '') |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @Given /^I am on the (.+) (page)?$/ |
||
| 81 | * @When /^I go to the (.+) (page)?$/ |
||
| 82 | */ |
||
| 83 | public function iAmOnThePage($page) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @Then /^I should be on the (.+) (page)$/ |
||
| 90 | * @Then /^I should be redirected to the (.+) (page)$/ |
||
| 91 | * @Then /^I should still be on the (.+) (page)$/ |
||
| 92 | */ |
||
| 93 | public function iShouldBeOnThePage($page) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @Then /^access should be forbidden$/ |
||
| 101 | */ |
||
| 102 | public function accessShouldBeForbidden() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @Then /^I should be unauthorized on the (.+) (page)$/ |
||
| 111 | */ |
||
| 112 | public function iShouldBeUnauthorizedOnThePage($page) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @Given /^I leave "([^"]*)" empty$/ |
||
| 120 | * @Given /^I leave "([^"]*)" field blank/ |
||
| 121 | */ |
||
| 122 | public function iLeaveFieldEmpty($field) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @Then /^I should see (\d+) validation errors?$/ |
||
| 129 | */ |
||
| 130 | public function iShouldSeeFieldsOnError($amount) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @Then /^I should see (\d+) (?:alert )?((error|success) )?message$/ |
||
| 137 | */ |
||
| 138 | public function iShouldSeeAlertMessage($amount, $type = '') |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @Given /^I am logged in with (.*) account$/ |
||
| 147 | */ |
||
| 148 | public function iAmLoggedInWithAccount($username) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @When /^I fill in (.+) form with:$/ |
||
| 165 | */ |
||
| 166 | public function whenIFillInFormWith($base, TableNode $table) |
||
| 195 | |||
| 196 | public function fillCheckbox($fieldName, $value) |
||
| 210 | |||
| 211 | public function fillRadio($fieldName, $value) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @Then /^I should see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/ |
||
| 225 | */ |
||
| 226 | public function iShouldSeeResourceWithValueInThatList($value) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @Then /^I should not see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/ |
||
| 233 | */ |
||
| 234 | public function iShouldNotSeeResourceWithValueInThatList($value) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @Then /^I should see (\d+) ([^"" ]*) in (that|the) list$/ |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function iShouldSeeThatMuchResourcesInTheList($amount, $type) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 253 | * @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function iShouldBeOnTheResourcePage($type, $property, $value) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 266 | * @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 267 | */ |
||
| 268 | public function iAmOnTheResourcePage($type, $property, $value) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @Given /^I am on the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
| 279 | * @Given /^I go to the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
| 280 | */ |
||
| 281 | public function iAmOnTheResourcePageByName($type, $name) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function iAmDoingSomethingWithResource($action, $type, $property, $value) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @Given /^I am (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
| 301 | */ |
||
| 302 | public function iAmDoingSomethingWithResourceByName($action, $type, $name) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @Then /^I should be (building|viewing|editing|testing) ([^"]*) with ([^"]*) "([^""]*)"$/ |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @Then /^I should be (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
| 323 | */ |
||
| 324 | public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Assert that given code equals the current one. |
||
| 331 | * |
||
| 332 | * @param integer $code |
||
| 333 | */ |
||
| 334 | protected function assertStatusCodeEquals($code) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @Then /^I should see (\d+) associated games?$/ |
||
| 341 | */ |
||
| 342 | public function iShouldSeeAssociatedGames($amount) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Then /^I should see (\d+) buttons? "([^"]+)"$/ |
||
| 349 | */ |
||
| 350 | public function iShouldSeeButton($count, $value) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Then /^I should not see button "([^"]+)"$/ |
||
| 359 | */ |
||
| 360 | public function iShouldNotSeeButton($value) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Then /^I should be on 403 page$/ |
||
| 369 | * @Then /^I should be on 403 page with "([^"]+)"$/ |
||
| 370 | */ |
||
| 371 | public function iShouldBeOn403($message = "Vous n'avez pas accès à cette page.") |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Then /^I should see (\d+) "([^"]+)" checkbox(?:es)? in "([^"]+)" form$/ |
||
| 381 | */ |
||
| 382 | public function iShouldSeeCheckboxes($count, $type, $form) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Then /^I should see (\d+) "([^"]+)" options in "([^"]+)" form$/ |
||
| 391 | */ |
||
| 392 | public function iShouldSeeOptionsInSelect($count, $type, $form) |
||
| 399 | |||
| 400 | public function findField($form, $fieldName) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @Then /^I should see (\d+) ([^" ]*) servers in (that|the) list$/ |
||
| 414 | */ |
||
| 415 | View Code Duplication | public function iShouldSeeThatMuchServersInTheList($amount, $type) |
|
| 423 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: