Reader   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 7
Metric Value
eloc 35
c 7
b 0
f 7
dl 0
loc 128
rs 10
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 9 4
A __construct() 0 3 2
A readPiped() 0 18 5
A readHiddenWinOS() 0 16 4
A readHidden() 0 15 2
A readAll() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of the PHP-CLI package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Cli\Input;
13
14
/**
15
 * Cli Reader.
16
 *
17
 * @author  Jitendra Adhikari <[email protected]>
18
 * @license MIT
19
 *
20
 * @link    https://github.com/adhocore/cli
21
 */
22
class Reader
23
{
24
    /** @var resource Input file handle */
25
    protected $stream;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string|null $path Read path. Defaults to STDIN.
31
     */
32
    public function __construct(string $path = null)
33
    {
34
        $this->stream = $path ? \fopen($path, 'r') : \STDIN;
0 ignored issues
show
Documentation Bug introduced by
It seems like $path ? fopen($path, 'r') : STDIN can also be of type false. However, the property $stream 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...
35
    }
36
37
    /**
38
     * Read a line from configured stream (or terminal).
39
     *
40
     * @param mixed         $default The default value.
41
     * @param callable|null $fn      The validator/sanitizer callback.
42
     *
43
     * @return mixed
44
     */
45
    public function read($default = null, callable $fn = null)
46
    {
47
        $in = \rtrim(\fgets($this->stream), "\r\n");
48
49
        if ('' === $in && null !== $default) {
50
            return $default;
51
        }
52
53
        return $fn ? $fn($in) : $in;
54
    }
55
56
    /**
57
     * Same like read but it reads all the lines.
58
     *
59
     * @codeCoverageIgnore
60
     *
61
     * @param callable|null $fn The validator/sanitizer callback.
62
     *
63
     * @return string
64
     */
65
    public function readAll(callable $fn = null): string
66
    {
67
        $in = \stream_get_contents($this->stream);
68
69
        return $fn ? $fn($in) : $in;
70
    }
71
72
    /**
73
     * Read content piped to the stream without waiting.
74
     *
75
     * @codeCoverageIgnore
76
     *
77
     * @param callable|null $fn The callback to execute if stream is empty.
78
     *
79
     * @return string
80
     */
81
    public function readPiped(callable $fn = null): string
82
    {
83
        $stdin = '';
84
        $read  = [$this->stream];
85
        $write = [];
86
        $exept = [];
87
88
        if (\stream_select($read, $write, $exept, 0) === 1) {
89
            while ($line = \fgets($this->stream)) {
90
                $stdin .= $line;
91
            }
92
        }
93
94
        if ('' === $stdin) {
95
            return $fn ? $fn($this) : '';
96
        }
97
98
        return $stdin;
99
    }
100
101
    /**
102
     * Read a line from configured stream (or terminal) but don't echo it back.
103
     *
104
     * @param callable|null $fn The validator/sanitizer callback.
105
     *
106
     * @return mixed
107
     */
108
    public function readHidden($default = null, callable $fn = null)
109
    {
110
        // @codeCoverageIgnoreStart
111
        if ('\\' === \DIRECTORY_SEPARATOR) {
112
            return $this->readHiddenWinOS($default, $fn);
113
        }
114
        // @codeCoverageIgnoreEnd
115
116
        \shell_exec('stty -echo');
117
        $in = $this->read($default, $fn);
118
        \shell_exec('stty echo');
119
120
        echo \PHP_EOL;
121
122
        return $in;
123
    }
124
125
    /**
126
     * Read a line from configured stream (or terminal) but don't echo it back.
127
     *
128
     * @codeCoverageIgnore
129
     *
130
     * @param callable|null $fn The validator/sanitizer callback.
131
     *
132
     * @return mixed
133
     */
134
    protected function readHiddenWinOS($default = null, callable $fn = null)
135
    {
136
        $cmd = 'powershell -Command ' . \implode('; ', \array_filter([
137
            '$pword = Read-Host -AsSecureString',
138
            '$pword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword)',
139
            '$pword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($pword)',
140
            'echo $pword',
141
        ]));
142
143
        $in = \rtrim(\shell_exec($cmd), "\r\n");
144
145
        if ('' === $in && null !== $default) {
146
            return $default;
147
        }
148
149
        return $fn ? $fn($in) : $in;
150
    }
151
}
152