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 | /** |
||
| 40 | * @var array Stores the asset tuples. |
||
| 41 | */ |
||
| 42 | protected $createdAssets = array(); |
||
| 43 | |||
| 44 | public function __construct(array $parameters) { |
||
| 47 | |||
| 48 | public function getSession($name = null) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return \FixtureFactory |
||
| 54 | */ |
||
| 55 | public function getFixtureFactory() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param \FixtureFactory $factory |
||
| 64 | */ |
||
| 65 | public function setFixtureFactory(\FixtureFactory $factory) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param String |
||
| 71 | */ |
||
| 72 | public function setFilesPath($path) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return String |
||
| 78 | */ |
||
| 79 | public function getFilesPath() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @BeforeScenario @database-defaults |
||
| 85 | */ |
||
| 86 | public function beforeDatabaseDefaults(ScenarioEvent $event) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @AfterScenario |
||
| 98 | */ |
||
| 99 | public function afterResetDatabase(ScenarioEvent $event) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @AfterScenario |
||
| 105 | */ |
||
| 106 | public function afterResetAssets(ScenarioEvent $event) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Example: Given a "page" "Page 1" |
||
| 117 | * |
||
| 118 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"$/ |
||
| 119 | */ |
||
| 120 | public function stepCreateRecord($type, $id) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Example: Given a "page" "Page 1" has the "content" "My content" |
||
| 128 | * |
||
| 129 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has (?:(an|a|the) )"(?<field>.*)" "(?<value>.*)"$/ |
||
| 130 | */ |
||
| 131 | public function stepCreateRecordHasField($type, $id, $field, $value) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1" |
||
| 151 | * Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1" |
||
| 152 | * |
||
| 153 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/ |
||
| 154 | */ |
||
| 155 | public function stepCreateRecordWithData($type, $id, $data) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Example: And the "page" "Page 2" has the following data |
||
| 181 | * | Content | <blink> | |
||
| 182 | * | My Property | foo | |
||
| 183 | * | My Boolean | bar | |
||
| 184 | * |
||
| 185 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has the following data$/ |
||
| 186 | */ |
||
| 187 | public function stepCreateRecordWithTable($type, $id, $null, TableNode $fieldsTable) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Example: Given the "page" "Page 1.1" is a child of the "page" "Page1". |
||
| 207 | * Note that this change is not published by default |
||
| 208 | * |
||
| 209 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is a (?<relation>[^\s]*) of (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"/ |
||
| 210 | */ |
||
| 211 | public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
| 248 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
| 249 | * |
||
| 250 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" |
||
| 251 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/ |
||
| 252 | */ |
||
| 253 | public function stepIAssignObjToObj($type, $value, $relationType, $relationId) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Assign a type of object to another type of object. The base object will be created if it does not exist already. |
||
| 259 | * If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used. |
||
| 260 | * Assumption: one object has relationship (has_one, has_many or many_many ) with the other object |
||
| 261 | * |
||
| 262 | * @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation |
||
| 263 | * @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/ |
||
| 264 | */ |
||
| 265 | public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Example: Given the "page" "Page 1" is not published |
||
| 316 | * |
||
| 317 | * @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/ |
||
| 318 | */ |
||
| 319 | public function stepUpdateRecordState($type, $id, $state) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. |
||
| 354 | * |
||
| 355 | * Example: Given there are the following member records: |
||
| 356 | * member1: |
||
| 357 | * Email: [email protected] |
||
| 358 | * member2: |
||
| 359 | * Email: [email protected] |
||
| 360 | * |
||
| 361 | * @Given /^there are the following ([^\s]*) records$/ |
||
| 362 | */ |
||
| 363 | public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Example: Given a "member" "Admin" belonging to "Admin Group" |
||
| 375 | * |
||
| 376 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/ |
||
| 377 | */ |
||
| 378 | public function stepCreateMemberWithGroup($id, $groupId) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="[email protected]" |
||
| 388 | * |
||
| 389 | * @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/ |
||
| 390 | */ |
||
| 391 | public function stepCreateMemberWithGroupAndData($id, $groupId, $data) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" |
||
| 412 | * |
||
| 413 | * @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/ |
||
| 414 | */ |
||
| 415 | public function stepCreateGroupWithPermissions($id, $permissionStr) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Navigates to a record based on its identifier set during fixture creation, |
||
| 445 | * using its RelativeLink() method to map the record to a URL. |
||
| 446 | * Example: Given I go to the "page" "My Page" |
||
| 447 | * |
||
| 448 | * @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/ |
||
| 449 | */ |
||
| 450 | public function stepGoToNamedRecord($type, $id) { |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Checks that a file or folder exists in the webroot. |
||
| 469 | * Example: There should be a file "assets/Uploads/test.jpg" |
||
| 470 | * |
||
| 471 | * @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/ |
||
| 472 | */ |
||
| 473 | public function stepThereShouldBeAFileOrFolder($type, $path) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Checks that a file exists in the asset store with a given filename and hash |
||
| 479 | * |
||
| 480 | * Example: there should be a filename "Uploads/test.jpg" with hash "59de0c841f" |
||
| 481 | * |
||
| 482 | * @Then /^there should be a filename "(?<filename>[^"]*)" with hash "(?<sha>[a-fA-Z0-9]+)"/ |
||
| 483 | */ |
||
| 484 | public function stepThereShouldBeAFileWithTuple($filename, $hash) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Replaces fixture references in values with their respective database IDs, |
||
| 491 | * with the notation "=><class>.<identifier>". Example: "=>Page.My Page". |
||
| 492 | * |
||
| 493 | * @Transform /^([^"]+)$/ |
||
| 494 | */ |
||
| 495 | public function lookupFixtureReference($string) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/ |
||
| 513 | */ |
||
| 514 | public function aRecordWasLastEditedRelative($type, $id, $mod, $time) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Prepares a fixture for use |
||
| 542 | * |
||
| 543 | * @param string $class |
||
| 544 | * @param string $identifier |
||
| 545 | * @param array $data |
||
| 546 | * @return array Prepared $data with additional injected fields |
||
| 547 | */ |
||
| 548 | protected function prepareFixture($class, $identifier, $data = array()) { |
||
| 554 | |||
| 555 | protected function prepareAsset($class, $identifier, $data = null) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * |
||
| 605 | * @return AssetStore |
||
| 606 | */ |
||
| 607 | protected function getAssetStore() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Converts a natural language class description to an actual class name. |
||
| 613 | * Respects {@link DataObject::$singular_name} variations. |
||
| 614 | * Example: "redirector page" -> "RedirectorPage" |
||
| 615 | * |
||
| 616 | * @param String |
||
| 617 | * @return String Class name |
||
| 618 | */ |
||
| 619 | protected function convertTypeToClass($type) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Updates an object with values, resolving aliases set through |
||
| 641 | * {@link DataObject->fieldLabels()}. |
||
| 642 | * |
||
| 643 | * @param String Class name |
||
| 644 | * @param Array Map of field names or aliases to their values. |
||
| 645 | * @return Array Map of actual object properties to their values. |
||
| 646 | */ |
||
| 647 | protected function convertFields($class, $fields) { |
||
| 658 | |||
| 659 | protected function joinPaths() { |
||
| 667 | |||
| 668 | } |
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: