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

ColorProfile::fromFilename()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 2
nop 1
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\Exception\RuntimeException;
11
use GuzzleHttp\Stream\StreamInterface;
12
use GuzzleHttp\Stream\Utils as StreamUtils;
13
14
/**
15
 * Color profile class.
16
 *
17
 * @package GravityMedia\Magickly\Image
18
 */
19
class ColorProfile
20
{
21
    /**
22
     * @var StreamInterface
23
     */
24
    private $stream;
25
26
    /**
27
     * Create color profile object.
28
     *
29
     * @param StreamInterface $stream
30
     */
31
    public function __construct(StreamInterface $stream)
32
    {
33
        $this->stream = $stream;
34
    }
35
36
    /**
37
     * Get stream.
38
     *
39
     * @return StreamInterface
40
     */
41
    public function getStream()
42
    {
43
        return $this->stream;
44
    }
45
46
    /**
47
     * Get data.
48
     *
49
     * @return string
50
     */
51
    public function getData()
52
    {
53
        $offset = $this->stream->tell();
54
        $this->stream->seek(0);
55
56
        $data = $this->stream->getContents();
57
        $this->stream->seek($offset);
0 ignored issues
show
Bug introduced by
It seems like $offset defined by $this->stream->tell() on line 53 can also be of type boolean; however, GuzzleHttp\Stream\StreamInterface::seek() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
59
        return $data;
60
    }
61
62
    /**
63
     * Create color profile object from filename.
64
     *
65
     * @param string $filename
66
     *
67
     * @return $this
68
     */
69
    public static function fromFilename($filename)
70
    {
71
        if (!file_exists($filename) || !is_file($filename) || !is_readable($filename)) {
72
            throw new RuntimeException(sprintf('Filename %s is an invalid profile file or is not readable', $filename));
73
        }
74
75
        return new static(StreamUtils::open($filename, 'r'));
0 ignored issues
show
Documentation introduced by
\GuzzleHttp\Stream\Utils::open($filename, 'r') is of type resource, but the function expects a object<GuzzleHttp\Stream\StreamInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
    }
77
}
78