Test Failed
Push — trunk ( dc8fe4...b76cfb )
by SuperNova.WS
10:59
created

ImageContainer::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/** Created by Gorlum 09.01.2024 15:59 */
3
4
namespace Tools;
5
6
/**
7
 * @property int $height
8
 * @property int $width
9
 */
10
class ImageContainer {
11
  private $height = -1;
12
  private $width = -1;
13
14
  /** @var resource|null $image */
15
  public $image = null;
16
17
  /**
18
   * @param string $file
19
   *
20
   * @return static|null
21
   */
22
  public static function load($file) {
23
    $image = @imagecreatefromstring(file_get_contents($file));
24
    if (!$image) {
25
      return null;
26
    }
27
28
    $that = new static();
29
30
    $that->image = $image;
0 ignored issues
show
Documentation Bug introduced by
It seems like $image can also be of type GdImage. However, the property $image is declared as type null|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...
31
    imagesavealpha($that->image, true);
32
33
    $that->width  = imagesx($that->image);
34
    $that->height = imagesy($that->image);
35
36
    return $that;
37
  }
38
39
  /**
40
   * @param int $width
41
   * @param int $height
42
   *
43
   * @return static
44
   */
45
  public static function create($width, $height) {
46
    $that = new static();
47
48
    $that->width  = $width;
49
    $that->height = $height;
50
51
    $that->imageReset();
52
53
    return $that;
54
  }
55
56
  public function __get($property) {
57
    if (in_array($property, ['height', 'width',]) && ($this->$property === -1)) {
58
      if (isset($this->image)) {
59
        $this->width  = imagesx($this->image);
60
        $this->height = imagesy($this->image);
61
      } else {
62
        $this->width = $this->height = 0;
63
      }
64
    }
65
66
    return property_exists($this, $property) ? $this->$property : null;
67
  }
68
69
  public function __destruct() {
70
    if (!empty($this->image)) {
71
      imagedestroy($this->image);
72
    }
73
  }
74
75
  /**
76
   * @param ImageContainer $anImage
77
   * @param int            $positionX
78
   * @param int            $positionY
79
   * @param int            $sourceX
80
   * @param int            $sourceY
81
   *
82
   * @return bool
83
   */
84
  public function copyFrom(ImageContainer $anImage, $positionX, $positionY, $sourceX = 0, $sourceY = 0) {
85
    return imagecopy($this->image, $anImage->image, $positionX, $positionY, $sourceX, $sourceY, $anImage->width, $anImage->height);
86
  }
87
88
  /**
89
   * @param string $string
90
   *
91
   * @return bool
92
   */
93
  public function savePng($string) {
94
    return imagepng($this->image, $string, 9);
95
  }
96
97
  /**
98
   * @return void
99
   */
100
  protected function imageReset() {
101
    if (!empty($this->image)) {
102
      imagedestroy($this->image);
103
    }
104
105
    $this->image = imagecreatetruecolor($this->width, $this->height);
0 ignored issues
show
Documentation Bug introduced by
It seems like imagecreatetruecolor($this->width, $this->height) can also be of type GdImage. However, the property $image is declared as type null|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...
106
    imagealphablending($this->image, true);
107
    imagesavealpha($this->image, true);
108
    $color = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
109
    imagefill($this->image, 0, 0, $color);
110
  }
111
112
}
113