Completed
Push — master ( 10cab8...549589 )
by Pierre
35:30 queued 32:24
created

Input   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 149
ccs 37
cts 38
cp 0.9737
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 16 2
A openStream() 0 10 2
A getStreamName() 0 3 1
A closeStream() 0 6 2
A getStreamHandler() 0 4 1
A setMaxLength() 0 4 1
A __construct() 0 9 2
A getMaxLength() 0 3 1
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
    const DEBUGER = 'phpdbg';
16
17
    /**
18
     * streamName
19
     *
20
     * @var string
21
     */
22
    protected $streamName;
23
24
    /**
25
     * streamMode
26
     *
27
     * @var string
28
     */
29
    protected $streamMode;
30
31
    /**
32
     * $streamHandler
33
     *
34
     * @var resource
35
     */
36
    protected $streamHandler;
37
38
    /**
39
     * $maxLength
40
     *
41
     * @var int
42
     */
43
    protected $maxLength;
44
45
    /**
46
     * instanciate
47
     *
48
     * @param string $streamName
49
     */
50 6
    public function __construct(
51
        string $streamName = self::STREAM_STDIN,
52
        string $streamMode = self::STREAM_MODE_WRITE_APPEND
53
    ) {
54 6
        $this->streamName = (php_sapi_name() == self::DEBUGER)
55 6
            ? self::STREAM_MEMORY
56
            : $streamName;
57 6
        $this->streamMode = $streamMode;
58 6
        $this->setMaxLength(1);
59
    }
60
61
    /**
62
     * return the input value
63
     *
64
     * @return string
65
     */
66 1
    public function value(string $forcedValue = ''): string
67
    {
68 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...
69
        //readline_callback_handler_install('', function () { });
70 1
        if (!empty($forcedValue)) {
71 1
            $this->setMaxLength(strlen($forcedValue));
72 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

72
            rewind(/** @scrutinizer ignore-type */ $handle);
Loading history...
73 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

73
            fwrite(/** @scrutinizer ignore-type */ $handle, $forcedValue);
Loading history...
74
        }
75 1
        $value = stream_get_contents(
76 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

76
            /** @scrutinizer ignore-type */ $handle,
Loading history...
77 1
            $this->getMaxLength(),
78
            0
79
        );
80 1
        $this->closeStream();
81 1
        return $value;
82
    }
83
84
    /**
85
     * set stream max length
86
     *
87
     * @return Input
88
     */
89 6
    public function setMaxLength(int $len): Input
90
    {
91 6
        $this->maxLength = $len;
92 6
        return $this;
93
    }
94
95
    /**
96
     * returns stream handler
97
     *
98
     * @return int
99
     */
100 1
    protected function getMaxLength(): int
101
    {
102 1
        return $this->maxLength;
103
    }
104
105
    /**
106
     * returns stream handler
107
     *
108
     * @return void
109
     */
110 1
    protected function getStreamHandler()
111
    {
112 1
        $this->openStream();
113 1
        return $this->streamHandler;
114
    }
115
116
    /**
117
     * returns stream name
118
     *
119
     * @return string
120
     */
121 1
    protected function getStreamName(): string
122
    {
123 1
        return $this->streamName;
124
    }
125
126
    /**
127
     * open resource
128
     *
129
     * @return Input
130
     */
131 1
    protected function openStream(): Input
132
    {
133 1
        if (!is_resource($this->streamHandler)) {
134 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...
135 1
                $this->getStreamName(),
136 1
                $this->streamMode,
137 1
                false
138
            );
139
        }
140 1
        return $this;
141
    }
142
143
    /**
144
     * close resource
145
     *
146
     * @return Input
147
     */
148 1
    protected function closeStream(): Input
149
    {
150 1
        if (is_resource($this->streamHandler)) {
151 1
            fclose($this->streamHandler);
152
        }
153 1
        return $this;
154
    }
155
}
156