| Conditions | 15 |
| Paths | 22 |
| Total Lines | 100 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 100 | public function renderAvatar(int $size = 0, string $type = 'F'): void |
||
| 101 | { |
||
| 102 | if ($size <= 0 || $size > env('MAX_AVATAR_SIZE')) { |
||
| 103 | $size = (int) env('DEFAULT_AVATAR_SIZE'); |
||
| 104 | } |
||
| 105 | // generate png from url/path |
||
| 106 | $image = \imagecreatefrompng($this->skinPath); |
||
| 107 | \imagealphablending($image, false); |
||
|
|
|||
| 108 | \imagesavealpha($image, true); |
||
| 109 | |||
| 110 | // Head |
||
| 111 | $this->imgResource = \imagecreatetruecolor($size, $size); |
||
| 112 | |||
| 113 | // Helm |
||
| 114 | $helm_check = \imagecreatetruecolor($size, $size); |
||
| 115 | \imagealphablending($helm_check, false); |
||
| 116 | \imagesavealpha($helm_check, true); |
||
| 117 | $transparent = \imagecolorallocatealpha($helm_check, 255, 255, 255, 127); |
||
| 118 | \imagefilledrectangle($helm_check, 0, 0, 8, 8, $transparent); |
||
| 119 | |||
| 120 | switch ($type) { |
||
| 121 | case 'F': |
||
| 122 | // Avatar front |
||
| 123 | $sectionSrcX = 8; |
||
| 124 | $sectionSrcY = 8; |
||
| 125 | |||
| 126 | // Avatar Helm front |
||
| 127 | $sectionHelmSrcX = 40; |
||
| 128 | $sectionHelmSrcY = 8; |
||
| 129 | |||
| 130 | break; |
||
| 131 | case 'B': |
||
| 132 | // Avatar back |
||
| 133 | $sectionSrcX = 24; |
||
| 134 | $sectionSrcY = 8; |
||
| 135 | |||
| 136 | // Avatar Helm back |
||
| 137 | $sectionHelmSrcX = 56; |
||
| 138 | $sectionHelmSrcY = 8; |
||
| 139 | |||
| 140 | break; |
||
| 141 | case 'R': |
||
| 142 | // Avatar right |
||
| 143 | $sectionSrcX = 0; |
||
| 144 | $sectionSrcY = 8; |
||
| 145 | |||
| 146 | // Avatar Helm right |
||
| 147 | $sectionHelmSrcX = 32; |
||
| 148 | $sectionHelmSrcY = 8; |
||
| 149 | |||
| 150 | break; |
||
| 151 | case 'L': |
||
| 152 | // Avatar left |
||
| 153 | $sectionSrcX = 16; |
||
| 154 | $sectionSrcY = 8; |
||
| 155 | |||
| 156 | // Avatar Helm left |
||
| 157 | $sectionHelmSrcX = 56; |
||
| 158 | $sectionHelmSrcY = 8; |
||
| 159 | |||
| 160 | break; |
||
| 161 | case 'T': |
||
| 162 | // Avatar right |
||
| 163 | $sectionSrcX = 8; |
||
| 164 | $sectionSrcY = 0; |
||
| 165 | |||
| 166 | // Avatar Helm right |
||
| 167 | $sectionHelmSrcX = 40; |
||
| 168 | $sectionHelmSrcY = 0; |
||
| 169 | break; |
||
| 170 | default: |
||
| 171 | // TODO: Custom exception |
||
| 172 | throw new \Exception('Invalid avatar section specified'); |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | |||
| 176 | @\imagecopyresampled($this->imgResource, $image, 0, 0, $sectionSrcX, $sectionSrcY, $size, $size, 8, 8); |
||
| 177 | @\imagecopyresampled($helm_check, $image, 0, 0, $sectionHelmSrcX, $sectionHelmSrcY, 8, 8, 8, 8); |
||
| 178 | |||
| 179 | $this->calculateHelmStandardDeviation($helm_check); |
||
| 180 | |||
| 181 | // if all pixel have transparency or the colors aren't the same |
||
| 182 | if (( |
||
| 183 | ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION) || |
||
| 184 | ($this->redStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION) || |
||
| 185 | ($this->greenStdDev > self::DEFAULT_STANDARD_DEVIATION && $this->blueStdDev > self::DEFAULT_STANDARD_DEVIATION) |
||
| 186 | ) || |
||
| 187 | ($this->meanAlpha === 127)) { |
||
| 188 | $helm = \imagecreatetruecolor($size, $size); |
||
| 189 | \imagealphablending($helm, false); |
||
| 190 | \imagesavealpha($helm, true); |
||
| 191 | $transparent = \imagecolorallocatealpha($helm, 255, 255, 255, 127); |
||
| 192 | \imagefilledrectangle($helm, 0, 0, $size, $size, $transparent); |
||
| 193 | \imagecopyresampled($helm, $image, 0, 0, $sectionHelmSrcX, $sectionHelmSrcY, $size, $size, 8, 8); |
||
| 194 | $merge = \imagecreatetruecolor($size, $size); |
||
| 195 | \imagecopy($merge, $this->imgResource, 0, 0, 0, 0, $size, $size); |
||
| 196 | \imagecopy($merge, $helm, 0, 0, 0, 0, $size, $size); |
||
| 197 | \imagecopymerge($this->imgResource, $merge, 0, 0, 0, 0, $size, $size, 0); |
||
| 198 | $this->imgResource = $merge; |
||
| 199 | unset($merge); |
||
| 200 | } |
||
| 203 |