Completed
Push — master ( 2d4622...b13427 )
by Pierre
04:00
created

Input::closeStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Component\Console;
4
5
class Input
6
{
7
8
    const STREAM_STDIN = 'php://stdin';
9
    const STREAM_MEMORY = 'php://memory';
10
    const STREAM_TEMP = 'php://temp';
11
    const STREAM_MODE_READ = 'r';
12
    const STREAM_MODE_APPEND = 'r+';
13
    const STREAM_MODE_WRITE = 'w';
14
    const STREAM_MODE_WRITE_APPEND = 'w+';
15
16
    /**
17
     * streamName
18
     *
19
     * @var string
20
     */
21
    protected $streamName;
22
23
    /**
24
     * streamMode
25
     *
26
     * @var string
27
     */
28
    protected $streamMode;
29
30
    /**
31
     * $streamHandler
32
     *
33
     * @var resource
34
     */
35
    protected $streamHandler;
36
37
    /**
38
     * $maxLength
39
     *
40
     * @var int
41
     */
42
    protected $maxLength;
43
44
    /**
45
     * instanciate
46
     *
47
     * @param string $streamName
48
     */
49 6
    public function __construct(
50
        string $streamName = self::STREAM_STDIN,
51
        string $streamMode = self::STREAM_MODE_WRITE_APPEND
52
    ) {
53 6
        $this->streamName = $streamName;
54 6
        $this->streamMode = $streamMode;
55 6
        $this->setMaxLength(1);
56
    }
57
58
    /**
59
     * return the input value
60
     *
61
     * @return string
62
     */
63 1
    public function value(string $forcedValue = ''): string
64
    {
65 1
        $handle = $this->getStreamHandler();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $handle is correct as $this->getStreamHandler() targeting App\Component\Console\Input::getStreamHandler() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
        //readline_callback_handler_install('', function () { });
67 1
        if (!empty($forcedValue)) {
68 1
            $this->setMaxLength(strlen($forcedValue));
69 1
            rewind($handle);
0 ignored issues
show
Bug introduced by
$handle of type void is incompatible with the type resource expected by parameter $handle of rewind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            rewind(/** @scrutinizer ignore-type */ $handle);
Loading history...
70 1
            fwrite($handle, $forcedValue);
0 ignored issues
show
Bug introduced by
$handle of type void is incompatible with the type resource expected by parameter $handle of fwrite(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            fwrite(/** @scrutinizer ignore-type */ $handle, $forcedValue);
Loading history...
71
        }
72 1
        $value = stream_get_contents(
73 1
            $handle, 
0 ignored issues
show
Bug introduced by
$handle of type void is incompatible with the type resource expected by parameter $handle of stream_get_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            /** @scrutinizer ignore-type */ $handle, 
Loading history...
74 1
            $this->getMaxLength(), 
75
            0
76
        );
77 1
        $this->closeStream();
78 1
        return $value;
79
    }
80
81
    /**
82
     * set stream max length
83
     *
84
     * @return Input
85
     */
86 6
    public function setMaxLength(int $len): Input
87
    {
88 6
        $this->maxLength = $len;
89 6
        return $this;
90
    }    
91
92
    /**
93
     * returns stream handler
94
     *
95
     * @return int
96
     */
97 1
    protected function getMaxLength(): int
98
    {
99 1
        return $this->maxLength;
100
    }
101
102
    /**
103
     * returns stream handler
104
     *
105
     * @return void
106
     */
107 1
    protected function getStreamHandler()
108
    {
109 1
        $this->openStream();
110 1
        return $this->streamHandler;
111
    }
112
113
    /**
114
     * returns stream name
115
     *
116
     * @return string
117
     */
118 1
    protected function getStreamName(): string
119
    {
120 1
        return $this->streamName;
121
    }
122
123
    /**
124
     * open resource
125
     *
126
     * @return Input
127
     */
128 1
    protected function openStream(): Input
129
    {
130 1
        if (!is_resource($this->streamHandler)) {
131 1
            $this->streamHandler = fopen(
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($this->getStreamNa...his->streamMode, false) can also be of type false. However, the property $streamHandler is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
132 1
                $this->getStreamName(),
133 1
                $this->streamMode,
134 1
                false
135
            );
136
        }
137 1
        return $this;
138
    }
139
140
    /**
141
     * close resource
142
     *
143
     * @return Input
144
     */
145 1
    protected function closeStream(): Input
146
    {
147 1
        if (is_resource($this->streamHandler)) {
148 1
            fclose($this->streamHandler);
149
        }
150 1
        return $this;
151
    }
152
}
153