Linux::height()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace League\CLImate\Util\System;
4
5
use function getenv;
6
7
class Linux extends System
8
{
9
    /**
10
     * Get the width of the terminal
11
     *
12 8
     * @return integer|null
13
     */
14 8
    public function width()
15
    {
16
        return $this->getDimension($this->tput("cols"));
0 ignored issues
show
Bug introduced by Craig Duncan
It seems like $this->tput('cols') targeting League\CLImate\Util\System\Linux::tput() can also be of type array; however, League\CLImate\Util\System\Linux::getDimension() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
17
    }
18
19
    /**
20
     * Get the height of the terminal
21
     *
22 8
     * @return integer|null
23
     */
24 8
    public function height()
25
    {
26
        return $this->getDimension($this->tput("lines"));
0 ignored issues
show
Bug introduced by Craig Duncan
It seems like $this->tput('lines') targeting League\CLImate\Util\System\Linux::tput() can also be of type array; however, League\CLImate\Util\System\Linux::getDimension() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27
    }
28
29
    /**
30
     * Get a value from the tput command.
31
     *
32
     * @param string $type
33
     *
34
     * @return array|null|string
35
     */
36
    private function tput($type)
37
    {
38
        return $this->exec("tput {$type} 2>/dev/null");
39
    }
40
41
    /**
42
     * Determine if system has access to bash commands
43
     *
44
     * @return bool
45
     */
46
    public function canAccessBash()
47
    {
48
        return (rtrim($this->exec("/usr/bin/env bash -c 'echo OK'")) === 'OK');
49
    }
50
51
    /**
52
     * Display a hidden response prompt and return the response
53
     *
54
     * @param string $prompt
55
     *
56
     * @return string
57
     */
58 16
    public function hiddenResponsePrompt($prompt)
59
    {
60 16
        $bash_command = 'read -s -p "' . $prompt . '" response && echo $response';
61
62
        return rtrim($this->exec("/usr/bin/env bash -c '{$bash_command}'"));
63
    }
64
65
    /**
66
     * Determine if dimension is numeric and return it
67
     *
68
     * @param integer|string|null $dimension
69
     *
70
     * @return integer|null
71
     */
72
    protected function getDimension($dimension)
73
    {
74
        return (is_numeric($dimension)) ? $dimension : null;
75
    }
76
77
    /**
78
     * Check if the stream supports ansi escape characters.
79
     *
80
     * Based on https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/StreamOutput.php
81
     *
82
     * @return bool
83
     */
84
    protected function systemHasAnsiSupport()
85
    {
86
        return (function_exists('posix_isatty') && @posix_isatty(STDOUT));
87
    }
88
}
89