Completed
Push — master ( 1316ca...59eabb )
by Daniel
11:50
created

AbstractImage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPalette() 0 21 4
A setPalette() 0 15 3
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 setPalette(PaletteInterface $palette)
49
    {
50
        if ($this->getColorSpace() === $palette->getColorSpace()) {
51
            return $this;
52
        }
53
54
        if (null === $this->getColorProfile()) {
55
            $this->setColorProfile($this->getPalette()->getColorProfile());
56
        }
57
58
        $this->setColorProfile($palette->getColorProfile());
0 ignored issues
show
Bug introduced by
It seems like $palette->getColorProfile() can be null; however, setColorProfile() 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...
59
        $this->setColorSpace($palette->getColorSpace());
60
61
        return $this;
62
    }
63
}
64