Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
OutputCapabilityDetector
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 supportsAnsi
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Output;
21
22use Symfony\Component\Console\Output\OutputInterface;
23use Symfony\Component\Console\Output\StreamOutput;
24use Throwable;
25
26use function Safe\stream_isatty;
27
28/**
29 * Detects ANSI-friendly output by decoration state or TTY-backed streams.
30 */
31 class OutputCapabilityDetector implements OutputCapabilityDetectorInterface
32{
33    /**
34     * Determines whether the output supports ANSI-capable human interaction.
35     *
36     * @param OutputInterface $output the output to inspect
37     *
38     * @return bool true when the output is decorated or connected to a TTY
39     */
40    public function supportsAnsi(OutputInterface $output): bool
41    {
42        if ($output->isDecorated()) {
43            return true;
44        }
45
46        if (! $output instanceof StreamOutput) {
47            return false;
48        }
49
50        try {
51            return stream_isatty($output->getStream());
52        } catch (Throwable) {
53            return false;
54        }
55    }
56}