Passed
Pull Request — master (#8)
by Shinji
08:28
created

KeyboardCancelLoop::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\Loop\LoopProcess;
15
16
use PhpProfiler\Lib\Loop\LoopProcessInterface;
17
18
final class KeyboardCancelLoop implements LoopProcessInterface
19
{
20
    private LoopProcessInterface $chain;
21
    private string $cancel_key;
22
    /** @var resource */
23
    private $keyboard_input;
24
25
    public function __construct(string $cancel_key, LoopProcessInterface $chain)
26
    {
27
        $this->chain = $chain;
28
        exec('stty -icanon -echo');
29
        $this->keyboard_input = fopen('php://stdin', 'r');
1 ignored issue
show
Documentation Bug introduced by
It seems like fopen('php://stdin', 'r') can also be of type false. However, the property $keyboard_input 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...
30
        stream_set_blocking($this->keyboard_input, false);
1 ignored issue
show
Bug introduced by
It seems like $this->keyboard_input can also be of type false; however, parameter $stream of stream_set_blocking() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

30
        stream_set_blocking(/** @scrutinizer ignore-type */ $this->keyboard_input, false);
Loading history...
31
        $this->cancel_key = $cancel_key;
32
    }
33
34
    public function invoke(): bool
35
    {
36
        $key = fread($this->keyboard_input, 1);
37
        if ($key === $this->cancel_key) {
38
            return false;
39
        }
40
        return $this->chain->invoke();
41
    }
42
}
43