| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Minepic\Image\Sections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Minepic\Image\Exceptions\ImageResourceCreationFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Minepic\Image\ImageSection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Skin extends ImageSection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private function checkType(string $type): string | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         if ($type !== self::BACK) { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $type = self::FRONT; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         return $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param $skinHeight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private function checkHeight($skinHeight): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if ($skinHeight === 0 || $skinHeight < 0 || $skinHeight > (int) env('MAX_SKINS_SIZE')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $skinHeight = (int) env('DEFAULT_SKIN_SIZE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return $skinHeight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Create a PNG with raw texture. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws \Minepic\Image\Exceptions\ImageCreateFromPngFailedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function prepareTextureDownload(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->imgResource = $this->createImageFromPng($this->skinPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         \imagealphablending($this->imgResource, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         \imagesavealpha($this->imgResource, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Render skin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @throws \Throwable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function render(int $skin_height = 256, $type = self::FRONT): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $type = $this->checkType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $skin_height = $this->checkHeight($skin_height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $image = $this->createImageFromPng($this->skinPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $scale = $skin_height / 32; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($scale === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $scale = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->imgResource = \imagecreatetruecolor(16 * $scale, 32 * $scale); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($this->imgResource === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         \imagealphablending($this->imgResource, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         \imagesavealpha($this->imgResource, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $transparent = \imagecolorallocatealpha($this->imgResource, 255, 255, 255, 127); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         \imagefilledrectangle($this->imgResource, 0, 0, 16 * $scale, 32 * $scale, $transparent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $tmpAvatar = new Avatar($this->skinPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $tmpAvatar->render(8, $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Front | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($type === self::FRONT) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             // Head | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             \imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // Body Front | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             \imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 20, 20, 8 * $scale, 12 * $scale, 8, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             // Right Arm (left on img) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $r_arm = \imagecreatetruecolor(4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             \imagecopy($r_arm, $image, 0, 0, 44, 20, 4, 12); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             \imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             // Right leg (left on img) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $r_leg = \imagecreatetruecolor(4, 20); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             \imagecopy($r_leg, $image, 0, 0, 4, 20, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             \imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             // Head | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             \imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             // Body Back | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             \imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 32, 20, 8 * $scale, 12 * $scale, 8, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             // Right Arm Back (left on img) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $r_arm = \imagecreatetruecolor(4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             \imagecopy($r_arm, $image, 0, 0, 52, 20, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             \imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             // Right leg Back (left on img) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $r_leg = \imagecreatetruecolor(4, 20); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             \imagecopy($r_leg, $image, 0, 0, 12, 20, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             \imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // Left Arm (right flipped) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $l_arm = \imagecreatetruecolor(4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         for ($x = 0; $x < 4; ++$x) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             \imagecopy($l_arm, $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         \imagecopyresized($this->imgResource, $l_arm, 12 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // Left leg (right flipped) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $l_leg = \imagecreatetruecolor(4, 20); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         for ($x = 0; $x < 4; ++$x) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             \imagecopy($l_leg, $r_leg, $x, 0, 4 - $x - 1, 0, 1, 20); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         \imagecopyresized($this->imgResource, $l_leg, 8 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                        
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.