Completed
Push — master ( c16185...55c0ed )
by Daniel
11:54
created

AbstractImage::getPalette()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 0
crap 20
1
<?php
2
/**
3
 * This file is part of the Magickly project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Magickly\Image;
9
10
use GravityMedia\Magickly\Enum\ColorSpace;
11
use GravityMedia\Magickly\Image\Palette\PaletteInterface;
12
13
/**
14
 * Abstract image class.
15
 *
16
 * @package GravityMedia\Magickly\Image
17
 */
18
abstract class AbstractImage implements ImageInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getPalette()
24
    {
25
        switch ($this->getColorSpace()) {
26
            default:
27
                $palette = new Palette\RGB();
28
                break;
29
            case ColorSpace::COLOR_SPACE_CMYK:
30
                $palette = new Palette\CMYK();
31
                break;
32
            case ColorSpace::COLOR_SPACE_GRAYSCALE:
33
                $palette = new Palette\Grayscale();
34
                break;
35
        }
36
37
        $colorProfile = $this->getColorProfile();
38
        if (null !== $colorProfile) {
39
            $palette->setColorProfile($colorProfile);
40
        }
41
42
        return $palette;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function withPalette(PaletteInterface $palette)
49
    {
50
        $image = clone $this;
51
52
        if ($image->getColorSpace() === $palette->getColorSpace()) {
53
            return $image;
54
        }
55
56
        if (null === $image->getColorProfile()) {
57
            $image = $image->withColorProfile($image->getPalette()->getColorProfile());
0 ignored issues
show
Bug introduced by
It seems like $image->getPalette()->getColorProfile() can be null; however, withColorProfile() 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...
58
        }
59
60
        $image = $image
61
            ->withColorProfile($palette->getColorProfile())
0 ignored issues
show
Bug introduced by
It seems like $palette->getColorProfile() can be null; however, withColorProfile() 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...
62
            ->withColorSpace($palette->getColorSpace());
63
64
        return $image;
65
    }
66
}
67