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 FixtureContext 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 FixtureContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class FixtureContext extends BehatContext |
||
22 | { |
||
23 | protected $context; |
||
24 | |||
25 | /** |
||
26 | * @var \FixtureFactory |
||
27 | */ |
||
28 | protected $fixtureFactory; |
||
29 | |||
30 | /** |
||
31 | * @var String Absolute path where file fixtures are located. |
||
32 | * These will automatically get copied to their location |
||
33 | * declare through the 'Given a file "..."' step defition. |
||
34 | */ |
||
35 | protected $filesPath; |
||
36 | |||
37 | /** |
||
38 | * @var String Tracks all files and folders created from fixtures, for later cleanup. |
||
39 | */ |
||
40 | protected $createdFilesPaths = array(); |
||
41 | |||
42 | /** |
||
43 | * @var array Stores the asset tuples. |
||
44 | */ |
||
45 | protected $createdAssets = array(); |
||
46 | |||
47 | public function __construct(array $parameters) |
||
51 | |||
52 | public function getSession($name = null) |
||
56 | |||
57 | /** |
||
58 | * @return \FixtureFactory |
||
59 | */ |
||
60 | public function getFixtureFactory() |
||
67 | |||
68 | /** |
||
69 | * @param \FixtureFactory $factory |
||
70 | */ |
||
71 | public function setFixtureFactory(\FixtureFactory $factory) |
||
75 | |||
76 | /** |
||
77 | * @param String |
||
78 | */ |
||
79 | public function setFilesPath($path) |
||
83 | |||
84 | /** |
||
85 | * @return String |
||
86 | */ |
||
87 | public function getFilesPath() |
||
91 | |||
92 | /** |
||
93 | * @BeforeScenario @database-defaults |
||
94 | */ |
||
95 | public function beforeDatabaseDefaults(ScenarioEvent $event) |
||
105 | |||
106 | /** |
||
107 | * @AfterScenario |
||
108 | */ |
||
109 | public function afterResetDatabase(ScenarioEvent $event) |
||
113 | |||
114 | /** |
||
115 | * @AfterScenario |
||
116 | */ |
||
117 | public function afterResetAssets(ScenarioEvent $event) |
||
126 | |||
127 | /** |
||
128 | * Example: Given a "page" "Page 1" |
||
129 | * |
||
130 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"$/ |
||
131 | */ |
||
132 | public function stepCreateRecord($type, $id) |
||
138 | |||
139 | /** |
||
140 | * Example: Given a "page" "Page 1" has the "content" "My content" |
||
141 | * |
||
142 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has (?:(an|a|the) )"(?<field>.*)" "(?<value>.*)"$/ |
||
143 | */ |
||
144 | public function stepCreateRecordHasField($type, $id, $field, $value) |
||
162 | |||
163 | /** |
||
164 | * Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1" |
||
165 | * Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1" |
||
166 | * |
||
167 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/ |
||
168 | */ |
||
169 | public function stepCreateRecordWithData($type, $id, $data) |
||
193 | |||
194 | /** |
||
195 | * Example: And the "page" "Page 2" has the following data |
||
196 | * | Content | <blink> | |
||
197 | * | My Property | foo | |
||
198 | * | My Boolean | bar | |
||
199 | * |
||
200 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has the following data$/ |
||
201 | */ |
||
202 | public function stepCreateRecordWithTable($type, $id, $null, TableNode $fieldsTable) |
||
220 | |||
221 | /** |
||
222 | * Example: Given the "page" "Page 1.1" is a child of the "page" "Page1". |
||
223 | * Note that this change is not published by default |
||
224 | * |
||
225 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is a (?<relation>[^\s]*) of (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"/ |
||
226 | */ |
||
227 | public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) |
||
265 | |||
266 | /** |
||
267 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
268 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
269 | * |
||
270 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" |
||
271 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/ |
||
272 | */ |
||
273 | public function stepIAssignObjToObj($type, $value, $relationType, $relationId) |
||
277 | |||
278 | /** |
||
279 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
280 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
281 | * Assumption: one object has relationship (has_one, has_many or many_many ) with the other object |
||
282 | * |
||
283 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation |
||
284 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/ |
||
285 | */ |
||
286 | public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) |
||
349 | |||
350 | /** |
||
351 | * Example: Given the "page" "Page 1" is not published |
||
352 | * |
||
353 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/ |
||
354 | */ |
||
355 | public function stepUpdateRecordState($type, $id, $state) |
||
390 | |||
391 | /** |
||
392 | * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. |
||
393 | * |
||
394 | * Example: Given there are the following member records: |
||
395 | * member1: |
||
396 | * Email: [email protected] |
||
397 | * member2: |
||
398 | * Email: [email protected] |
||
399 | * |
||
400 | * @Given /^there are the following ([^\s]*) records$/ |
||
401 | */ |
||
402 | public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) |
||
412 | |||
413 | /** |
||
414 | * Example: Given a "member" "Admin" belonging to "Admin Group" |
||
415 | * |
||
416 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/ |
||
417 | */ |
||
418 | public function stepCreateMemberWithGroup($id, $groupId) |
||
428 | |||
429 | /** |
||
430 | * Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="[email protected]" |
||
431 | * |
||
432 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/ |
||
433 | */ |
||
434 | public function stepCreateMemberWithGroupAndData($id, $groupId, $data) |
||
455 | |||
456 | /** |
||
457 | * Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" |
||
458 | * |
||
459 | * @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/ |
||
460 | */ |
||
461 | public function stepCreateGroupWithPermissions($id, $permissionStr) |
||
491 | |||
492 | /** |
||
493 | * Navigates to a record based on its identifier set during fixture creation, |
||
494 | * using its RelativeLink() method to map the record to a URL. |
||
495 | * Example: Given I go to the "page" "My Page" |
||
496 | * |
||
497 | * @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/ |
||
498 | */ |
||
499 | public function stepGoToNamedRecord($type, $id) |
||
515 | |||
516 | |||
517 | /** |
||
518 | * Checks that a file or folder exists in the webroot. |
||
519 | * Example: There should be a file "assets/Uploads/test.jpg" |
||
520 | * |
||
521 | * @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/ |
||
522 | */ |
||
523 | public function stepThereShouldBeAFileOrFolder($type, $path) |
||
527 | |||
528 | /** |
||
529 | * Checks that a file exists in the asset store with a given filename and hash |
||
530 | * |
||
531 | * Example: there should be a filename "Uploads/test.jpg" with hash "59de0c841f" |
||
532 | * |
||
533 | * @Then /^there should be a filename "(?<filename>[^"]*)" with hash "(?<hash>[a-fA-Z0-9]+)"/ |
||
534 | */ |
||
535 | public function stepThereShouldBeAFileWithTuple($filename, $hash) |
||
540 | |||
541 | /** |
||
542 | * Replaces fixture references in values with their respective database IDs, |
||
543 | * with the notation "=><class>.<identifier>". Example: "=>Page.My Page". |
||
544 | * |
||
545 | * @Transform /^([^"]+)$/ |
||
546 | */ |
||
547 | public function lookupFixtureReference($string) |
||
563 | |||
564 | /** |
||
565 | * @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/ |
||
566 | */ |
||
567 | public function aRecordWasLastEditedRelative($type, $id, $mod, $time) |
||
587 | |||
588 | /** |
||
589 | * Prepares a fixture for use |
||
590 | * |
||
591 | * @param string $class |
||
592 | * @param string $identifier |
||
593 | * @param array $data |
||
594 | * @return array Prepared $data with additional injected fields |
||
595 | */ |
||
596 | protected function prepareFixture($class, $identifier, $data = array()) |
||
603 | |||
604 | protected function prepareAsset($class, $identifier, $data = null) |
||
654 | |||
655 | /** |
||
656 | * |
||
657 | * @return AssetStore |
||
658 | */ |
||
659 | protected function getAssetStore() |
||
663 | |||
664 | /** |
||
665 | * Converts a natural language class description to an actual class name. |
||
666 | * Respects {@link DataObject::$singular_name} variations. |
||
667 | * Example: "redirector page" -> "RedirectorPage" |
||
668 | * |
||
669 | * @param String |
||
670 | * @return String Class name |
||
671 | */ |
||
672 | protected function convertTypeToClass($type) |
||
694 | |||
695 | /** |
||
696 | * Updates an object with values, resolving aliases set through |
||
697 | * {@link DataObject->fieldLabels()}. |
||
698 | * |
||
699 | * @param string $class Class name |
||
700 | * @param array $fields Map of field names or aliases to their values. |
||
701 | * @return array Map of actual object properties to their values. |
||
702 | */ |
||
703 | protected function convertFields($class, $fields) |
||
714 | |||
715 | protected function joinPaths() |
||
730 | } |
||
731 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: