Passed
Pull Request — master (#50)
by Jitendra
02:23 queued 48s
created

Reader   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 6
Metric Value
eloc 33
c 6
b 0
f 6
dl 0
loc 103
rs 10
wmc 17

5 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
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
    public function readPiped(callable $fn = null): string
57
    {
58
        $stdin = '';
59
        $read  = [$this->stream];
60
        $write = [];
61
        $exept = [];
62
63
        if (\stream_select($read, $write, $exept, 0) === 1) {
64
            while ($line = \fgets($this->stream)) {
65
                $stdin .= $line;
66
            }
67
        }
68
69
        if ('' === $stdin) {
70
            return $fn ? $fn($this) : '';
71
        }
72
73
        return $stdin;
74
    }
75
76
    /**
77
     * Read a line from configured stream (or terminal) but don't echo it back.
78
     *
79
     * @param callable|null $fn The validator/sanitizer callback.
80
     *
81
     * @return mixed
82
     */
83
    public function readHidden($default = null, callable $fn = null)
84
    {
85
        // @codeCoverageIgnoreStart
86
        if ('\\' === \DIRECTORY_SEPARATOR) {
87
            return $this->readHiddenWinOS($default, $fn);
88
        }
89
        // @codeCoverageIgnoreEnd
90
91
        \shell_exec('stty -echo');
92
        $in = $this->read($default, $fn);
93
        \shell_exec('stty echo');
94
95
        echo \PHP_EOL;
96
97
        return $in;
98
    }
99
100
    /**
101
     * Read a line from configured stream (or terminal) but don't echo it back.
102
     *
103
     * @codeCoverageIgnore
104
     *
105
     * @param callable|null $fn The validator/sanitizer callback.
106
     *
107
     * @return mixed
108
     */
109
    private function readHiddenWinOS($default = null, callable $fn = null)
110
    {
111
        $cmd = 'powershell -Command ' . \implode('; ', \array_filter([
112
            '$pword = Read-Host -AsSecureString',
113
            '$pword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword)',
114
            '$pword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($pword)',
115
            'echo $pword',
116
        ]));
117
118
        $in = \rtrim(\shell_exec($cmd), "\r\n");
119
120
        if ('' === $in && null !== $default) {
121
            return $default;
122
        }
123
124
        return $fn ? $fn($in) : $in;
125
    }
126
}
127