StreamOutputStream   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.07%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 15
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 78
ccs 20
cts 27
cp 0.7407
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isClosed() 0 4 1
A __construct() 0 6 1
A write() 0 10 3
A flush() 0 10 3
A close() 0 7 2
B supportsAnsi() 0 8 5
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\OutputStream;
13
14
use Webmozart\Assert\Assert;
15
use Webmozart\Console\Api\IO\IOException;
16
use Webmozart\Console\Api\IO\OutputStream;
17
18
/**
19
 * An output stream that writes to a PHP stream.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
class StreamOutputStream implements OutputStream
26
{
27
    /**
28
     * @var resource
29
     */
30
    private $stream;
31
32
    /**
33
     * Creates the stream.
34
     *
35
     * @param resource $stream A stream resource.
36
     */
37 144
    public function __construct($stream)
38
    {
39 144
        Assert::resource($stream, 'stream');
40
41 144
        $this->stream = $stream;
42 144
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function write($string)
48
    {
49 2
        if (null === $this->stream) {
50 1
            throw new IOException('Cannot read from a closed input.');
51
        }
52
53 1
        if (false === fwrite($this->stream, $string)) {
54
            throw new IOException('Could not write stream.');
55
        }
56 1
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function flush()
62
    {
63 1
        if (null === $this->stream) {
64 1
            throw new IOException('Cannot read from a closed input.');
65
        }
66
67
        if (false === fflush($this->stream)) {
68
            throw new IOException('Could not flush stream.');
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 138
    public function supportsAnsi()
76
    {
77 138
        if (DIRECTORY_SEPARATOR === '\\') {
78
            return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM');
79
        }
80
81 138
        return function_exists('posix_isatty') && @posix_isatty($this->stream);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 3
    public function close()
88
    {
89 3
        if ($this->stream) {
90 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...
91 3
            $this->stream = null;
92
        }
93 3
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function isClosed()
99
    {
100
        return null === $this->stream;
101
    }
102
}
103