Passed
Push — master ( e6b90f...ace279 )
by Mattia
03:45
created

Skin   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 112
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareTextureDownload() 0 5 1
A checkType() 0 7 2
A checkHeight() 0 7 4
B render() 0 62 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Image\Sections;
6
7
use App\Image\Exceptions\ImageResourceCreationFailedException;
8
use App\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 \App\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);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagecreatetruecolor(16 * $scale, 32 * $scale) can also be of type false. However, the property $imgResource is declared as type resource. Maybe add an additional type check?

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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $dst_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            \imagecopy(/** @scrutinizer ignore-type */ $r_arm, $image, 0, 0, 44, 20, 4, 12);
Loading history...
90
            \imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
            \imagecopyresized($this->imgResource, /** @scrutinizer ignore-type */ $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            \imagecopy($l_arm, /** @scrutinizer ignore-type */ $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12);
Loading history...
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