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 | protected static $registrationCallbacks; |
||
| 64 | |||
| 65 | protected function setUp() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return Di |
||
| 87 | */ |
||
| 88 | public function getDi() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Di $di |
||
| 95 | */ |
||
| 96 | public function setDi($di) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return WebDriver |
||
| 103 | */ |
||
| 104 | public function getWebdriver() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param WebDriver $webdriver |
||
| 114 | */ |
||
| 115 | public function setWebdriver(WebDriver $webdriver) |
||
| 119 | |||
| 120 | public function getInitializer() |
||
| 124 | |||
| 125 | |||
| 126 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
| 132 | |||
| 133 | public function setUseErrorHandlerFromAnnotation() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return MasterListener |
||
| 146 | */ |
||
| 147 | |||
| 148 | public static function getMasterListener() |
||
| 159 | |||
| 160 | protected function tearDown() |
||
| 179 | |||
| 180 | |||
| 181 | public function filterWebDriverAction($by) |
||
| 194 | |||
| 195 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
| 199 | |||
| 200 | |||
| 201 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
| 206 | |||
| 207 | public static function addBaseNamespace($namespace) |
||
| 213 | |||
| 214 | public static function resolveClass( $class, $prefix = null) |
||
| 245 | |||
| 246 | public function setTypePreference($type, $preference) |
||
| 254 | |||
| 255 | protected function normalizeClassRequest($class) |
||
| 259 | |||
| 260 | public function addPostTestCallback($callback) |
||
| 267 | |||
| 268 | /** |
||
| 269 | |||
| 270 | * @param string $theme |
||
| 271 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
| 272 | */ |
||
| 273 | |||
| 274 | public function getTheme($theme = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * |
||
| 285 | * @param string $action |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | |||
| 289 | public function getAction($action) |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $name |
||
| 299 | * @return \Magium\Identities\NameInterface |
||
| 300 | */ |
||
| 301 | |||
| 302 | public function getIdentity($name = 'Customer') |
||
| 308 | |||
| 309 | /** |
||
| 310 | * |
||
| 311 | * @param string $navigator |
||
| 312 | * @return \Magium\Navigators\NavigatorInterface |
||
| 313 | */ |
||
| 314 | |||
| 315 | public function getNavigator($navigator = 'BaseMenu') |
||
| 321 | |||
| 322 | public function getAssertion($assertion) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * |
||
| 331 | * @param string $extractor |
||
| 332 | * @return \Magium\Extractors\AbstractExtractor |
||
| 333 | */ |
||
| 334 | |||
| 335 | public function getExtractor($extractor) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Sleep the specified amount of time. |
||
| 344 | * |
||
| 345 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
| 346 | * |
||
| 347 | * @param $time |
||
| 348 | */ |
||
| 349 | |||
| 350 | public function sleep($time) |
||
| 364 | |||
| 365 | |||
| 366 | public function commandOpen($url) |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @return LoggerInterface |
||
| 374 | */ |
||
| 375 | |||
| 376 | public function getLogger() |
||
| 380 | |||
| 381 | public function get($class) |
||
| 390 | |||
| 391 | public static function isPHPUnit5() |
||
| 395 | |||
| 396 | public function assertElementExists($selector, $by = 'byId') |
||
| 400 | |||
| 401 | public function assertTitleEquals($title) |
||
| 406 | |||
| 407 | |||
| 408 | public function assertTitleContains($title) |
||
| 413 | |||
| 414 | |||
| 415 | public function assertNotTitleEquals($title) |
||
| 420 | |||
| 421 | |||
| 422 | public function assertNotTitleContains($title) |
||
| 427 | |||
| 428 | public function assertURLEquals($url) |
||
| 433 | |||
| 434 | public function assertURLContains($url) |
||
| 439 | |||
| 440 | |||
| 441 | public function assertURLNotEquals($url) |
||
| 446 | |||
| 447 | public function assertURLNotContains($url) |
||
| 452 | |||
| 453 | protected function elementAssertion($selector, $by, $name) |
||
| 460 | |||
| 461 | public function assertElementDisplayed($selector, $by = 'byId') |
||
| 465 | |||
| 466 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
| 470 | |||
| 471 | public function assertElementNotExists($selector, $by = 'byId') |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return LoggingAssertionExecutor |
||
| 478 | */ |
||
| 479 | |||
| 480 | public function getAssertionLogger() |
||
| 484 | |||
| 485 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
| 508 | |||
| 509 | public static function assertWebDriverElement($element) |
||
| 513 | |||
| 514 | View Code Duplication | public function assertElementHasText($node, $text) |
|
| 523 | |||
| 524 | View Code Duplication | public function assertPageHasText($text) |
|
| 534 | |||
| 535 | public function assertPageNotHasText($text) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param $xpath |
||
| 548 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 549 | */ |
||
| 550 | |||
| 551 | public function byXpath($xpath) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param $id |
||
| 558 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 559 | */ |
||
| 560 | |||
| 561 | public function byId($id) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param $selector |
||
| 568 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 569 | */ |
||
| 570 | |||
| 571 | public function byCssSelector($selector) |
||
| 575 | |||
| 576 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @param string $text |
||
| 598 | * @param string $specificNodeType |
||
| 599 | * @param string $parentElementSelector |
||
| 600 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 601 | */ |
||
| 602 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 607 | |||
| 608 | |||
| 609 | /** |
||
| 610 | * @param string $text |
||
| 611 | * @param string $specificNodeType |
||
| 612 | * @param string $parentElementSelector |
||
| 613 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
| 614 | */ |
||
| 615 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return \Magium\Util\Translator\Translator |
||
| 623 | */ |
||
| 624 | |||
| 625 | public function getTranslator() |
||
| 629 | |||
| 630 | public function addTranslationCsvFile($file, $locale) |
||
| 634 | } |
||
| 635 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.