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 |
||
18 | class DefaultContext extends SyliusDefaultContext |
||
19 | { |
||
20 | protected $users = []; |
||
21 | |||
22 | /** |
||
23 | * Actions. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $actions = array( |
||
28 | 'viewing' => 'show', |
||
29 | 'creation' => 'create', |
||
30 | 'editing' => 'update', |
||
31 | 'building' => 'build', |
||
32 | 'testing' => 'connection_test', |
||
33 | ); |
||
34 | |||
35 | /** {@inheritdoc} */ |
||
36 | protected function generatePageUrl($page, array $parameters = array()) |
||
54 | |||
55 | protected function getRepository($resource) |
||
59 | |||
60 | public function thereIsUser($username, $email, $password, $role = null, $enabled = true, $group = null, $flush = true) |
||
91 | |||
92 | public function thereIsGame($name, $installName = null, $launchName = null, $bin = null, $type = null, $available = true, $flush = true) |
||
114 | |||
115 | /** |
||
116 | * @Given /^I am on the (.+) (page)?$/ |
||
117 | * @When /^I go to the (.+) (page)?$/ |
||
118 | */ |
||
119 | public function iAmOnThePage($page) |
||
123 | |||
124 | /** |
||
125 | * @Then /^I should be on the (.+) (page)$/ |
||
126 | * @Then /^I should be redirected to the (.+) (page)$/ |
||
127 | * @Then /^I should still be on the (.+) (page)$/ |
||
128 | */ |
||
129 | public function iShouldBeOnThePage($page) |
||
134 | |||
135 | /** |
||
136 | * @Then /^access should be forbidden$/ |
||
137 | */ |
||
138 | public function accessShouldBeForbidden() |
||
144 | |||
145 | /** |
||
146 | * @Then /^I should be unauthorized on the (.+) (page)$/ |
||
147 | */ |
||
148 | public function iShouldBeUnauthorizedOnThePage($page) |
||
153 | |||
154 | /** |
||
155 | * @Given /^I leave "([^"]*)" empty$/ |
||
156 | * @Given /^I leave "([^"]*)" field blank/ |
||
157 | */ |
||
158 | public function iLeaveFieldEmpty($field) |
||
162 | |||
163 | /** |
||
164 | * @Then /^I should see (\d+) validation errors?$/ |
||
165 | */ |
||
166 | public function iShouldSeeFieldsOnError($amount) |
||
170 | |||
171 | /** |
||
172 | * @Then /^I should see (\d+) (?:alert )?((error|success) )?message$/ |
||
173 | */ |
||
174 | public function iShouldSeeAlertMessage($amount, $type = '') |
||
180 | |||
181 | /** |
||
182 | * @Given /^there are following users:$/ |
||
183 | */ |
||
184 | public function thereAreFollowingUsers(TableNode $table) |
||
200 | |||
201 | /** |
||
202 | * @Given /^I am logged in with (.*) account$/ |
||
203 | */ |
||
204 | public function iAmLoggedInWithAccount($username) |
||
218 | |||
219 | /** |
||
220 | * @Given /^there are following games:$/ |
||
221 | */ |
||
222 | public function thereAreFollowingGames(TableNode $table) |
||
238 | |||
239 | /** |
||
240 | * @When /^I fill in (.+) form with:$/ |
||
241 | */ |
||
242 | public function whenIFillInFormWith($base, TableNode $table) |
||
271 | |||
272 | public function fillCheckbox($fieldName, $value) |
||
286 | |||
287 | public function fillRadio($fieldName, $value) |
||
298 | |||
299 | /** |
||
300 | * @Then /^I should see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/ |
||
301 | */ |
||
302 | public function iShouldSeeResourceWithValueInThatList($value) |
||
306 | |||
307 | /** |
||
308 | * @Then /^I should not see [\w\s]+ with [\w\s]+ "([^""]*)" in (that|the) list$/ |
||
309 | */ |
||
310 | public function iShouldNotSeeResourceWithValueInThatList($value) |
||
314 | |||
315 | /** |
||
316 | * @Then /^I should see (\d+) ([^"" ]*) in (that|the) list$/ |
||
317 | */ |
||
318 | View Code Duplication | public function iShouldSeeThatMuchResourcesInTheList($amount, $type) |
|
326 | |||
327 | /** |
||
328 | * @Then /^I should be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
329 | * @Then /^I should still be on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
330 | */ |
||
331 | View Code Duplication | public function iShouldBeOnTheResourcePage($type, $property, $value) |
|
339 | |||
340 | /** |
||
341 | * @Given /^I am on the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
342 | * @Given /^I go to the page of ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
343 | */ |
||
344 | public function iAmOnTheResourcePage($type, $property, $value) |
||
352 | |||
353 | /** |
||
354 | * @Given /^I am on the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
355 | * @Given /^I go to the page of (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
356 | */ |
||
357 | public function iAmOnTheResourcePageByName($type, $name) |
||
361 | |||
362 | /** |
||
363 | * @Given /^I am (building|viewing|editing) ([^""]*) with ([^""]*) "([^""]*)"$/ |
||
364 | */ |
||
365 | View Code Duplication | public function iAmDoingSomethingWithResource($action, $type, $property, $value) |
|
374 | |||
375 | /** |
||
376 | * @Given /^I am (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
377 | */ |
||
378 | public function iAmDoingSomethingWithResourceByName($action, $type, $name) |
||
382 | |||
383 | /** |
||
384 | * @Then /^I should be (building|viewing|editing|testing) ([^"]*) with ([^"]*) "([^""]*)"$/ |
||
385 | */ |
||
386 | View Code Duplication | public function iShouldBeDoingSomethingWithResource($action, $type, $property, $value) |
|
396 | |||
397 | /** |
||
398 | * @Then /^I should be (building|viewing|editing) (?!teamspeak)([^""(w)]*) "([^""]*)"$/ |
||
399 | */ |
||
400 | public function iShouldBeDoingSomethingWithResourceByName($action, $type, $name) |
||
404 | |||
405 | /** |
||
406 | * Assert that given code equals the current one. |
||
407 | * |
||
408 | * @param integer $code |
||
409 | */ |
||
410 | protected function assertStatusCodeEquals($code) |
||
414 | |||
415 | /** |
||
416 | * @Given /^there are following plugins:$/ |
||
417 | */ |
||
418 | public function thereAreFollowingPlugins(TableNode $table) |
||
432 | |||
433 | public function thereIsPlugin($name, $version, $scriptName, $downloadUrl, $flush = true) |
||
453 | |||
454 | /** |
||
455 | * @Then /^I should see (\d+) associated games?$/ |
||
456 | */ |
||
457 | public function iShouldSeeAssociatedGames($amount) |
||
461 | |||
462 | /** |
||
463 | * @Given /^there are following groups:$/ |
||
464 | */ |
||
465 | public function thereAreFollowingGroups(TableNode $table) |
||
477 | |||
478 | public function thereIsGroup($name, array $roles = array(), $parent = null, $flush = true) |
||
505 | |||
506 | protected function validate($data) |
||
514 | |||
515 | /** |
||
516 | * @Then /^I should see (\d+) buttons? "([^"]+)"$/ |
||
517 | */ |
||
518 | public function iShouldSeeButton($count, $value) |
||
524 | |||
525 | /** |
||
526 | * @Then /^I should not see button "([^"]+)"$/ |
||
527 | */ |
||
528 | public function iShouldNotSeeButton($value) |
||
534 | |||
535 | /** |
||
536 | * @Then /^I should be on 403 page$/ |
||
537 | * @Then /^I should be on 403 page with "([^"]+)"$/ |
||
538 | */ |
||
539 | public function iShouldBeOn403($message = "Vous n'avez pas accès à cette page.") |
||
546 | |||
547 | /** |
||
548 | * @Then /^I should see (\d+) "([^"]+)" checkbox(?:es)? in "([^"]+)" form$/ |
||
549 | */ |
||
550 | public function iShouldSeeCheckboxes($count, $type, $form) |
||
556 | |||
557 | /** |
||
558 | * @Then /^I should see (\d+) "([^"]+)" options in "([^"]+)" form$/ |
||
559 | */ |
||
560 | public function iShouldSeeOptionsInSelect($count, $type, $form) |
||
567 | |||
568 | /** |
||
569 | * @Given /^there are following machines:$/ |
||
570 | */ |
||
571 | public function thereAreFollowingMachines(TableNode $table) |
||
589 | |||
590 | public function thereIsMachine($username, $privateIp = null, $privateKey = null, $groups = array(), $is64Bit = false, $flush = true) |
||
614 | |||
615 | public function findField($form, $fieldName) |
||
626 | |||
627 | /** |
||
628 | * @Then /^I should see (\d+) ([^" ]*) servers in (that|the) list$/ |
||
629 | */ |
||
630 | View Code Duplication | public function iShouldSeeThatMuchServersInTheList($amount, $type) |
|
638 | } |
||
639 |
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: