Completed
Push — tput-fix ( d02275 )
by Craig
01:29
created

Linux::hiddenResponsePrompt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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
     * @return integer|null
13
     */
14
    public function width()
15
    {
16
        return $this->getDimension($this->tput("cols"));
0 ignored issues
show
Bug introduced by
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
     * @return integer|null
23
     */
24
    public function height()
25
    {
26
        return $this->getDimension($this->tput("lines"));
0 ignored issues
show
Bug introduced by
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
        # If there's no terminal available then tput throws up, so don't try it
39
        $term = (string) getenv("TERM");
40
        if ($term === "") {
41
            return null;
42
        }
43
44
        return $this->exec("tput {$type}");
45
    }
46
47
    /**
48
     * Determine if system has access to bash commands
49
     *
50
     * @return bool
51
     */
52
    public function canAccessBash()
53
    {
54
        return (rtrim($this->exec("/usr/bin/env bash -c 'echo OK'")) === 'OK');
55
    }
56
57
    /**
58
     * Display a hidden response prompt and return the response
59
     *
60
     * @param string $prompt
61
     *
62
     * @return string
63
     */
64
    public function hiddenResponsePrompt($prompt)
65
    {
66
        $bash_command = 'read -s -p "' . $prompt . '" response && echo $response';
67
68
        return rtrim($this->exec("/usr/bin/env bash -c '{$bash_command}'"));
69
    }
70
71
    /**
72
     * Determine if dimension is numeric and return it
73
     *
74
     * @param integer|string|null $dimension
75
     *
76
     * @return integer|null
77
     */
78
    protected function getDimension($dimension)
79
    {
80
        return (is_numeric($dimension)) ? $dimension : null;
81
    }
82
83
    /**
84
     * Check if the stream supports ansi escape characters.
85
     *
86
     * Based on https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/StreamOutput.php
87
     *
88
     * @return bool
89
     */
90
    protected function systemHasAnsiSupport()
91
    {
92
        return (function_exists('posix_isatty') && @posix_isatty(STDOUT));
93
    }
94
}
95