Skip to content

Commit 005a3e1

Browse files
committed
Changed internal setting "colors" to enum values
1 parent 08094a2 commit 005a3e1

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

src/ErrorFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
class ErrorFormatter
3737
{
38-
/** @var bool */
38+
/** @var string */
3939
private $useColors;
4040

4141
/** @var bool */
@@ -44,7 +44,7 @@ class ErrorFormatter
4444
/** @var bool */
4545
private $translateTokens;
4646

47-
public function __construct($useColors = false, $translateTokens = false, $forceColors = false)
47+
public function __construct($useColors = Settings::AUTODETECT, $translateTokens = false, $forceColors = false)
4848
{
4949
$this->useColors = $useColors;
5050
$this->forceColors = $forceColors;
@@ -82,7 +82,7 @@ public function formatSyntaxErrorMessage(SyntaxError $error, $withCodeSnipped =
8282
$string .= ":$onLine" . PHP_EOL;
8383

8484
if ($withCodeSnipped) {
85-
if ($this->useColors) {
85+
if ($this->useColors !== Settings::DISABLED) {
8686
$string .= $this->getColoredCodeSnippet($error->getFilePath(), $onLine);
8787
} else {
8888
$string .= $this->getCodeSnippet($error->getFilePath(), $onLine);

src/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function getDefaultOutput(Settings $settings)
107107
if ($settings->json) {
108108
return new JsonOutput($writer);
109109
} else {
110-
return ($settings->colors ? new TextOutputColored($writer, $settings->forceColors) : new TextOutput($writer));
110+
return ($settings->colors !== Settings::DISABLED ? new TextOutputColored($writer, $settings->colors) : new TextOutput($writer));
111111
}
112112
}
113113

src/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ class TextOutputColored extends TextOutput
320320
/** @var \JakubOnderka\PhpConsoleColor\ConsoleColor */
321321
private $colors;
322322

323-
public function __construct(IWriter $writer, $forceColors = false)
323+
public function __construct(IWriter $writer, $colors = Settings::AUTODETECT)
324324
{
325325
parent::__construct($writer);
326326

327327
if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
328328
$this->colors = new \JakubOnderka\PhpConsoleColor\ConsoleColor();
329-
$this->colors->setForceStyle($forceColors);
329+
$this->colors->setForceStyle($colors === Settings::FORCED);
330330
}
331331
}
332332

src/Settings.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333
class Settings
3434
{
35+
36+
/**
37+
* constants for enum settings
38+
*/
39+
const FORCED = 'FORCED';
40+
const DISABLED = 'DISABLED';
41+
const AUTODETECT = 'AUTODETECT';
42+
3543
/**
3644
* Path to PHP executable
3745
* @var string
@@ -75,16 +83,10 @@ class Settings
7583
public $excluded = array();
7684

7785
/**
78-
* Print to console with colors
79-
* @var bool
80-
*/
81-
public $colors = true;
82-
83-
/**
84-
* Do not autodetect if colors are available
85-
* @var bool
86+
* Mode for color detection. Possible values: self::FORCED, self::DISABLED and self::AUTODETECT
87+
* @var string
8688
*/
87-
public $forceColors = false;
89+
public $colors = self::AUTODETECT;
8890

8991
/**
9092
* Output results as JSON string
@@ -165,12 +167,11 @@ public static function parseArguments(array $arguments)
165167
break;
166168

167169
case '--colors':
168-
$settings->forceColors = true;
169-
$settings->colors = true;
170+
$settings->colors = self::FORCED;
170171
break;
171172

172173
case '--no-colors':
173-
$settings->colors = false;
174+
$settings->colors = self::DISABLED;
174175
break;
175176

176177
case '--json':

0 commit comments

Comments
 (0)