| Conditions | 15 |
| Paths | 577 |
| Total Lines | 78 |
| Lines | 18 |
| Ratio | 23.08 % |
| 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 |
||
| 78 | protected static function slice($image, $originalSize, $targetSize, $axis) |
||
| 79 | { |
||
| 80 | $aSlice = null; |
||
| 81 | $bSlice = null; |
||
| 82 | |||
| 83 | // Just an arbitrary size of slice size |
||
| 84 | $sliceSize = ceil(($originalSize - $targetSize) / 25); |
||
| 85 | |||
| 86 | $aBottom = $originalSize; |
||
| 87 | $aTop = 0; |
||
| 88 | |||
| 89 | // while there still are uninvestigated slices of the image |
||
| 90 | while (($aBottom - $aTop) > $targetSize) { |
||
| 91 | // Make sure that we don't try to slice outside the picture |
||
| 92 | $sliceSize = min($aBottom - $aTop - $targetSize, $sliceSize); |
||
| 93 | |||
| 94 | // Make a top slice image |
||
| 95 | View Code Duplication | if (!$aSlice) { |
|
|
|
|||
| 96 | $aSlice = clone $image; |
||
| 97 | |||
| 98 | if ($axis === 'h') { |
||
| 99 | $aSlice->cropImage($sliceSize, $originalSize, $aTop, 0); |
||
| 100 | } else { |
||
| 101 | $aSlice->cropImage($originalSize, $sliceSize, 0, $aTop); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | // Make a bottom slice image |
||
| 106 | View Code Duplication | if (!$bSlice) { |
|
| 107 | $bSlice = clone $image; |
||
| 108 | |||
| 109 | if ($axis === 'h') { |
||
| 110 | $bSlice->cropImage($sliceSize, $originalSize, $aBottom - $sliceSize, 0); |
||
| 111 | } else { |
||
| 112 | $bSlice->cropImage($originalSize, $sliceSize, 0, $aBottom - $sliceSize); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // calculate slices potential |
||
| 117 | $aPosition = (($axis === 'h') ? 'left' : 'top'); |
||
| 118 | $bPosition = (($axis === 'h') ? 'right' : 'bottom'); |
||
| 119 | |||
| 120 | $aPot = static::getPotential($aPosition, $aTop, $sliceSize); |
||
| 121 | $bPot = static::getPotential($bPosition, $aBottom, $sliceSize); |
||
| 122 | |||
| 123 | $canCutA = ($aPot <= 0); |
||
| 124 | $canCutB = ($bPot <= 0); |
||
| 125 | |||
| 126 | // if no slices are "cutable", we force if a slice has a lot of potential |
||
| 127 | if (!$canCutA && !$canCutB) { |
||
| 128 | if (($aPot * self::POTENTIAL_RATIO) < $bPot) { |
||
| 129 | $canCutA = true; |
||
| 130 | } elseif ($aPot > ($bPot * self::POTENTIAL_RATIO)) { |
||
| 131 | $canCutB = true; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // if we can only cut on one side |
||
| 136 | if ($canCutA xor $canCutB) { |
||
| 137 | if ($canCutA) { |
||
| 138 | $aTop += $sliceSize; |
||
| 139 | $aSlice = null; |
||
| 140 | } else { |
||
| 141 | $aBottom -= $sliceSize; |
||
| 142 | $bSlice = null; |
||
| 143 | } |
||
| 144 | } elseif (static::grayscaleEntropy($aSlice) < static::grayscaleEntropy($bSlice)) { |
||
| 145 | // bSlice has more entropy, so remove aSlice and bump aTop down |
||
| 146 | $aTop += $sliceSize; |
||
| 147 | $aSlice = null; |
||
| 148 | } else { |
||
| 149 | $aBottom -= $sliceSize; |
||
| 150 | $bSlice = null; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return $aTop; |
||
| 155 | } |
||
| 156 | |||
| 288 |
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.