Skip to content

Commit 6483c98

Browse files
jrfnlgrogy
authored andcommitted
ErrorFormatter: support PHP Console Highlighter 1.0.0
PHP Console Highlighter 1.0.0 has just been released. This PR adds cross-version support for both PHP Console Highlighter < 1.0.0 and 1.0.0+ to PHP Parallel Lint, which allows people to update the Highlighter dependency to the latest version. Includes changelog entry to allow this PR to be included in the 1.3.2 release. Ref: https://github.com/php-parallel-lint/PHP-Console-Highlighter/releases/tag/v1.0.0
1 parent 8fba43e commit 6483c98

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
[Unreleased]: https://github.com/php-parallel-lint/PHP-Parallel-Lint/compare/v1.3.2...HEAD
1010

11-
## [1.3.2] - 2022-02-17
11+
## [1.3.2] - 2022-02-19
12+
13+
### Added
14+
15+
- Support for PHP Console Highlighter 1.0.0, which comes with PHP Console Color 1.0.1, [#92] from [@jrfnl].
1216

1317
### Fixed
1418

@@ -56,6 +60,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5660
[#84]: https://github.com/php-parallel-lint/PHP-Parallel-Lint/pull/84
5761
[#88]: https://github.com/php-parallel-lint/PHP-Parallel-Lint/pull/88
5862
[#89]: https://github.com/php-parallel-lint/PHP-Parallel-Lint/pull/89
63+
[#92]: https://github.com/php-parallel-lint/PHP-Parallel-Lint/pull/92
5964

6065

6166
## [1.3.1] - 2021-08-13

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"require-dev": {
2121
"nette/tester": "^1.3 || ^2.0",
22-
"php-parallel-lint/php-console-highlighter": "~0.3",
22+
"php-parallel-lint/php-console-highlighter": "0.* || ^1.0",
2323
"squizlabs/php_codesniffer": "^3.6"
2424
},
2525
"suggest": {

src/ErrorFormatter.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
namespace JakubOnderka\PhpParallelLint;
33

4-
use JakubOnderka\PhpConsoleColor\ConsoleColor;
5-
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
4+
use JakubOnderka\PhpConsoleColor\ConsoleColor as OldConsoleColor;
5+
use JakubOnderka\PhpConsoleHighlighter\Highlighter as OldHighlighter;
6+
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
7+
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;
68

79
class ErrorFormatter
810
{
@@ -111,15 +113,26 @@ protected function getCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $lin
111113
protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2)
112114
{
113115
if (
114-
!class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter') ||
115-
!class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')
116+
class_exists('\PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter')
117+
&& class_exists('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')
116118
) {
117-
return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter);
119+
// Highlighter and ConsoleColor 1.0+.
120+
$colors = new ConsoleColor();
121+
$colors->setForceStyle($this->forceColors);
122+
$highlighter = new Highlighter($colors);
123+
} else if (
124+
class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter')
125+
&& class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')
126+
) {
127+
// Highlighter and ConsoleColor < 1.0.
128+
$colors = new OldConsoleColor();
129+
$colors->setForceStyle($this->forceColors);
130+
$highlighter = new OldHighlighter($colors);
118131
}
119132

120-
$colors = new ConsoleColor();
121-
$colors->setForceStyle($this->forceColors);
122-
$highlighter = new Highlighter($colors);
133+
if (isset($colors, $highlighter) === false) {
134+
return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter);
135+
}
123136

124137
$fileContent = file_get_contents($filePath);
125138
return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);

src/Output.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,17 @@ public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ign
456456

457457
class TextOutputColored extends TextOutput
458458
{
459-
/** @var \JakubOnderka\PhpConsoleColor\ConsoleColor */
459+
/** @var \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor|\JakubOnderka\PhpConsoleColor\ConsoleColor */
460460
private $colors;
461461

462462
public function __construct(IWriter $writer, $colors = Settings::AUTODETECT)
463463
{
464464
parent::__construct($writer);
465465

466-
if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
466+
if (class_exists('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')) {
467+
$this->colors = new \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor();
468+
$this->colors->setForceStyle($colors === Settings::FORCED);
469+
} else if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
467470
$this->colors = new \JakubOnderka\PhpConsoleColor\ConsoleColor();
468471
$this->colors->setForceStyle($colors === Settings::FORCED);
469472
}
@@ -472,11 +475,14 @@ public function __construct(IWriter $writer, $colors = Settings::AUTODETECT)
472475
/**
473476
* @param string $string
474477
* @param string $type
475-
* @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
478+
* @throws \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException|\JakubOnderka\PhpConsoleColor\InvalidStyleException
476479
*/
477480
public function write($string, $type = self::TYPE_DEFAULT)
478481
{
479-
if (!$this->colors instanceof \JakubOnderka\PhpConsoleColor\ConsoleColor) {
482+
if (
483+
!$this->colors instanceof \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor
484+
&& !$this->colors instanceof \JakubOnderka\PhpConsoleColor\ConsoleColor
485+
) {
480486
parent::write($string, $type);
481487
} else {
482488
switch ($type) {

0 commit comments

Comments
 (0)