Completed
Push — master ( a44ccc...2e789b )
by Pierre
14:15
created

Terminal::isWindows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Component\Console;
4
5
use App\Component\Console\Dimensions;
6
use App\Component\Console\Process;
7
8
/**
9
 * Console terminal is a terminal for console.
10
 *
11
 * @todo A lot of stuff
12
 */
13
class Terminal
14
{
15
16
    const TTY_DIM_COMMAND_0 = 'stty size';
17
    const TTY_DIM_COMMAND_1 = 'stty -a | grep rows';
18
    const _S = ' ';
19
20
    /**
21
     * terminal dimensions
22
     *
23
     * @var Dimensions
24
     */
25
    protected $dimensions;
26
27
    /**
28
     * instanciate
29
     */
30 3
    public function __construct()
31
    {
32 3
        $this->setDimensions();
33
    }
34
35
    /**
36
     * setDimensions
37
     *
38
     * @return void
39
     */
40 1
    protected function setDimensions(): Terminal
41
    {
42 1
        $this->dimensions = new Dimensions();
43 1
        if (!$this->isWindows()) {
44 1
            $matcher = [0,0,0];
45
            //$ttyDims = Process::readFromProcess(self::TTY_DIM_COMMAND_0);
46
            //echo $ttyDims;die;*/
47
            //var_dump($ttyDims);
48 1
            $ttyDims  = [];
49 1
            if (!empty($ttyDims)) {
50
                $matcher = explode(self::_S, self::_S . $ttyDims);
0 ignored issues
show
Bug introduced by
Are you sure $ttyDims of type array can be used in concatenation? ( Ignorable by Annotation )

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

50
                $matcher = explode(self::_S, self::_S . /** @scrutinizer ignore-type */ $ttyDims);
Loading history...
51
            } else {
52 1
                $ttyDims = Process::readFromProcess(self::TTY_DIM_COMMAND_1);
53
                //var_dump($ttyDims);
54 1
                if (preg_match('/(\d+)+;.\w{7}.(\d+)+;/', $ttyDims, $matches)) {
55 1
                    $matcher = $matches;
56
                } elseif (preg_match('/;(\d+).\w{4};.(\d+)/i', $ttyDims, $matches)) {
57
                    $matcher = $matches;
58
                }
59
            }
60
            //var_dump($matcher, $ttyDims);
61 1
            $this->dimensions->set((int) $matcher[2], (int) $matcher[1]);
62
        }
63 1
        return $this;
64
    }
65
66
    /**
67
     * return true if microsoft platform
68
     *
69
     * @return boolean
70
     */
71 1
    protected function isWindows(): bool
72
    {
73 1
        return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
74
    }
75
}
76