Conditions | 15 |
Paths | 32 |
Total Lines | 41 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
19 | public static function calculate(Edge $edge, $inverse = false) |
||
20 | { |
||
21 | $width = $edge->image->getWidth(); |
||
22 | $height = $edge->image->getHeight(); |
||
23 | $offset = $inverse ? -1 : 1; |
||
24 | if ($edge->horizontal) { |
||
25 | $xOffset = $offset; |
||
26 | $yOffset = 0; |
||
27 | $xSampleOffset = 0; |
||
28 | $ySampleOffset = $edge->inward ? -1 : 1; |
||
29 | } else { |
||
30 | $xOffset = 0; |
||
31 | $yOffset = $offset; |
||
32 | $xSampleOffset = $edge->inward ? -1 : 1; |
||
33 | $ySampleOffset = 0; |
||
34 | } |
||
35 | $distance = 1; |
||
36 | $averageLuma = $edge->averageLuma; |
||
37 | for ($i = 0; $i < 10; $i++) { |
||
38 | $edgeX = $edge->x + $distance * $xOffset; |
||
39 | $edgeY = $edge->y + $distance * $yOffset; |
||
40 | if ($edgeX < 0 || $edgeX >= $width || $edgeY < 0 || $edgeY >= $height) { |
||
41 | break; |
||
42 | } |
||
43 | $sampleX = $edgeX + $xSampleOffset; |
||
44 | $sampleY = $edgeY + $ySampleOffset; |
||
45 | if ($sampleX < 0 || $sampleX >= $width || $sampleY < 0 || $sampleY >= $height) { |
||
46 | break; |
||
47 | } |
||
48 | $edgeColor = $edge->image->getColorAt($edgeX, $edgeY); |
||
49 | $sampleColor = $edge->image->getColorAt($sampleX, $sampleY); |
||
50 | $edgeLuma = Luma::compute($edgeColor); |
||
51 | $sampleLuma = Luma::compute($sampleColor); |
||
52 | $luma = ($edgeLuma + $sampleLuma) / 2; |
||
53 | if (abs($averageLuma - $luma) >= $edge->threshold) { |
||
54 | break; |
||
55 | } |
||
56 | $multiplier = (int) ($distance / 4); |
||
57 | $distance += 1 + $multiplier * $multiplier; |
||
58 | } |
||
59 | return $distance; |
||
60 | } |
||
68 |