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 AbstractTestCase 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 AbstractTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class AbstractTestCase extends TestCase |
||
| 27 | { |
||
| 28 | |||
| 29 | protected static $baseNamespaces = []; |
||
| 30 | |||
| 31 | protected $baseThemeClass = 'Magium\Themes\ThemeConfigurationInterface'; |
||
| 32 | |||
| 33 | protected $postCallbacks = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var MasterListener |
||
| 37 | */ |
||
| 38 | |||
| 39 | protected static $masterListener; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Magium\WebDriver\WebDriver |
||
| 43 | */ |
||
| 44 | protected $webdriver; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Di |
||
| 48 | */ |
||
| 49 | |||
| 50 | protected $di; |
||
| 51 | |||
| 52 | protected $textElementNodeSearch = [ |
||
| 53 | 'button', 'span', 'a', 'li', 'label', 'option', 'h1', 'h2', 'h3', 'td' |
||
| 54 | ]; |
||
| 55 | |||
| 56 | protected $initializer; |
||
| 57 | |||
| 58 | const BY_XPATH = 'byXpath'; |
||
| 59 | const BY_ID = 'byId'; |
||
| 60 | const BY_CSS_SELECTOR = 'byCssSelector'; |
||
| 61 | const BY_TEXT = 'byText'; |
||
| 62 | |||
| 63 | private $sectionTimerStart; |
||
| 64 | |||
| 65 | public function startTimer() |
||
| 69 | |||
| 70 | public function endTimer($name) |
||
| 91 | |||
| 92 | protected static $registrationCallbacks; |
||
| 93 | |||
| 94 | protected function setUp() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return Di |
||
| 116 | */ |
||
| 117 | public function getDi() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Di $di |
||
| 124 | */ |
||
| 125 | public function setDi($di) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return WebDriver |
||
| 132 | */ |
||
| 133 | public function getWebdriver() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param WebDriver $webdriver |
||
| 143 | */ |
||
| 144 | public function setWebdriver(WebDriver $webdriver) |
||
| 148 | |||
| 149 | public function getInitializer() |
||
| 153 | |||
| 154 | |||
| 155 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
| 161 | |||
| 162 | public function setUseErrorHandlerFromAnnotation() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return MasterListener |
||
| 175 | */ |
||
| 176 | |||
| 177 | public static function getMasterListener() |
||
| 188 | |||
| 189 | protected function tearDown() |
||
| 208 | |||
| 209 | |||
| 210 | public function filterWebDriverAction($by) |
||
| 223 | |||
| 224 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
| 228 | |||
| 229 | |||
| 230 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
| 235 | |||
| 236 | public static function addBaseNamespace($namespace) |
||
| 242 | |||
| 243 | public static function resolveClass( $class, $prefix = null) |
||
| 274 | |||
| 275 | public function setTypePreference($type, $preference) |
||
| 283 | |||
| 284 | protected function normalizeClassRequest($class) |
||
| 288 | |||
| 289 | public function addPostTestCallback($callback) |
||
| 296 | |||
| 297 | /** |
||
| 298 | |||
| 299 | * @param string $theme |
||
| 300 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
| 301 | */ |
||
| 302 | |||
| 303 | public function getTheme($theme = null) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * |
||
| 314 | * @param string $action |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | |||
| 318 | public function getAction($action) |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $name |
||
| 328 | * @return \Magium\Identities\NameInterface |
||
| 329 | */ |
||
| 330 | |||
| 331 | public function getIdentity($name = 'Customer') |
||
| 337 | |||
| 338 | /** |
||
| 339 | * |
||
| 340 | * @param string $navigator |
||
| 341 | * @return \Magium\Navigators\NavigatorInterface |
||
| 342 | */ |
||
| 343 | |||
| 344 | public function getNavigator($navigator = 'BaseMenu') |
||
| 350 | |||
| 351 | public function getAssertion($assertion) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * |
||
| 360 | * @param string $extractor |
||
| 361 | * @return \Magium\Extractors\AbstractExtractor |
||
| 362 | */ |
||
| 363 | |||
| 364 | public function getExtractor($extractor) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Sleep the specified amount of time. |
||
| 373 | * |
||
| 374 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
| 375 | * |
||
| 376 | * @param $time |
||
| 377 | */ |
||
| 378 | |||
| 379 | public function sleep($time) |
||
| 393 | |||
| 394 | |||
| 395 | public function commandOpen($url) |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @return LoggerInterface |
||
| 403 | */ |
||
| 404 | |||
| 405 | public function getLogger() |
||
| 409 | |||
| 410 | public function get($class) |
||
| 419 | |||
| 420 | public static function isPHPUnit5() |
||
| 424 | |||
| 425 | public function assertElementExists($selector, $by = 'byId') |
||
| 429 | |||
| 430 | public function assertTitleEquals($title) |
||
| 435 | |||
| 436 | |||
| 437 | public function assertTitleContains($title) |
||
| 442 | |||
| 443 | |||
| 444 | public function assertNotTitleEquals($title) |
||
| 449 | |||
| 450 | |||
| 451 | public function assertNotTitleContains($title) |
||
| 456 | |||
| 457 | public function assertURLEquals($url) |
||
| 462 | |||
| 463 | public function assertURLContains($url) |
||
| 468 | |||
| 469 | |||
| 470 | public function assertURLNotEquals($url) |
||
| 475 | |||
| 476 | public function assertURLNotContains($url) |
||
| 481 | |||
| 482 | protected function elementAssertion($selector, $by, $name) |
||
| 489 | |||
| 490 | public function assertElementDisplayed($selector, $by = 'byId') |
||
| 494 | |||
| 495 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
| 499 | |||
| 500 | public function assertElementNotExists($selector, $by = 'byId') |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return LoggingAssertionExecutor |
||
| 507 | */ |
||
| 508 | |||
| 509 | public function getAssertionLogger() |
||
| 513 | |||
| 514 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
| 537 | |||
| 538 | public static function assertWebDriverElement($element) |
||
| 542 | |||
| 543 | View Code Duplication | public function assertElementHasText($node, $text) |
|
| 552 | |||
| 553 | View Code Duplication | public function assertPageHasText($text) |
|
| 563 | |||
| 564 | public function assertPageNotHasText($text) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param $xpath |
||
| 577 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 578 | */ |
||
| 579 | |||
| 580 | public function byXpath($xpath) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param $id |
||
| 587 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 588 | */ |
||
| 589 | |||
| 590 | public function byId($id) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param $selector |
||
| 597 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 598 | */ |
||
| 599 | |||
| 600 | public function byCssSelector($selector) |
||
| 604 | |||
| 605 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param string $text |
||
| 627 | * @param string $specificNodeType |
||
| 628 | * @param string $parentElementSelector |
||
| 629 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 630 | */ |
||
| 631 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 636 | |||
| 637 | |||
| 638 | /** |
||
| 639 | * @param string $text |
||
| 640 | * @param string $specificNodeType |
||
| 641 | * @param string $parentElementSelector |
||
| 642 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 643 | */ |
||
| 644 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @return \Magium\Util\Translator\Translator |
||
| 652 | */ |
||
| 653 | |||
| 654 | public function getTranslator() |
||
| 658 | |||
| 659 | public function addTranslationCsvFile($file, $locale) |
||
| 663 | } |
||
| 664 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.