StandardInput   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createIterator() 0 16 4
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Cli.
5
 *
6
 * (c) Sebastian Feldmann <[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
namespace SebastianFeldmann\Cli\Reader;
13
14
use Exception;
15
16
/**
17
 * StandardInput
18
 *
19
 * @package SebastianFeldmann\Cli
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/cli
22
 * @since   Class available since Release 3.3.0
23
 */
24
class StandardInput extends Abstraction
25
{
26
    /**
27
     * Standard Input stream handle
28
     *
29
     * @var resource
30
     */
31
    private $handle;
32
33
    /**
34
     * StandardInput constructor.
35
     *
36
     * @param resource $stdInHandle
37
     */
38
    public function __construct($stdInHandle)
39
    {
40
        $this->handle = $stdInHandle;
41
    }
42
43
    /**
44
     * Create the generator
45
     *
46
     * @return iterable
47
     * @throws \Exception
48
     */
49
    protected function createIterator(): iterable
50
    {
51
        $read   = [$this->handle];
52
        $write  = [];
53
        $except = [];
54
        $result = stream_select($read, $write, $except, 0);
55
56
        if ($result === false) {
57
            throw new Exception('stream_select failed');
58
        }
59
        if ($result !== 0) {
60
            while (!\feof($this->handle)) {
61
                yield \fgets($this->handle);
62
            }
63
        }
64
    }
65
}
66