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 |
||
8 | class TestTypeTest extends TestCase |
||
9 | { |
||
10 | public function testIsStringNotNullOrEmptyWithNull() |
||
16 | |||
17 | public function testIsStringNotNullOrEmptyWithEmpty() |
||
23 | |||
24 | public function testIsStringNotNullOrEmptyWithNonString() |
||
30 | |||
31 | public function testIsStringNotNullOrEmptyWithObject() |
||
37 | |||
38 | public function testIsStringNotNullOrEmptyWithNumber() |
||
44 | |||
45 | public function testIsStringNotNullOrEmptyWithString() |
||
51 | |||
52 | public function testIsStringNotNullOrEmptyWithNumberAsString() |
||
58 | |||
59 | View Code Duplication | public function testIsNotNullInstanceOfWhenPassedObjectToCheck() |
|
|
|||
60 | { |
||
61 | $var = new \stdClass(); |
||
62 | $instanceof = new \stdClass(); |
||
63 | |||
64 | $foo = new testType(); |
||
65 | $this->assertTrue($foo->isNotNullInstanceOf($var, $instanceof)); |
||
66 | } |
||
67 | |||
68 | View Code Duplication | public function testIsNotNullInstanceOfWhenPassedStringToCheck() |
|
69 | { |
||
70 | $var = new \stdClass(); |
||
71 | $instanceof = get_class($var); |
||
72 | |||
73 | $foo = new testType(); |
||
74 | $this->assertTrue($foo->isNotNullInstanceOf($var, $instanceof)); |
||
75 | } |
||
76 | |||
77 | View Code Duplication | public function testIsNotNullInstanceOfWhenPassedNonObjectToCheck() |
|
78 | { |
||
79 | $var = 'Another string. How amazing.'; |
||
80 | $instanceof = new \stdClass(); |
||
81 | |||
82 | $foo = new testType(); |
||
83 | $this->assertFalse($foo->isNotNullInstanceOf($var, $instanceof)); |
||
84 | } |
||
85 | |||
86 | View Code Duplication | public function testIsNotNullInstanceOfWhenPassedNullToCheck() |
|
87 | { |
||
88 | $var = null; |
||
89 | $instanceof = new \stdClass(); |
||
90 | |||
91 | $foo = new testType(); |
||
92 | $this->assertFalse($foo->isNotNullInstanceOf($var, $instanceof)); |
||
93 | } |
||
94 | |||
95 | public function testIsNullInstanceOfWhenPassedObjectToCheck() |
||
103 | |||
104 | View Code Duplication | public function testIsNullInstanceOfWhenPassedStringToCheck() |
|
112 | |||
113 | public function testIsNullInstanceOfWhenPassedNonObjectToCheck() |
||
121 | |||
122 | View Code Duplication | public function testIsNullInstanceOfWhenPassedNullToCheck() |
|
123 | { |
||
124 | $var = null; |
||
125 | $instanceof = new \stdClass(); |
||
126 | |||
127 | $foo = new testType(); |
||
128 | $this->assertTrue($foo->isNullInstanceOf($var, $instanceof)); |
||
129 | } |
||
130 | |||
131 | View Code Duplication | public function testIsValidArrayNotEnoughBitz() |
|
139 | |||
140 | View Code Duplication | public function testIsValidArrayTooManyBitz() |
|
148 | |||
149 | View Code Duplication | public function testIsValidArrayWrongType() |
|
157 | |||
158 | View Code Duplication | public function testIsValidArrayRightType() |
|
166 | |||
167 | public function testIsChildArrayOkForEmptyArray() |
||
175 | |||
176 | public function testIsChildArrayOkForNonEmptyArrayWithBadGubbins() |
||
186 | |||
187 | public function testIsChildArrayOkForNonEmptyArrayWithGoodGubbinsNotOk() |
||
204 | |||
205 | public function testIsUrlValidWithNull() |
||
212 | |||
213 | public function testIsUrlValidWithNonUrlString() |
||
220 | |||
221 | public function testIsUrlValidWithUrlString() |
||
228 | |||
229 | public function testIsUrlValidWithUrlStringWithWWW() |
||
236 | |||
237 | public function testObjectNullOrOkWithNullObject() |
||
238 | { |
||
239 | $msg = null; |
||
240 | $obj = null; |
||
241 | $foo = new testType(); |
||
244 | |||
245 | public function testObjectNullOrOkWithIsOkObjectActuallyOk() |
||
257 | |||
258 | public function testObjectNullOrOkWithIsOkObjectNotOk() |
||
272 | |||
273 | public function testIsValidArrayOkWhenNotValidArray() |
||
283 | |||
284 | public function testIsValidArrayOkWhenChildArrayNotOK() |
||
297 | |||
298 | public function testIsValidArrayOkWhenChildArrayIsOk() |
||
311 | } |
||
312 |
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.