| Conditions | 9 |
| Paths | 37 |
| Total Lines | 58 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 106 | public function getMidPoint($axis, $sliceSize = 20) |
||
| 107 | { |
||
| 108 | if (!in_array($axis, ['x', 'y'])) { |
||
| 109 | throw new InvalidArgumentException('argument $axis must be either "x" or "y"'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $targetSize = $sliceSize; |
||
| 113 | |||
| 114 | $clone = clone($this->image); |
||
| 115 | $clone->modulateImage(100, 0, 100); |
||
| 116 | $clone->setImageColorspace(Imagick::COLORSPACE_GRAY); |
||
| 117 | $image = new CropEntropy($clone); |
||
| 118 | |||
| 119 | $size = $clone->getImageGeometry(); |
||
| 120 | $min = 0; |
||
| 121 | |||
| 122 | if ($axis === 'x') { |
||
| 123 | $max = $size['width']; |
||
| 124 | } else { |
||
| 125 | $max = $size['height']; |
||
| 126 | } |
||
| 127 | |||
| 128 | $a = null; |
||
| 129 | $b = null; |
||
| 130 | |||
| 131 | // until we have a slice that would fit inside the target size |
||
| 132 | do { |
||
| 133 | // Make sure that we don't try to slice outside the image |
||
| 134 | $sliceSize = min($max - $min - $targetSize, $sliceSize); |
||
| 135 | |||
| 136 | if ($a === null) { |
||
| 137 | if ($axis === 'x') { |
||
| 138 | $a = $image->getVerticalSlice($min - 1, $sliceSize); |
||
| 139 | } else { |
||
| 140 | $a = $image->getHorizontalSlice($min - 1, $sliceSize); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($b === null) { |
||
| 145 | if ($axis === 'x') { |
||
| 146 | $b = $image->getVerticalSlice($max - $sliceSize - 1, $sliceSize); |
||
| 147 | } else { |
||
| 148 | $b = $image->getHorizontalSlice($max - $sliceSize - 1, $sliceSize); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // a has higher energy, so create a new b slice, i.e move b $sliceSize against a |
||
| 153 | if ($a->compare($b) > 0) { |
||
| 154 | $max -= $sliceSize; |
||
| 155 | $b = null; |
||
| 156 | } else { |
||
| 157 | $min += $sliceSize; |
||
| 158 | $a = null; |
||
| 159 | } |
||
| 160 | } while ($max - $min > $targetSize); |
||
| 161 | |||
| 162 | return $min + ($sliceSize / 2); |
||
| 163 | } |
||
| 164 | |||
| 196 |