Passed
Push — master ( 9aa048...898b5e )
by Mattia
04:05
created

Skin::render()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 41
c 1
b 0
f 0
nc 18
nop 2
dl 0
loc 62
rs 8.6417

How to fix   Long Method   

Long Method

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:

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);
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