Linux   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 82
ccs 6
cts 13
cp 0.4615
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A width() 0 4 1
A height() 0 4 1
A tput() 0 4 1
A canAccessBash() 0 4 1
A hiddenResponsePrompt() 0 6 1
A getDimension() 0 4 2
A systemHasAnsiSupport() 0 4 2
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