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:
1 | <?php |
||
9 | abstract class BaseContext extends RawMinkContext implements TranslatableContext |
||
10 | { |
||
11 | public static function getTranslationResources() |
||
15 | |||
16 | /** |
||
17 | * @transform /^(0|[1-9]\d*)(?:st|nd|rd|th)?$/ |
||
18 | */ |
||
19 | public function castToInt($count) |
||
23 | |||
24 | protected function not(Callable $callbable, $errorMessage) |
||
25 | { |
||
26 | try { |
||
27 | $callbable(); |
||
28 | } |
||
29 | catch (\Exception $e) { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | throw new ExpectationException($errorMessage, $this->getSession()); |
||
34 | } |
||
35 | |||
36 | protected function assert($test, $message) |
||
37 | { |
||
38 | if ($test === false) { |
||
39 | throw new ExpectationException($message, $this->getSession()); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | protected function assertContains($expected, $actual, $message = null) |
||
44 | { |
||
45 | $regex = '/' . preg_quote($expected, '/') . '/ui'; |
||
46 | |||
47 | $this->assert( |
||
48 | preg_match($regex, $actual) > 0, |
||
49 | $message ?: "The string '$expected' was not found." |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | protected function assertNotContains($expected, $actual, $message = null) |
||
54 | { |
||
55 | $message = $message ?: "The string '$expected' was found."; |
||
56 | |||
57 | $this->not(function () use($expected, $actual) { |
||
58 | $this->assertContains($expected, $actual); |
||
59 | }, $message); |
||
60 | } |
||
61 | |||
62 | protected function assertCount($expected, array $elements, $message = null) |
||
63 | { |
||
64 | $this->assert( |
||
65 | intval($expected) === count($elements), |
||
66 | $message ?: sprintf('%d elements found, but should be %d.', count($elements), $expected) |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | protected function assertEquals($expected, $actual, $message = null) |
||
71 | { |
||
72 | $this->assert( |
||
73 | $expected == $actual, |
||
74 | $message ?: "The element '$actual' is not equal to '$expected'" |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | protected function assertSame($expected, $actual, $message = null) |
||
79 | { |
||
80 | $this->assert( |
||
81 | $expected === $actual, |
||
82 | $message ?: "The element '$actual' is not equal to '$expected'" |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | protected function assertArrayHasKey($key, $array, $message = null) |
||
87 | { |
||
88 | $this->assert( |
||
89 | isset($array[$key]), |
||
90 | $message ?: "The array has no key '$key'" |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | protected function assertArrayNotHasKey($key, $array, $message = null) |
||
95 | { |
||
96 | $message = $message ?: "The array has key '$key'"; |
||
97 | |||
98 | $this->not(function () use($key, $array) { |
||
99 | $this->assertArrayHasKey($key, $array); |
||
100 | }, $message); |
||
101 | } |
||
102 | |||
103 | protected function assertTrue($value, $message = 'The value is false') |
||
107 | |||
108 | protected function assertFalse($value, $message = 'The value is true') |
||
109 | { |
||
110 | $this->not(function () use($value) { |
||
111 | $this->assertTrue($value); |
||
112 | }, $message); |
||
113 | } |
||
114 | |||
115 | protected function getMinkContext() |
||
116 | { |
||
117 | $context = new \Behat\MinkExtension\Context\MinkContext(); |
||
123 | |||
124 | View Code Duplication | protected function countElements($element, $index, $parent) |
|
137 | |||
138 | View Code Duplication | protected function findElement($selector, $locator, $index) |
|
150 | } |
||
151 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.