Skip to content

Commit

Permalink
ErrorFormatter: support PHP Console Highlighter 1.0.0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl authored and grogy committed Feb 21, 2022
1 parent 8fba43e commit 6483c98
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

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

## [1.3.2] - 2022-02-17
## [1.3.2] - 2022-02-19

### Added

- Support for PHP Console Highlighter 1.0.0, which comes with PHP Console Color 1.0.1, [#92] from [@jrfnl].

### Fixed

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


## [1.3.1] - 2021-08-13
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require-dev": {
"nette/tester": "^1.3 || ^2.0",
"php-parallel-lint/php-console-highlighter": "~0.3",
"php-parallel-lint/php-console-highlighter": "0.* || ^1.0",
"squizlabs/php_codesniffer": "^3.6"
},
"suggest": {
Expand Down
29 changes: 21 additions & 8 deletions src/ErrorFormatter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace JakubOnderka\PhpParallelLint;

use JakubOnderka\PhpConsoleColor\ConsoleColor;
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
use JakubOnderka\PhpConsoleColor\ConsoleColor as OldConsoleColor;
use JakubOnderka\PhpConsoleHighlighter\Highlighter as OldHighlighter;
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;

class ErrorFormatter
{
Expand Down Expand Up @@ -111,15 +113,26 @@ protected function getCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $lin
protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2)
{
if (
!class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter') ||
!class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')
class_exists('\PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter')
&& class_exists('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')
) {
return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter);
// Highlighter and ConsoleColor 1.0+.
$colors = new ConsoleColor();
$colors->setForceStyle($this->forceColors);
$highlighter = new Highlighter($colors);
} else if (
class_exists('\JakubOnderka\PhpConsoleHighlighter\Highlighter')
&& class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')
) {
// Highlighter and ConsoleColor < 1.0.
$colors = new OldConsoleColor();
$colors->setForceStyle($this->forceColors);
$highlighter = new OldHighlighter($colors);
}

$colors = new ConsoleColor();
$colors->setForceStyle($this->forceColors);
$highlighter = new Highlighter($colors);
if (isset($colors, $highlighter) === false) {
return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter);
}

$fileContent = file_get_contents($filePath);
return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter);
Expand Down
14 changes: 10 additions & 4 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,17 @@ public function writeResult(Result $result, ErrorFormatter $errorFormatter, $ign

class TextOutputColored extends TextOutput
{
/** @var \JakubOnderka\PhpConsoleColor\ConsoleColor */
/** @var \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor|\JakubOnderka\PhpConsoleColor\ConsoleColor */
private $colors;

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

if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
if (class_exists('\PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor')) {
$this->colors = new \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor();
$this->colors->setForceStyle($colors === Settings::FORCED);
} else if (class_exists('\JakubOnderka\PhpConsoleColor\ConsoleColor')) {
$this->colors = new \JakubOnderka\PhpConsoleColor\ConsoleColor();
$this->colors->setForceStyle($colors === Settings::FORCED);
}
Expand All @@ -472,11 +475,14 @@ public function __construct(IWriter $writer, $colors = Settings::AUTODETECT)
/**
* @param string $string
* @param string $type
* @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException
* @throws \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException|\JakubOnderka\PhpConsoleColor\InvalidStyleException
*/
public function write($string, $type = self::TYPE_DEFAULT)
{
if (!$this->colors instanceof \JakubOnderka\PhpConsoleColor\ConsoleColor) {
if (
!$this->colors instanceof \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor
&& !$this->colors instanceof \JakubOnderka\PhpConsoleColor\ConsoleColor
) {
parent::write($string, $type);
} else {
switch ($type) {
Expand Down

0 comments on commit 6483c98

Please sign in to comment.