Completed
Push — master ( 0db193...3f588d )
by Jitendra
16s queued 12s
created

Reader::readHiddenWinOS()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 16
rs 9.9666
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
     * Read a line from configured stream (or terminal) but don't echo it back.
58
     *
59
     * @param callable|null $fn The validator/sanitizer callback.
60
     *
61
     * @return mixed
62
     */
63
    public function readHidden($default = null, callable $fn = null)
64
    {
65
        // @codeCoverageIgnoreStart
66
        if ('\\' === \DIRECTORY_SEPARATOR) {
67
            return $this->readHiddenWinOS($default, $fn);
68
        }
69
        // @codeCoverageIgnoreEnd
70
71
        \shell_exec('stty -echo');
72
        $in = $this->read($default, $fn);
73
        \shell_exec('stty echo');
74
75
        echo \PHP_EOL;
76
77
        return $in;
78
    }
79
80
    /**
81
     * Read a line from configured stream (or terminal) but don't echo it back.
82
     *
83
     * @codeCoverageIgnore
84
     *
85
     * @param callable|null $fn The validator/sanitizer callback.
86
     *
87
     * @return mixed
88
     */
89
    private function readHiddenWinOS($default = null, callable $fn = null)
90
    {
91
        $cmd = 'powershell -Command ' . \implode('; ', \array_filter([
92
            '$pword = Read-Host -AsSecureString',
93
            '$pword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword)',
94
            '$pword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($pword)',
95
            'echo $pword',
96
        ]));
97
98
        $in = \rtrim(\shell_exec($cmd), "\r\n");
99
100
        if ('' === $in && null !== $default) {
101
            return $default;
102
        }
103
104
        return $fn ? $fn($in) : $in;
105
    }
106
}
107