Total Complexity | 44 |
Total Lines | 261 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractTerminal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractTerminal, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
17 | abstract class AbstractTerminal |
||
18 | { |
||
19 | protected const DEFAULT_WIDTH = 80; |
||
20 | protected const DEFAULT_HEIGHT = 50; |
||
21 | protected const ENV_TERM = 'TERM'; |
||
22 | protected const ENV_ANSICON = 'ANSICON'; |
||
23 | protected const ENV_CON_EMU_ANSI = 'ConEmuANSI'; |
||
24 | protected const ENV_DOCKER_TERM = 'DOCKER_TERM'; |
||
25 | protected const COLOR_NEEDLE = '256color'; |
||
26 | protected const ENV_COLUMNS = 'COLUMNS'; |
||
27 | protected const ENV_LINES = 'LINES'; |
||
28 | protected const ENV_TERM_PROGRAM = 'TERM_PROGRAM'; |
||
29 | |||
30 | /** @var null|int */ |
||
31 | protected static $width; |
||
32 | |||
33 | /** @var null|int */ |
||
34 | protected static $height; |
||
35 | |||
36 | /** @var null|bool */ |
||
37 | protected static $supports256Color; |
||
38 | |||
39 | /** @var null|bool */ |
||
40 | protected static $supportsColor; |
||
41 | |||
42 | /** |
||
43 | * Gets the terminal width. |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | protected function getWidth(): int |
||
48 | { |
||
49 | $width = getenv(static::ENV_COLUMNS); |
||
50 | if (false !== $width) { |
||
51 | return (int)trim($width); |
||
52 | } |
||
53 | // @codeCoverageIgnoreStart |
||
54 | if (null === static::$width) { |
||
55 | static::initDimensions(); |
||
56 | } |
||
57 | return static::$width ?: static::DEFAULT_WIDTH; |
||
58 | // @codeCoverageIgnoreEnd |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @codeCoverageIgnore |
||
63 | */ |
||
64 | protected static function initDimensions(): void |
||
65 | { |
||
66 | if (static::onWindows()) { |
||
67 | self::initDimensionsWindows(); |
||
68 | } elseif ($sttyString = static::getSttyColumns()) { |
||
69 | self::initDimensionsUnix($sttyString); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | protected static function onWindows(): bool |
||
77 | { |
||
78 | return '\\' === \DIRECTORY_SEPARATOR; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @codeCoverageIgnore |
||
83 | */ |
||
84 | protected static function initDimensionsWindows(): void |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @codeCoverageIgnore |
||
101 | * |
||
102 | * Runs and parses mode CON if it's available, suppressing any error output. |
||
103 | * |
||
104 | * @return int[]|null An array composed of the width and the height or null if it could not be parsed |
||
105 | */ |
||
106 | protected static function getConsoleMode(): ?array |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @codeCoverageIgnore |
||
133 | * |
||
134 | * Runs and parses stty -a if it's available, suppressing any error output. |
||
135 | * |
||
136 | * @return string|null |
||
137 | */ |
||
138 | protected static function getSttyColumns(): ?string |
||
139 | { |
||
140 | if (!\function_exists('proc_open')) { |
||
141 | return null; |
||
142 | } |
||
143 | |||
144 | $descriptorSpec = [ |
||
145 | 1 => ['pipe', 'w'], |
||
146 | 2 => ['pipe', 'w'], |
||
147 | ]; |
||
148 | |||
149 | $process = proc_open( |
||
150 | 'stty -a | grep columns', |
||
151 | $descriptorSpec, |
||
152 | $pipes, |
||
153 | null, |
||
154 | null, |
||
155 | ['suppress_errors' => true] |
||
156 | ); |
||
157 | |||
158 | if (\is_resource($process)) { |
||
159 | $info = stream_get_contents($pipes[1]); |
||
160 | fclose($pipes[1]); |
||
161 | fclose($pipes[2]); |
||
162 | proc_close($process); |
||
163 | |||
164 | return $info ?: null; |
||
165 | } |
||
166 | return null; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @codeCoverageIgnore |
||
171 | * @param string $sttyString |
||
172 | */ |
||
173 | protected static function initDimensionsUnix(string $sttyString): void |
||
174 | { |
||
175 | if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { |
||
176 | // extract [w, h] from "rows h; columns w;" |
||
177 | static::$width = (int)$matches[2]; |
||
178 | static::$height = (int)$matches[1]; |
||
179 | } elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { |
||
180 | // extract [w, h] from "; h rows; w columns" |
||
181 | static::$width = (int)$matches[2]; |
||
182 | static::$height = (int)$matches[1]; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Gets the terminal height. |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | protected function getHeight(): int |
||
202 | // @codeCoverageIgnoreEnd |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return bool |
||
207 | */ |
||
208 | protected function check256ColorSupport(): bool |
||
209 | { |
||
210 | return |
||
211 | $this->supportsColor() ? |
||
|
|||
212 | $this->checkFor256ColorSupport(static::ENV_TERM) || |
||
213 | $this->checkFor256ColorSupport(static::ENV_DOCKER_TERM) : |
||
214 | false; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns true if the stream supports colorization. |
||
219 | * |
||
220 | * Colorization is disabled if not supported by the stream: |
||
221 | * |
||
222 | * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo |
||
223 | * terminals via named pipes, so we can only check the environment. |
||
224 | * |
||
225 | * Reference: Composer\XdebugHandler\Process::supportsColor |
||
226 | * https://github.com/composer/xdebug-handler |
||
227 | * |
||
228 | * Reference: Symfony\Component\Console\Output\StreamOutput::hasColorSupport() |
||
229 | * https://github.com/symfony/console |
||
230 | * |
||
231 | * @return bool true if the stream supports colorization, false otherwise |
||
232 | */ |
||
233 | protected function hasColorSupport(): bool |
||
264 | // @codeCoverageIgnoreEnd |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param string $varName |
||
269 | * @return bool |
||
270 | */ |
||
271 | protected function checkFor256ColorSupport(string $varName): bool |
||
278 | } |
||
279 | } |
||
280 |