humbug /
box
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the box project. |
||
| 7 | * |
||
| 8 | * (c) Kevin Herrera <[email protected]> |
||
| 9 | * Théo Fidry <[email protected]> |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace KevinGH\RequirementChecker; |
||
| 16 | |||
| 17 | use function array_shift; |
||
| 18 | use function count; |
||
| 19 | use function explode; |
||
| 20 | use function ltrim; |
||
| 21 | use function min; |
||
| 22 | use function sprintf; |
||
| 23 | use function str_pad; |
||
| 24 | use function str_repeat; |
||
| 25 | use function strlen; |
||
| 26 | use function trim; |
||
| 27 | use function wordwrap; |
||
| 28 | use const PHP_EOL; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @private |
||
| 32 | */ |
||
| 33 | final class Printer |
||
| 34 | { |
||
| 35 | private $styles = [ |
||
| 36 | 'reset' => "\033[0m", |
||
| 37 | 'red' => "\033[31m", |
||
| 38 | 'green' => "\033[32m", |
||
| 39 | 'yellow' => "\033[33m", |
||
| 40 | 'title' => "\033[33m", |
||
| 41 | 'error' => "\033[37;41m", |
||
| 42 | 'success' => "\033[30;42m", |
||
| 43 | ]; |
||
| 44 | private $verbosity; |
||
| 45 | private $supportColors; |
||
| 46 | private $width; |
||
| 47 | |||
| 48 | public function __construct(int $verbosity, bool $supportColors, ?int $width = null) |
||
| 49 | { |
||
| 50 | if (null === $width) { |
||
| 51 | $terminal = new Terminal(); |
||
| 52 | $width = $terminal->getWidth(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->verbosity = $verbosity; |
||
| 56 | $this->supportColors = $supportColors; |
||
| 57 | $this->width = $width ?: 80; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getVerbosity(): int |
||
| 61 | { |
||
| 62 | return $this->verbosity; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function setVerbosity($verbosity): void |
||
| 66 | { |
||
| 67 | $this->verbosity = $verbosity; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function title(string $title, int $verbosity, ?string $style = null): void |
||
| 71 | { |
||
| 72 | if (null === $style) { |
||
| 73 | $style = 'title'; |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->printvln('', $verbosity, $style); |
||
| 77 | $this->printvln($title, $verbosity, $style); |
||
| 78 | $this->printvln( |
||
| 79 | str_repeat( |
||
| 80 | '=', |
||
| 81 | min(strlen($title), $this->width) |
||
| 82 | ), |
||
| 83 | $verbosity, |
||
| 84 | $style |
||
| 85 | ); |
||
| 86 | $this->printvln('', $verbosity, $style); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getRequirementErrorMessage(Requirement $requirement): ?string |
||
| 90 | { |
||
| 91 | if ($requirement->isFulfilled()) { |
||
| 92 | return null; |
||
| 93 | } |
||
| 94 | |||
| 95 | return wordwrap($requirement->getHelpText(), $this->width - 3, PHP_EOL.' ').PHP_EOL; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function block(string $title, string $message, int $verbosity, ?string $style = null): void |
||
| 99 | { |
||
| 100 | $prefix = ' ['.$title.'] '; |
||
| 101 | $lineLength = $this->width - strlen($prefix) - 1; |
||
| 102 | if ($lineLength < 0) { |
||
| 103 | $lineLength = 0; |
||
| 104 | } |
||
| 105 | $message = $prefix.trim($message); |
||
| 106 | |||
| 107 | $lines = []; |
||
| 108 | |||
| 109 | $remainingMessage = $message; |
||
| 110 | |||
| 111 | $wrapped = wordwrap($remainingMessage, $lineLength, '¬'); |
||
| 112 | $wrapped = explode('¬', $wrapped); |
||
| 113 | |||
| 114 | do { |
||
| 115 | $line = array_shift($wrapped); |
||
| 116 | if ($lines && $lineLength > 0) { |
||
|
0 ignored issues
–
show
|
|||
| 117 | $line = str_repeat(' ', strlen($prefix)).ltrim($line); |
||
| 118 | } |
||
| 119 | $lines[] = str_pad($line, $this->width, ' ', STR_PAD_RIGHT); |
||
| 120 | } while (count($wrapped)); |
||
| 121 | |||
| 122 | $this->printvln('', $verbosity); |
||
| 123 | $this->printvln(str_repeat(' ', $this->width), $verbosity, $style); |
||
| 124 | foreach ($lines as $line) { |
||
| 125 | $this->printvln($line, $verbosity, $style); |
||
| 126 | } |
||
| 127 | $this->printv(str_repeat(' ', $this->width), $verbosity, $style); |
||
| 128 | $this->printvln('', $verbosity); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function printvln(string $message, int $verbosity, ?string $style = null): void |
||
| 132 | { |
||
| 133 | $this->printv($message, $verbosity, $style); |
||
| 134 | $this->printv(PHP_EOL, $verbosity, null); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function printv(string $message, int $verbosity, ?string $style = null): void |
||
| 138 | { |
||
| 139 | if ($verbosity > $this->verbosity) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | $message = wordwrap($message, $this->width); |
||
| 144 | |||
| 145 | $message = sprintf( |
||
| 146 | '%s%s%s', |
||
| 147 | $this->supportColors && isset($this->styles[$style]) ? $this->styles[$style] : '', |
||
| 148 | $message, |
||
| 149 | $this->supportColors ? $this->styles['reset'] : '' |
||
| 150 | ); |
||
| 151 | |||
| 152 | if ('1' === getenv('BOX_REQUIREMENTS_CHECKER_LOG_TO_STDOUT')) { |
||
| 153 | // use echo/print to support output buffering |
||
| 154 | echo $message; |
||
| 155 | } else { |
||
| 156 | fwrite(STDERR, $message); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 |
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.