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 |
||
27 | class GigyaTest extends TestCase |
||
28 | { |
||
29 | /** |
||
30 | * @var Gigya |
||
31 | */ |
||
32 | protected $gigya; |
||
33 | |||
34 | /** |
||
35 | * @var float |
||
36 | */ |
||
37 | private $time; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | private $memory; |
||
43 | |||
44 | public function createBasicHandler() |
||
53 | |||
54 | public function createAccountInfoHandler() |
||
93 | |||
94 | private function startBenchmark() |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | private function endBenchmark() |
||
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * @param int $iterations |
||
114 | */ |
||
115 | private function printBenchmark($name, $iterations) |
||
126 | |||
127 | public function testSingleCall() |
||
128 | { |
||
129 | $this->createBasicHandler(); |
||
130 | $this->startBenchmark(); |
||
131 | |||
132 | $this->gigya->accounts()->getAccountInfo(['uid' => 'some_uid']); |
||
133 | |||
134 | $this->printBenchmark(__METHOD__, 1); |
||
135 | |||
136 | list($duration) = $this->endBenchmark(); |
||
137 | static::assertLessThan( |
||
138 | 1000, |
||
139 | $duration, |
||
140 | 'An individual request should take less than 2s of prep and response validation' |
||
141 | ); |
||
142 | } |
||
143 | |||
144 | View Code Duplication | public function testSingleChildCall() |
|
|
|||
145 | { |
||
146 | $this->createBasicHandler(); |
||
147 | $this->startBenchmark(); |
||
148 | |||
149 | $num = 1000; |
||
150 | for ($i = 0; $i < $num; $i++) { |
||
151 | $this->gigya->accounts()->getAccountInfo(['uid' => $i]); |
||
152 | } |
||
153 | |||
154 | $this->printBenchmark(__METHOD__, $num); |
||
155 | |||
156 | list($duration) = $this->endBenchmark(); |
||
157 | static::assertLessThan( |
||
158 | 1000, |
||
159 | $duration, |
||
160 | 'An individual request should take less than 2s of prep and response validation' |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | View Code Duplication | public function testDoubleChildCall() |
|
183 | |||
184 | View Code Duplication | public function testUidValidationResponse() |
|
185 | { |
||
202 | } |
||
203 |
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.