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 = null) |
||
100 | |||
101 | protected static $registrationCallbacks; |
||
102 | |||
103 | protected function setUp() |
||
122 | |||
123 | /** |
||
124 | * @return Di |
||
125 | */ |
||
126 | public function getDi() |
||
130 | |||
131 | /** |
||
132 | * @param Di $di |
||
133 | */ |
||
134 | public function setDi($di) |
||
138 | |||
139 | /** |
||
140 | * @return WebDriver |
||
141 | */ |
||
142 | public function getWebdriver() |
||
149 | |||
150 | /** |
||
151 | * @param WebDriver $webdriver |
||
152 | */ |
||
153 | public function setWebdriver(WebDriver $webdriver) |
||
157 | |||
158 | public function getInitializer() |
||
162 | |||
163 | |||
164 | public function __construct($name = null, array $data = [], $dataName = null, Initializer $initializer = null) |
||
170 | |||
171 | public function setUseErrorHandlerFromAnnotation() { |
||
181 | |||
182 | /** |
||
183 | * @return MasterListener |
||
184 | */ |
||
185 | |||
186 | public static function getMasterListener() |
||
197 | |||
198 | protected function tearDown() |
||
217 | |||
218 | |||
219 | public function filterWebDriverAction($by) |
||
232 | |||
233 | public function assertElementClickable($selector, $by = WebDriver::BY_ID) |
||
237 | |||
238 | |||
239 | public function assertElementNotClickable($selector, $by = WebDriver::BY_ID) |
||
244 | |||
245 | public static function addBaseNamespace($namespace) |
||
251 | |||
252 | public static function resolveClass( $class, $prefix = null) |
||
283 | |||
284 | public function setTypePreference($type, $preference) |
||
292 | |||
293 | protected function normalizeClassRequest($class) |
||
297 | |||
298 | public function addPostTestCallback($callback) |
||
305 | |||
306 | /** |
||
307 | |||
308 | * @param string $theme |
||
309 | * @return \Magium\Themes\ThemeConfigurationInterface |
||
310 | */ |
||
311 | |||
312 | public function getTheme($theme = null) |
||
320 | |||
321 | /** |
||
322 | * |
||
323 | * @param string $action |
||
324 | * @return mixed |
||
325 | */ |
||
326 | |||
327 | public function getAction($action) |
||
333 | |||
334 | |||
335 | /** |
||
336 | * @param string $name |
||
337 | * @return \Magium\Identities\NameInterface |
||
338 | */ |
||
339 | |||
340 | public function getIdentity($name = 'Customer') |
||
346 | |||
347 | /** |
||
348 | * |
||
349 | * @param string $navigator |
||
350 | * @return \Magium\Navigators\NavigatorInterface |
||
351 | */ |
||
352 | |||
353 | public function getNavigator($navigator = 'BaseMenu') |
||
359 | |||
360 | public function getAssertion($assertion) |
||
366 | |||
367 | /** |
||
368 | * |
||
369 | * @param string $extractor |
||
370 | * @return \Magium\Extractors\AbstractExtractor |
||
371 | */ |
||
372 | |||
373 | public function getExtractor($extractor) |
||
379 | |||
380 | /** |
||
381 | * Sleep the specified amount of time. |
||
382 | * |
||
383 | * Options: 1s (1 second), 1ms (1 millisecond), 1us (1 microsecond), 1ns (1 nanosecond) |
||
384 | * |
||
385 | * @param $time |
||
386 | */ |
||
387 | |||
388 | public function sleep($time) |
||
402 | |||
403 | |||
404 | public function commandOpen($url) |
||
408 | |||
409 | |||
410 | /** |
||
411 | * @return LoggerInterface |
||
412 | */ |
||
413 | |||
414 | public function getLogger() |
||
418 | |||
419 | public function get($class) |
||
428 | |||
429 | public static function isPHPUnit5() |
||
433 | |||
434 | public function assertElementExists($selector, $by = 'byId') |
||
438 | |||
439 | public function assertTitleEquals($title) |
||
444 | |||
445 | |||
446 | public function assertTitleContains($title) |
||
451 | |||
452 | |||
453 | public function assertNotTitleEquals($title) |
||
458 | |||
459 | |||
460 | public function assertNotTitleContains($title) |
||
465 | |||
466 | public function assertURLEquals($url) |
||
471 | |||
472 | public function assertURLContains($url) |
||
477 | |||
478 | |||
479 | public function assertURLNotEquals($url) |
||
484 | |||
485 | public function assertURLNotContains($url) |
||
490 | |||
491 | protected function elementAssertion($selector, $by, $name) |
||
498 | |||
499 | public function assertElementDisplayed($selector, $by = 'byId') |
||
503 | |||
504 | public function assertElementNotDisplayed($selector, $by = 'byId') |
||
508 | |||
509 | public function assertElementNotExists($selector, $by = 'byId') |
||
513 | |||
514 | /** |
||
515 | * @return LoggingAssertionExecutor |
||
516 | */ |
||
517 | |||
518 | public function getAssertionLogger() |
||
522 | |||
523 | public function switchThemeConfiguration($fullyQualifiedClassName) |
||
546 | |||
547 | public static function assertWebDriverElement($element) |
||
551 | |||
552 | View Code Duplication | public function assertElementHasText($node, $text) |
|
561 | |||
562 | View Code Duplication | public function assertPageHasText($text) |
|
572 | |||
573 | public function assertPageNotHasText($text) |
||
583 | |||
584 | /** |
||
585 | * @param $xpath |
||
586 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
587 | */ |
||
588 | |||
589 | public function byXpath($xpath) |
||
593 | |||
594 | /** |
||
595 | * @param $id |
||
596 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
597 | */ |
||
598 | |||
599 | public function byId($id) |
||
603 | |||
604 | /** |
||
605 | * @param $selector |
||
606 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
607 | */ |
||
608 | |||
609 | public function byCssSelector($selector) |
||
613 | |||
614 | protected function getElementByTextXpath($xpathTemplate, $text, $specificNodeType = null, $parentElementSelector = null) |
||
633 | |||
634 | /** |
||
635 | * @param string $text |
||
636 | * @param string $specificNodeType |
||
637 | * @param string $parentElementSelector |
||
638 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
639 | */ |
||
640 | public function byText($text, $specificNodeType = null, $parentElementSelector = null) |
||
645 | |||
646 | |||
647 | /** |
||
648 | * @param string $text |
||
649 | * @param string $specificNodeType |
||
650 | * @param string $parentElementSelector |
||
651 | * @return \Facebook\WebDriver\Remote\RemoteWebElement |
||
652 | */ |
||
653 | public function byContainsText($text, $specificNodeType = null, $parentElementSelector = null) |
||
658 | |||
659 | /** |
||
660 | * @return \Magium\Util\Translator\Translator |
||
661 | */ |
||
662 | |||
663 | public function getTranslator() |
||
667 | |||
668 | public function addTranslationCsvFile($file, $locale) |
||
672 | } |
||
673 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.