Conditions | 10 |
Paths | 1 |
Total Lines | 76 |
Code Lines | 47 |
Lines | 8 |
Ratio | 10.53 % |
Changes | 7 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
153 | public function testCompare() { |
||
154 | $this->doTest(function(CryptTool $cryptTool, $prefix) { |
||
155 | // make strings large enough |
||
156 | $string1 = str_repeat(Constants::myPrivateKey, 100000); |
||
157 | $string2 = str_repeat(Constants::otherPrivateKey, 100000); |
||
158 | echo PHP_EOL; |
||
159 | |||
160 | $humanDescr = [ |
||
161 | 'length' => 'different length', |
||
162 | 'diff' => 'same length, different content', |
||
163 | 'same' => 'same length, same content' |
||
164 | ]; |
||
165 | |||
166 | // test different strings when comparing and get time needed |
||
167 | $timeElapsed = []; |
||
168 | $comparisonResult = []; |
||
169 | $timeElapsedAvg = []; |
||
170 | foreach(array( |
||
171 | 'length' => [$string1, $string1 . 'a'], |
||
172 | 'diff' => [$string1, $string2], |
||
173 | 'same' => [$string1, $string1] |
||
174 | ) as $testName => $strings) { |
||
175 | $timeMin = [null, 0]; |
||
176 | $timeMax = [null, 0]; |
||
177 | for ($i=0; $i < 3; $i++) { |
||
178 | // test run with delay |
||
179 | $timeElapsed[$testName][$i] = -microtime(true); |
||
180 | $comparisonResult[$testName][$i] = $cryptTool->stringCompare($strings[0], $strings[1]); |
||
181 | $timeElapsed[$testName][$i] += microtime(true); |
||
182 | usleep(1000); |
||
183 | |||
184 | // debug output |
||
185 | echo $prefix.': '.$humanDescr[$testName].' #'.$i.': '.$timeElapsed[$testName][$i].'; result: '.$comparisonResult[$testName][$i].PHP_EOL; |
||
186 | |||
187 | // check result |
||
188 | if ($testName == 'length' || $testName == 'diff') { |
||
189 | $this->assertEquals(false, $comparisonResult[$testName][$i], $prefix.': comparison of "'.$humanDescr[$testName].' #'.$i.'" is wrong: expected: false, got '.$comparisonResult[$testName][$i]); |
||
190 | } else { |
||
191 | $this->assertEquals(true, $comparisonResult[$testName][$i], $prefix.': comparison of "'.$humanDescr[$testName].' #'.$i.'" is wrong: expected: true, got '.$comparisonResult[$testName][$i]); |
||
192 | } |
||
193 | |||
194 | // get min/max values for later normalisation |
||
195 | View Code Duplication | if (is_null($timeMin[0]) || $timeElapsed[$testName][$i] < $timeMin[0]) { |
|
196 | $timeMin[0] = $timeElapsed[$testName][$i]; //minimum value |
||
197 | $timeMin[1] = $i; //index |
||
198 | } |
||
199 | View Code Duplication | if (is_null($timeMax[0]) || $timeElapsed[$testName][$i] > $timeMax[0]) { |
|
200 | $timeMax[0] = $timeElapsed[$testName][$i]; //maxiumum value |
||
201 | $timeMax[1] = $i; //index |
||
202 | } |
||
203 | } |
||
204 | |||
205 | // remove min/max value from array to normalize value |
||
206 | array_splice($timeElapsed[$testName], $timeMin[1], 1); |
||
207 | array_splice($timeElapsed[$testName], $timeMax[1] >= $timeMin[1] ? ($timeMax[1]-1) : $timeMax[1], 1); |
||
208 | |||
209 | // calculate average (currently useless as there is only one value left, but it may be useful later) |
||
210 | $timeElapsedAvg[$testName] = array_sum($timeElapsed[$testName]) / count($timeElapsed[$testName]); |
||
211 | } |
||
212 | |||
213 | // check timings |
||
214 | echo 'Timing test results with '.$prefix.':'.PHP_EOL; |
||
215 | $timingRatio = $timeElapsedAvg['diff'] / $timeElapsedAvg['same']; |
||
216 | $absoluteDifference = abs($timeElapsedAvg['diff'] - $timeElapsedAvg['same']); |
||
217 | echo 'timing ratio: '.$timingRatio.PHP_EOL; |
||
218 | echo 'absolute difference: '.$absoluteDifference.PHP_EOL; |
||
219 | |||
220 | // only allow 25% relative difference of two values |
||
221 | $allowedDifference = 0.25; |
||
222 | $this->assertLessThan(1+$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Ratio: '.$timingRatio); |
||
223 | $this->assertGreaterThan(1-$allowedDifference, $timingRatio, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too low. Ratio: '.$timingRatio); |
||
224 | |||
225 | // make sure the absolute difference is smaller than 1 second! |
||
226 | $this->assertLessThan(1, $absoluteDifference, $prefix.': difference of comparison ration of "'.$humanDescr['diff'].'" compared to "'.$humanDescr['same'].'" is too high. Value is: '.$absoluteDifference.' micro seconds'); |
||
227 | }); |
||
228 | } |
||
229 | |||
264 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.