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) |
||
264 | |||
265 | /** |
||
266 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
267 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
268 | * |
||
269 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" |
||
270 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/ |
||
271 | */ |
||
272 | public function stepIAssignObjToObj($type, $value, $relationType, $relationId) |
||
276 | |||
277 | /** |
||
278 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
279 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
280 | * Assumption: one object has relationship (has_one, has_many or many_many ) with the other object |
||
281 | * |
||
282 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation |
||
283 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/ |
||
284 | */ |
||
285 | public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) |
||
348 | |||
349 | /** |
||
350 | * Example: Given the "page" "Page 1" is not published |
||
351 | * |
||
352 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/ |
||
353 | */ |
||
354 | public function stepUpdateRecordState($type, $id, $state) |
||
388 | |||
389 | /** |
||
390 | * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. |
||
391 | * |
||
392 | * Example: Given there are the following member records: |
||
393 | * member1: |
||
394 | * Email: [email protected] |
||
395 | * member2: |
||
396 | * Email: [email protected] |
||
397 | * |
||
398 | * @Given /^there are the following ([^\s]*) records$/ |
||
399 | */ |
||
400 | public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) |
||
410 | |||
411 | /** |
||
412 | * Example: Given a "member" "Admin" belonging to "Admin Group" |
||
413 | * |
||
414 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/ |
||
415 | */ |
||
416 | public function stepCreateMemberWithGroup($id, $groupId) |
||
426 | |||
427 | /** |
||
428 | * Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="[email protected]" |
||
429 | * |
||
430 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/ |
||
431 | */ |
||
432 | public function stepCreateMemberWithGroupAndData($id, $groupId, $data) |
||
453 | |||
454 | /** |
||
455 | * Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" |
||
456 | * |
||
457 | * @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/ |
||
458 | */ |
||
459 | public function stepCreateGroupWithPermissions($id, $permissionStr) |
||
489 | |||
490 | /** |
||
491 | * Navigates to a record based on its identifier set during fixture creation, |
||
492 | * using its RelativeLink() method to map the record to a URL. |
||
493 | * Example: Given I go to the "page" "My Page" |
||
494 | * |
||
495 | * @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/ |
||
496 | */ |
||
497 | public function stepGoToNamedRecord($type, $id) |
||
513 | |||
514 | |||
515 | /** |
||
516 | * Checks that a file or folder exists in the webroot. |
||
517 | * Example: There should be a file "assets/Uploads/test.jpg" |
||
518 | * |
||
519 | * @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/ |
||
520 | */ |
||
521 | public function stepThereShouldBeAFileOrFolder($type, $path) |
||
525 | |||
526 | /** |
||
527 | * Checks that a file exists in the asset store with a given filename and hash |
||
528 | * |
||
529 | * Example: there should be a filename "Uploads/test.jpg" with hash "59de0c841f" |
||
530 | * |
||
531 | * @Then /^there should be a filename "(?<filename>[^"]*)" with hash "(?<hash>[a-fA-Z0-9]+)"/ |
||
532 | */ |
||
533 | public function stepThereShouldBeAFileWithTuple($filename, $hash) |
||
538 | |||
539 | /** |
||
540 | * Replaces fixture references in values with their respective database IDs, |
||
541 | * with the notation "=><class>.<identifier>". Example: "=>Page.My Page". |
||
542 | * |
||
543 | * @Transform /^([^"]+)$/ |
||
544 | */ |
||
545 | public function lookupFixtureReference($string) |
||
561 | |||
562 | /** |
||
563 | * @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/ |
||
564 | */ |
||
565 | public function aRecordWasLastEditedRelative($type, $id, $mod, $time) |
||
585 | |||
586 | /** |
||
587 | * Prepares a fixture for use |
||
588 | * |
||
589 | * @param string $class |
||
590 | * @param string $identifier |
||
591 | * @param array $data |
||
592 | * @return array Prepared $data with additional injected fields |
||
593 | */ |
||
594 | protected function prepareFixture($class, $identifier, $data = array()) |
||
601 | |||
602 | protected function prepareAsset($class, $identifier, $data = null) |
||
652 | |||
653 | /** |
||
654 | * |
||
655 | * @return AssetStore |
||
656 | */ |
||
657 | protected function getAssetStore() |
||
661 | |||
662 | /** |
||
663 | * Converts a natural language class description to an actual class name. |
||
664 | * Respects {@link DataObject::$singular_name} variations. |
||
665 | * Example: "redirector page" -> "RedirectorPage" |
||
666 | * |
||
667 | * @param String |
||
668 | * @return String Class name |
||
669 | */ |
||
670 | protected function convertTypeToClass($type) |
||
692 | |||
693 | /** |
||
694 | * Updates an object with values, resolving aliases set through |
||
695 | * {@link DataObject->fieldLabels()}. |
||
696 | * |
||
697 | * @param string $class Class name |
||
698 | * @param array $fields Map of field names or aliases to their values. |
||
699 | * @return array Map of actual object properties to their values. |
||
700 | */ |
||
701 | protected function convertFields($class, $fields) |
||
712 | |||
713 | protected function joinPaths() |
||
728 | } |
||
729 |
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: