Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorFormatter: support PHP Console Highlighter 1.0.0 #92

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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