ImageEditor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace HustleWorks\Chute;
5
6
use HustleWorks\Chute\Contracts\ImageEditingInterface;
7
use Intervention\Image\Constraint;
8
use Intervention\Image\ImageManager;
9
10
class ImageEditor implements ImageEditingInterface
11
{
12
    /**
13
     * @var \Intervention\Image\Image
14
     */
15
    private $image;
16
17
    /**
18
     * @var int
19
     */
20
    private $quality = 100;
21
22
    /**
23
     * ImageEditor constructor.
24
     *
25
     * @param $file
26
     */
27
    public function __construct($file)
28
    {
29
        $this->image = (new ImageManager())->make($file);
30
    }
31
32
    /**
33
     * Crop Image
34
     *
35
     * @param      $width
36
     * @param      $height
37
     * @param null $x_pos
38
     * @param null $y_pos
39
     * @return ImageEditingInterface
40
     */
41
    public function crop($width, $height, $x_pos = null, $y_pos = null): ImageEditingInterface
42
    {
43
        $this->image->crop($width, $height, $x_pos, $y_pos);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param $width
50
     * @param $height
51
     * @return ImageEditingInterface
52
     */
53
    public function resize($width, $height): ImageEditingInterface
54
    {
55
        $this->image->resize(
56
            $width,
57
            $height,
58
            !($width and $height) ? function (Constraint $constraint) { $constraint->aspectRatio(); } : null
0 ignored issues
show
Bug introduced by
It seems like !($width and $height) ? ...aspectRatio(); } : null can be null; however, resize() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
59
        );
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set Quality
66
     *
67
     * @param $quality
68
     * @return ImageEditingInterface
69
     */
70
    public function setQuality($quality): ImageEditingInterface
71
    {
72
        $this->quality = $quality;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get Stream
79
     *
80
     * Finalize edits and get the finished product.
81
     *
82
     * @return string
83
     */
84
    public function getStream(): string
85
    {
86
        return $this->image->stream(null, $this->quality);
87
    }
88
}