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, $minimumElapsedTime = 0) |
||
| 95 | |||
| 96 | protected static $registrationCallbacks; |
||
| 97 | |||
| 98 | protected function setUp() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return Di |
||
| 120 | */ |
||
| 121 | public function getDi() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param Di $di |
||
| 128 | */ |
||
| 129 | public function setDi($di) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return WebDriver |
||
| 136 | */ |
||
| 137 | public function getWebdriver() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param WebDriver $webdriver |
||
| 147 | */ |
||
| 148 | public function setWebdriver(WebDriver $webdriver) |
||
| 152 | |||
| 153 | public function getInitializer() |
||
| 157 | |||
| 158 | |||
| 159 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
| 165 | |||
| 166 | public function setUseErrorHandlerFromAnnotation() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return MasterListener |
||
| 179 | */ |
||
| 180 | |||
| 181 | public static function getMasterListener() |
||
| 192 | |||
| 193 | protected function tearDown() |
||
| 212 | |||
| 213 | |||
| 214 | public function filterWebDriverAction($by) |
||
| 227 | |||
| 228 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
| 232 | |||
| 233 | |||
| 234 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
| 239 | |||
| 240 | public static function addBaseNamespace($namespace) |
||
| 246 | |||
| 247 | public static function resolveClass( $class, $prefix = null) |
||
| 278 | |||
| 279 | public function setTypePreference($type, $preference) |
||
| 287 | |||
| 288 | protected function normalizeClassRequest($class) |
||
| 292 | |||
| 293 | public function addPostTestCallback($callback) |
||
| 300 | |||
| 301 | /** |
||
| 302 | |||
| 303 | * @param string $theme |
||
| 304 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
| 305 | */ |
||
| 306 | |||
| 307 | public function getTheme($theme = null) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * |
||
| 318 | * @param string $action |
||
| 319 | * @return mixed |
||
| 320 | */ |
||
| 321 | |||
| 322 | public function getAction($action) |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $name |
||
| 332 | * @return \Magium\Identities\NameInterface |
||
| 333 | */ |
||
| 334 | |||
| 335 | public function getIdentity($name = 'Customer') |
||
| 341 | |||
| 342 | /** |
||
| 343 | * |
||
| 344 | * @param string $navigator |
||
| 345 | * @return \Magium\Navigators\NavigatorInterface |
||
| 346 | */ |
||
| 347 | |||
| 348 | public function getNavigator($navigator = 'BaseMenu') |
||
| 354 | |||
| 355 | public function getAssertion($assertion) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * |
||
| 364 | * @param string $extractor |
||
| 365 | * @return \Magium\Extractors\AbstractExtractor |
||
| 366 | */ |
||
| 367 | |||
| 368 | public function getExtractor($extractor) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Sleep the specified amount of time. |
||
| 377 | * |
||
| 378 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
| 379 | * |
||
| 380 | * @param $time |
||
| 381 | */ |
||
| 382 | |||
| 383 | public function sleep($time) |
||
| 397 | |||
| 398 | |||
| 399 | public function commandOpen($url) |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * @return LoggerInterface |
||
| 407 | */ |
||
| 408 | |||
| 409 | public function getLogger() |
||
| 413 | |||
| 414 | public function get($class) |
||
| 423 | |||
| 424 | public static function isPHPUnit5() |
||
| 428 | |||
| 429 | public function assertElementExists($selector, $by = 'byId') |
||
| 433 | |||
| 434 | public function assertTitleEquals($title) |
||
| 439 | |||
| 440 | |||
| 441 | public function assertTitleContains($title) |
||
| 446 | |||
| 447 | |||
| 448 | public function assertNotTitleEquals($title) |
||
| 453 | |||
| 454 | |||
| 455 | public function assertNotTitleContains($title) |
||
| 460 | |||
| 461 | public function assertURLEquals($url) |
||
| 466 | |||
| 467 | public function assertURLContains($url) |
||
| 472 | |||
| 473 | |||
| 474 | public function assertURLNotEquals($url) |
||
| 479 | |||
| 480 | public function assertURLNotContains($url) |
||
| 485 | |||
| 486 | protected function elementAssertion($selector, $by, $name) |
||
| 493 | |||
| 494 | public function assertElementDisplayed($selector, $by = 'byId') |
||
| 498 | |||
| 499 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
| 503 | |||
| 504 | public function assertElementNotExists($selector, $by = 'byId') |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return LoggingAssertionExecutor |
||
| 511 | */ |
||
| 512 | |||
| 513 | public function getAssertionLogger() |
||
| 517 | |||
| 518 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
| 541 | |||
| 542 | public static function assertWebDriverElement($element) |
||
| 546 | |||
| 547 | View Code Duplication | public function assertElementHasText($node, $text) |
|
| 556 | |||
| 557 | View Code Duplication | public function assertPageHasText($text) |
|
| 567 | |||
| 568 | public function assertPageNotHasText($text) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param $xpath |
||
| 581 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 582 | */ |
||
| 583 | |||
| 584 | public function byXpath($xpath) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param $id |
||
| 591 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 592 | */ |
||
| 593 | |||
| 594 | public function byId($id) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param $selector |
||
| 601 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 602 | */ |
||
| 603 | |||
| 604 | public function byCssSelector($selector) |
||
| 608 | |||
| 609 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param string $text |
||
| 631 | * @param string $specificNodeType |
||
| 632 | * @param string $parentElementSelector |
||
| 633 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 634 | */ |
||
| 635 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 640 | |||
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $text |
||
| 644 | * @param string $specificNodeType |
||
| 645 | * @param string $parentElementSelector |
||
| 646 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 647 | */ |
||
| 648 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return \Magium\Util\Translator\Translator |
||
| 656 | */ |
||
| 657 | |||
| 658 | public function getTranslator() |
||
| 662 | |||
| 663 | public function addTranslationCsvFile($file, $locale) |
||
| 667 | } |
||
| 668 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.