Terminal::setDimensions()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 12.1743

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
nc 6
nop 0
dl 0
loc 29
ccs 8
cts 18
cp 0.4444
crap 12.1743
rs 9.0444
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Console;
6
7
use App\Component\Console\Dimensions;
8
use App\Component\Console\Process;
9
10
/**
11
 * Console terminal is a terminal for console.
12
 *
13
 * @todo A lot of stuff
14
 */
15
class Terminal
16
{
17
18
    const TTY_DIM_COMMAND_0 = "bash -c 'stty size'";
19
    const TTY_DIM_COMMAND_1 = "bash -c 'stty -a | grep rows'";
20
    const _S = ' ';
21
22
    /**
23
     * terminal dimensions
24
     *
25
     * @var Dimensions
26
     */
27
    protected $dimensions;
28
29
    /**
30
     * terminal processor
31
     *
32
     * @var Process
33
     */
34
    protected $processor;
35
36
    /**
37
     * instanciate
38
     */
39 4
    public function __construct()
40
    {
41 4
        $this->processor = new Process();
42 4
        $this->setDimensions();
43
    }
44
45
    /**
46
     * setDimensions
47
     *
48
     * @return void
49
     */
50 1
    protected function setDimensions(): Terminal
51
    {
52 1
        $this->dimensions = new Dimensions();
53 1
        $this->dimensions->set(0, 0);
54 1
        if (!$this->isWindows()) {
55 1
            $matcher = [0, 0, 0];
56 1
            $ttyDims = $this->processor->setCommand(self::TTY_DIM_COMMAND_0)->run();
57 1
            if ($this->processor->isError()) {
58 1
                return $this;
59
            }
60
            //$ttyDims = Process::readFromProcess(self::TTY_DIM_COMMAND_0);
61
            //echo $ttyDims;die;*/
62
            //var_dump($ttyDims);
63
            if (!empty($ttyDims)) {
64
                $matcher = explode(self::_S, self::_S . $ttyDims);
65
            } else {
66
                $ttyDims = Process::readFromProcess(self::TTY_DIM_COMMAND_1);
0 ignored issues
show
Bug introduced by
The method readFromProcess() does not exist on App\Component\Console\Process. ( Ignorable by Annotation )

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

66
                /** @scrutinizer ignore-call */ 
67
                $ttyDims = Process::readFromProcess(self::TTY_DIM_COMMAND_1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The assignment to $ttyDims is dead and can be removed.
Loading history...
67
                $ttyDims  = 'speed 38400 baud; rows 96; columns 126; line = 0;';
68
                //var_dump($ttyDims);
69
                if (preg_match('/(\d+)+;.\w{7}.(\d+)+;/', $ttyDims, $matches)) {
70
                    $matcher = $matches;
71
                } elseif (preg_match('/;(\d+).\w{4};.(\d+)/i', $ttyDims, $matches)) {
72
                    $matcher = $matches;
73
                }
74
            }
75
            //var_dump($matcher, $ttyDims);
76
            $this->dimensions->set((int) $matcher[2], (int) $matcher[1]);
77
        }
78
        return $this;
79
    }
80
81
    /**
82
     * returns terminal dimensions
83
     *
84
     * @return Dimensions
85
     */
86 1
    protected function getDimensions(): Dimensions
87
    {
88 1
        return $this->dimensions;
89
    }
90
91
    /**
92
     * return true if microsoft platform
93
     *
94
     * @return boolean
95
     */
96 1
    protected function isWindows(): bool
97
    {
98 1
        return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
99
    }
100
}
101