Issues (224)

res/requirement-checker/src/Printer.php (1 issue)

1
<?php
2
3
declare (strict_types=1);
4
namespace HumbugBox451\KevinGH\RequirementChecker;
5
6
use function array_shift;
7
use function count;
8
use function explode;
9
use function ltrim;
10
use function min;
11
use function sprintf;
12
use function str_pad;
13
use function str_repeat;
14
use function strlen;
15
use function trim;
16
use function wordwrap;
17
use const PHP_EOL;
18
/** @internal */
19
final class Printer
20
{
21
    private $styles = ['reset' => "\x1b[0m", 'red' => "\x1b[31m", 'green' => "\x1b[32m", 'yellow' => "\x1b[33m", 'title' => "\x1b[33m", 'error' => "\x1b[37;41m", 'success' => "\x1b[30;42m"];
22
    private $verbosity;
23
    private $supportColors;
24
    private $width;
25
    public function __construct(int $verbosity, bool $supportColors, ?int $width = null)
26
    {
27
        if (null === $width) {
28
            $terminal = new Terminal();
29
            $width = $terminal->getWidth();
30
        }
31
        $this->verbosity = $verbosity;
32
        $this->supportColors = $supportColors;
33
        $this->width = $width ?: 80;
34
    }
35
    public function getVerbosity() : int
36
    {
37
        return $this->verbosity;
38
    }
39
    public function setVerbosity($verbosity) : void
40
    {
41
        $this->verbosity = $verbosity;
42
    }
43
    public function title(string $title, int $verbosity, ?string $style = null) : void
44
    {
45
        if (null === $style) {
46
            $style = 'title';
47
        }
48
        $this->printvln('', $verbosity, $style);
49
        $this->printvln($title, $verbosity, $style);
50
        $this->printvln(str_repeat('=', min(strlen($title), $this->width)), $verbosity, $style);
51
        $this->printvln('', $verbosity, $style);
52
    }
53
    public function getRequirementErrorMessage(Requirement $requirement) : ?string
54
    {
55
        if ($requirement->isFulfilled()) {
56
            return null;
57
        }
58
        return wordwrap($requirement->getHelpText(), $this->width - 3, PHP_EOL . '   ') . PHP_EOL;
59
    }
60
    public function block(string $title, string $message, int $verbosity, ?string $style = null) : void
61
    {
62
        $prefix = ' [' . $title . '] ';
63
        $lineLength = $this->width - strlen($prefix) - 1;
64
        if ($lineLength < 0) {
65
            $lineLength = 0;
66
        }
67
        $message = $prefix . trim($message);
68
        $lines = [];
69
        $remainingMessage = $message;
70
        $wrapped = wordwrap($remainingMessage, $lineLength, '¬');
71
        $wrapped = explode('¬', $wrapped);
72
        do {
73
            $line = array_shift($wrapped);
74
            if ($lines && $lineLength > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lines of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
                $line = str_repeat(' ', strlen($prefix)) . ltrim($line);
76
            }
77
            $lines[] = str_pad($line, $this->width, ' ', \STR_PAD_RIGHT);
78
        } while (count($wrapped));
79
        $this->printvln('', $verbosity);
80
        $this->printvln(str_repeat(' ', $this->width), $verbosity, $style);
81
        foreach ($lines as $line) {
82
            $this->printvln($line, $verbosity, $style);
83
        }
84
        $this->printv(str_repeat(' ', $this->width), $verbosity, $style);
85
        $this->printvln('', $verbosity);
86
    }
87
    public function printvln(string $message, int $verbosity, ?string $style = null) : void
88
    {
89
        $this->printv($message, $verbosity, $style);
90
        $this->printv(PHP_EOL, $verbosity, null);
91
    }
92
    public function printv(string $message, int $verbosity, ?string $style = null) : void
93
    {
94
        if ($verbosity > $this->verbosity) {
95
            return;
96
        }
97
        $message = wordwrap($message, $this->width);
98
        $message = sprintf('%s%s%s', $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', $message, $this->supportColors ? $this->styles['reset'] : '');
99
        if ('1' === \getenv('BOX_REQUIREMENTS_CHECKER_LOG_TO_STDOUT')) {
100
            echo $message;
101
        } else {
102
            \fwrite(\STDERR, $message);
103
        }
104
    }
105
}
106