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 |
||
| 18 | class FixtureContext extends BehatContext |
||
| 19 | { |
||
| 20 | protected $context; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \FixtureFactory |
||
| 24 | */ |
||
| 25 | protected $fixtureFactory; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var String Absolute path where file fixtures are located. |
||
| 29 | * These will automatically get copied to their location |
||
| 30 | * declare through the 'Given a file "..."' step defition. |
||
| 31 | */ |
||
| 32 | protected $filesPath; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var String Tracks all files and folders created from fixtures, for later cleanup. |
||
| 36 | */ |
||
| 37 | protected $createdFilesPaths = array(); |
||
| 38 | |||
| 39 | public function __construct(array $parameters) { |
||
| 42 | |||
| 43 | public function getSession($name = null) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return \FixtureFactory |
||
| 49 | */ |
||
| 50 | public function getFixtureFactory() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param \FixtureFactory $factory |
||
| 59 | */ |
||
| 60 | public function setFixtureFactory(\FixtureFactory $factory) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param String |
||
| 66 | */ |
||
| 67 | public function setFilesPath($path) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return String |
||
| 73 | */ |
||
| 74 | public function getFilesPath() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @BeforeScenario @database-defaults |
||
| 80 | */ |
||
| 81 | public function beforeDatabaseDefaults(ScenarioEvent $event) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @AfterScenario |
||
| 93 | */ |
||
| 94 | public function afterResetDatabase(ScenarioEvent $event) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @AfterScenario |
||
| 100 | */ |
||
| 101 | public function afterResetAssets(ScenarioEvent $event) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Example: Given a "page" "Page 1" |
||
| 116 | * |
||
| 117 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"$/ |
||
| 118 | */ |
||
| 119 | public function stepCreateRecord($type, $id) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Example: Given a "page" "Page 1" has the "content" "My content" |
||
| 127 | * |
||
| 128 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has (?:(an|a|the) )"(?<field>.*)" "(?<value>.*)"$/ |
||
| 129 | */ |
||
| 130 | public function stepCreateRecordHasField($type, $id, $field, $value) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1" |
||
| 150 | * Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1" |
||
| 151 | * |
||
| 152 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/ |
||
| 153 | */ |
||
| 154 | public function stepCreateRecordWithData($type, $id, $data) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Example: And the "page" "Page 2" has the following data |
||
| 180 | * | Content | <blink> | |
||
| 181 | * | My Property | foo | |
||
| 182 | * | My Boolean | bar | |
||
| 183 | * |
||
| 184 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has the following data$/ |
||
| 185 | */ |
||
| 186 | public function stepCreateRecordWithTable($type, $id, $null, TableNode $fieldsTable) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Example: Given the "page" "Page 1.1" is a child of the "page" "Page1". |
||
| 206 | * Note that this change is not published by default |
||
| 207 | * |
||
| 208 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is a (?<relation>[^\s]*) of (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"/ |
||
| 209 | */ |
||
| 210 | public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
| 247 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
| 248 | * |
||
| 249 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" |
||
| 250 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/ |
||
| 251 | */ |
||
| 252 | public function stepIAssignObjToObj($type, $value, $relationType, $relationId) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
| 258 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
| 259 | * Assumption: one object has relationship (has_one, has_many or many_many ) with the other object |
||
| 260 | * |
||
| 261 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation |
||
| 262 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/ |
||
| 263 | */ |
||
| 264 | public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Example: Given the "page" "Page 1" is not published |
||
| 315 | * |
||
| 316 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/ |
||
| 317 | */ |
||
| 318 | public function stepUpdateRecordState($type, $id, $state) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. |
||
| 353 | * |
||
| 354 | * Example: Given there are the following member records: |
||
| 355 | * member1: |
||
| 356 | * Email: [email protected] |
||
| 357 | * member2: |
||
| 358 | * Email: [email protected] |
||
| 359 | * |
||
| 360 | * @Given /^there are the following ([^\s]*) records$/ |
||
| 361 | */ |
||
| 362 | public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Example: Given a "member" "Admin" belonging to "Admin Group" |
||
| 374 | * |
||
| 375 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/ |
||
| 376 | */ |
||
| 377 | public function stepCreateMemberWithGroup($id, $groupId) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="[email protected]" |
||
| 387 | * |
||
| 388 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/ |
||
| 389 | */ |
||
| 390 | public function stepCreateMemberWithGroupAndData($id, $groupId, $data) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" |
||
| 411 | * |
||
| 412 | * @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/ |
||
| 413 | */ |
||
| 414 | public function stepCreateGroupWithPermissions($id, $permissionStr) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Navigates to a record based on its identifier set during fixture creation, |
||
| 444 | * using its RelativeLink() method to map the record to a URL. |
||
| 445 | * Example: Given I go to the "page" "My Page" |
||
| 446 | * |
||
| 447 | * @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/ |
||
| 448 | */ |
||
| 449 | public function stepGoToNamedRecord($type, $id) { |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Checks that a file or folder exists in the webroot. |
||
| 468 | * Example: There should be a file "assets/Uploads/test.jpg" |
||
| 469 | * |
||
| 470 | * @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/ |
||
| 471 | */ |
||
| 472 | public function stepThereShouldBeAFileOrFolder($type, $path) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Replaces fixture references in values with their respective database IDs, |
||
| 478 | * with the notation "=><class>.<identifier>". Example: "=>Page.My Page". |
||
| 479 | * |
||
| 480 | * @Transform /^([^"]+)$/ |
||
| 481 | */ |
||
| 482 | public function lookupFixtureReference($string) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/ |
||
| 500 | */ |
||
| 501 | public function aRecordWasLastEditedRelative($type, $id, $mod, $time) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Prepares a fixture for use |
||
| 529 | * |
||
| 530 | * @param string $class |
||
| 531 | * @param string $identifier |
||
| 532 | * @param array $data |
||
| 533 | * @return array Prepared $data with additional injected fields |
||
| 534 | */ |
||
| 535 | protected function prepareFixture($class, $identifier, $data = array()) { |
||
| 541 | |||
| 542 | protected function prepareAsset($class, $identifier, $data = null) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * |
||
| 592 | * @return AssetStore |
||
| 593 | */ |
||
| 594 | protected function getAssetStore() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Converts a natural language class description to an actual class name. |
||
| 600 | * Respects {@link DataObject::$singular_name} variations. |
||
| 601 | * Example: "redirector page" -> "RedirectorPage" |
||
| 602 | * |
||
| 603 | * @param String |
||
| 604 | * @return String Class name |
||
| 605 | */ |
||
| 606 | protected function convertTypeToClass($type) { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Updates an object with values, resolving aliases set through |
||
| 628 | * {@link DataObject->fieldLabels()}. |
||
| 629 | * |
||
| 630 | * @param String Class name |
||
| 631 | * @param Array Map of field names or aliases to their values. |
||
| 632 | * @return Array Map of actual object properties to their values. |
||
| 633 | */ |
||
| 634 | protected function convertFields($class, $fields) { |
||
| 645 | |||
| 646 | protected function joinPaths() { |
||
| 654 | |||
| 655 | } |
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: