| Conditions | 9 |
| Paths | 40 |
| Total Lines | 53 |
| Code Lines | 30 |
| 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 |
||
| 44 | public function execute($img, $channels) |
||
| 45 | { |
||
| 46 | $blank = array('red' => 0, 'green' => 0, 'blue' => 0); |
||
| 47 | |||
| 48 | if (isset($channels['alpha'])) { |
||
| 49 | unset($channels['alpha']); |
||
| 50 | } |
||
| 51 | |||
| 52 | $width = $img->getWidth(); |
||
| 53 | $height = $img->getHeight(); |
||
| 54 | $copy = PaletteImage::create($width, $height); |
||
| 55 | |||
| 56 | if ($img->isTransparent()) { |
||
| 57 | $otci = $img->getTransparentColor(); |
||
| 58 | $TRGB = $img->getColorRGB($otci); |
||
| 59 | $tci = $copy->allocateColor($TRGB); |
||
| 60 | } else { |
||
| 61 | $otci = null; |
||
| 62 | $tci = null; |
||
| 63 | } |
||
| 64 | |||
| 65 | for ($x = 0; $x < $width; $x++) { |
||
| 66 | for ($y = 0; $y < $height; $y++) { |
||
| 67 | $ci = $img->getColorAt($x, $y); |
||
| 68 | |||
| 69 | if ($ci === $otci) { |
||
| 70 | $copy->setColorAt($x, $y, $tci); |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | $RGB = $img->getColorRGB($ci); |
||
| 75 | |||
| 76 | $newRGB = $blank; |
||
| 77 | |||
| 78 | foreach ($channels as $channel) { |
||
| 79 | $newRGB[$channel] = $RGB[$channel]; |
||
| 80 | } |
||
| 81 | |||
| 82 | $color = $copy->getExactColor($newRGB); |
||
| 83 | |||
| 84 | if ($color == -1) { |
||
| 85 | $color = $copy->allocateColor($newRGB); |
||
| 86 | } |
||
| 87 | |||
| 88 | $copy->setColorAt($x, $y, $color); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($img->isTransparent()) { |
||
| 93 | $copy->setTransparentColor($tci); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $copy; |
||
| 97 | } |
||
| 99 |