Passed
Push — master ( 6ffe87...662eee )
by Mattia
03:26
created

Skin::render()   B

Complexity

Conditions 10
Paths 72

Size

Total Lines 66
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 43
nc 72
nop 2
dl 0
loc 66
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 App\Image\Sections;
6
7
use App\Image\Exceptions\ImageResourceCreationFailedException;
8
use App\Image\ImageSection;
9
10
class Skin extends ImageSection
11
{
12
    /**
13
     * Create a PNG with raw texture.
14
     *
15
     * @throws \App\Image\Exceptions\ImageCreateFromPngFailedException
16
     */
17
    public function prepareTextureDownload(): void
18
    {
19
        $this->imgResource = $this->createImageFromPng($this->skinPath);
20
        \imagealphablending($this->imgResource, true);
21
        \imagesavealpha($this->imgResource, true);
22
    }
23
24
    /**
25
     * Render skin.
26
     *
27
     * @param int
28
     * @param string
29
     *
30
     * @throws \Throwable
31
     */
32
    public function render(int $skin_height = 256, $type = self::FRONT): void
33
    {
34
        if ($type !== self::BACK) {
35
            $type = self::FRONT;
36
        }
37
        if ($skin_height === 0 || $skin_height < 0 || $skin_height > (int) env('MAX_SKINS_SIZE')) {
38
            $skin_height = (int) env('DEFAULT_SKIN_SIZE');
39
        }
40
41
        $image = $this->createImageFromPng($this->skinPath);
42
        $scale = $skin_height / 32;
43
        if ($scale === 0) {
44
            $scale = 1;
45
        }
46
        $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...
47
        if ($this->imgResource === false) {
48
            throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed');
49
        }
50
        \imagealphablending($this->imgResource, false);
51
        \imagesavealpha($this->imgResource, true);
52
        $transparent = \imagecolorallocatealpha($this->imgResource, 255, 255, 255, 127);
53
        \imagefilledrectangle($this->imgResource, 0, 0, 16 * $scale, 32 * $scale, $transparent);
54
55
        $tmpAvatar = new Avatar($this->skinPath);
56
        $tmpAvatar->render(8, $type);
57
        // Front
58
        if ($type === self::FRONT) {
59
            // Head
60
            \imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8);
61
            // Body Front
62
            \imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 20, 20, 8 * $scale, 12 * $scale, 8, 12);
63
            // Right Arm (left on img)
64
            $r_arm = \imagecreatetruecolor(4, 12);
65
            \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

65
            \imagecopy(/** @scrutinizer ignore-type */ $r_arm, $image, 0, 0, 44, 20, 4, 12);
Loading history...
66
            \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

66
            \imagecopyresized($this->imgResource, /** @scrutinizer ignore-type */ $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
Loading history...
67
            // Right leg (left on img)
68
            $r_leg = \imagecreatetruecolor(4, 20);
69
            \imagecopy($r_leg, $image, 0, 0, 4, 20, 4, 12);
70
            \imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
71
        } else {
72
            // Head
73
            \imagecopyresized($this->imgResource, $tmpAvatar->getResource(), 4 * $scale, 0 * $scale, 0, 0, 8 * $scale, 8 * $scale, 8, 8);
74
            // Body Back
75
            \imagecopyresized($this->imgResource, $image, 4 * $scale, 8 * $scale, 32, 20, 8 * $scale, 12 * $scale, 8, 12);
76
            // Right Arm Back (left on img)
77
            $r_arm = \imagecreatetruecolor(4, 12);
78
            \imagecopy($r_arm, $image, 0, 0, 52, 20, 4, 12);
79
            \imagecopyresized($this->imgResource, $r_arm, 0 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
80
            // Right leg Back (left on img)
81
            $r_leg = \imagecreatetruecolor(4, 20);
82
            \imagecopy($r_leg, $image, 0, 0, 12, 20, 4, 12);
83
            \imagecopyresized($this->imgResource, $r_leg, 4 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
84
        }
85
86
        // Left Arm (right flipped)
87
        $l_arm = \imagecreatetruecolor(4, 12);
88
        for ($x = 0; $x < 4; ++$x) {
89
            \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

89
            \imagecopy($l_arm, /** @scrutinizer ignore-type */ $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12);
Loading history...
90
        }
91
        \imagecopyresized($this->imgResource, $l_arm, 12 * $scale, 8 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
92
        // Left leg (right flipped)
93
        $l_leg = \imagecreatetruecolor(4, 20);
94
        for ($x = 0; $x < 4; ++$x) {
95
            \imagecopy($l_leg, $r_leg, $x, 0, 4 - $x - 1, 0, 1, 20);
96
        }
97
        \imagecopyresized($this->imgResource, $l_leg, 8 * $scale, 20 * $scale, 0, 0, 4 * $scale, 12 * $scale, 4, 12);
98
    }
99
}
100