Completed
Push — master ( bd5ff6...90c2c7 )
by Bernhard
07:04
created

StreamInputStream::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\IO\InputStream;
13
14
use Webmozart\Assert\Assert;
15
use Webmozart\Console\Api\IO\InputStream;
16
use Webmozart\Console\Api\IO\IOException;
17
18
/**
19
 * An input stream that reads from a PHP stream.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
class StreamInputStream implements InputStream
26
{
27
    /**
28
     * @var resource
29
     */
30
    private $stream;
31
32
    /**
33
     * Creates the input.
34
     *
35
     * @param resource $stream A stream resource.
36
     */
37 250
    public function __construct($stream)
38
    {
39 250
        Assert::resource($stream, 'stream');
40
41 250
        $this->stream = $stream;
42
43
        // Not all streams are seekable
44 250
        @rewind($this->stream);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45 250
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 7
    public function read($length)
51
    {
52 7
        if (null === $this->stream) {
53 1
            throw new IOException('Cannot read from a closed input.');
54
        }
55
56 6
        if (feof($this->stream)) {
57
            return null;
58
        }
59
60 6
        $data = fread($this->stream, $length);
61
62 6
        if (false === $data && !feof($this->stream)) {
63
            throw new IOException('Could not read stream.');
64
        }
65
66 6
        return $data ?: null;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 19
    public function readLine($length = null)
73
    {
74 19
        if (null === $this->stream) {
75 1
            throw new IOException('Cannot read from a closed input.');
76
        }
77
78 18
        if (feof($this->stream)) {
79 3
            return null;
80
        }
81
82 18
        if (null !== $length) {
83 3
            $data = fgets($this->stream, $length);
84
        } else {
85 18
            $data = fgets($this->stream);
86
        }
87
88 18
        if (false === $data && !feof($this->stream)) {
89
            throw new IOException('Could not read stream.');
90
        }
91
92 18
        return $data ?: null;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 3
    public function close()
99
    {
100 3
        if ($this->stream) {
101 3
            @fclose($this->stream);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102 3
            $this->stream = null;
103
        }
104 3
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function isClosed()
110
    {
111
        return null === $this->stream;
112
    }
113
}
114