-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a renderer which writes an ansi report string
- Loading branch information
Showing
6 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
namespace PHPMD\Renderer; | ||
|
||
use PHPMD\AbstractRenderer; | ||
use PHPMD\ProcessingError; | ||
use PHPMD\Report; | ||
use PHPMD\RuleViolation; | ||
|
||
/** | ||
* This renderer output a command line friendly log with all found violations | ||
* and suspect software artifacts. | ||
*/ | ||
class AnsiRenderer extends AbstractRenderer | ||
{ | ||
|
||
/** | ||
* @param \PHPMD\Report $report | ||
* @return void | ||
*/ | ||
public function renderReport(Report $report) | ||
{ | ||
$this->writeViolationsReport($report); | ||
$this->writeErrorsReport($report); | ||
$this->writeReportSummary($report); | ||
} | ||
|
||
/** | ||
* @param \PHPMD\Report $report | ||
* @return void | ||
*/ | ||
private function writeViolationsReport(Report $report) | ||
{ | ||
if ($report->isEmpty()) { | ||
return; | ||
} | ||
|
||
$padding = $this->getMaxLineNumberLength($report); | ||
$previousFile = null; | ||
foreach ($report->getRuleViolations() as $violation) { | ||
if ($violation->getFileName() !== $previousFile) { | ||
$this->writeViolationFileHeader($violation); | ||
} | ||
|
||
$this->writeViolationLine($violation, $padding); | ||
$previousFile = $violation->getFileName(); | ||
} | ||
} | ||
|
||
/** | ||
* @param \PHPMD\Report $report | ||
* @return int|null | ||
*/ | ||
private function getMaxLineNumberLength(Report $report) | ||
{ | ||
$maxLength = null; | ||
foreach ($report->getRuleViolations() as $violation) { | ||
if ($maxLength === null) { | ||
$maxLength = strlen($violation->getBeginLine()); | ||
continue; | ||
} | ||
if (strlen($violation->getBeginLine()) > $maxLength) { | ||
$maxLength = strlen($violation->getBeginLine()); | ||
} | ||
} | ||
return $maxLength; | ||
} | ||
|
||
/** | ||
* @param \PHPMD\RuleViolation $violation | ||
* @return void | ||
*/ | ||
private function writeViolationFileHeader(RuleViolation $violation) | ||
{ | ||
$fileHeader = sprintf( | ||
'FILE: %s', | ||
$violation->getFileName() | ||
); | ||
$this->getWriter()->write( | ||
PHP_EOL . $fileHeader . PHP_EOL . | ||
str_repeat('-', strlen($fileHeader)) . PHP_EOL | ||
); | ||
} | ||
|
||
/** | ||
* @param \PHPMD\RuleViolation $violation | ||
* @param int $padding | ||
* @return void | ||
*/ | ||
private function writeViolationLine(RuleViolation $violation, $padding) | ||
{ | ||
$this->getWriter()->write(sprintf( | ||
" %s | \e[31mVIOLATION\e[0m | %s" . PHP_EOL, | ||
str_pad($violation->getBeginLine(), $padding, ' '), | ||
$violation->getDescription() | ||
)); | ||
} | ||
|
||
/** | ||
* @param \PHPMD\Report $report | ||
* @return void | ||
*/ | ||
private function writeErrorsReport(Report $report) | ||
{ | ||
if (!$report->hasErrors()) { | ||
return; | ||
} | ||
|
||
/** @var ProcessingError $error */ | ||
foreach ($report->getErrors() as $error) { | ||
$errorHeader = sprintf( | ||
"\e[33mERROR\e[0m while parsing %s", | ||
$error->getFile() | ||
); | ||
|
||
$this->getWriter()->write( | ||
PHP_EOL . $errorHeader . PHP_EOL . | ||
str_repeat('-', strlen($errorHeader) - 9) . PHP_EOL | ||
); | ||
|
||
$this->getWriter()->write(sprintf( | ||
'%s' . PHP_EOL, | ||
$error->getMessage() | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* @param \PHPMD\Report $report | ||
* @return void | ||
*/ | ||
private function writeReportSummary(Report $report) | ||
{ | ||
$this->getWriter()->write( | ||
sprintf( | ||
PHP_EOL . 'Found %s %s and %s %s in %sms' . PHP_EOL, | ||
count($report->getRuleViolations()), | ||
count($report->getRuleViolations()) !== 1 ? 'violations' : 'violation', | ||
iterator_count($report->getErrors()), | ||
iterator_count($report->getErrors()) !== 1 ? 'errors' : 'error', | ||
$report->getElapsedTimeInMillis() | ||
) | ||
); | ||
if (count($report->getRuleViolations()) === 0 && iterator_count($report->getErrors()) === 0) { | ||
$this->getWriter()->write(PHP_EOL . "\e[32mNo mess detected\e[0m" . PHP_EOL); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Renderer; | ||
|
||
use PHPMD\AbstractTest; | ||
use PHPMD\Stubs\WriterStub; | ||
|
||
/** | ||
* Test case for the ansi renderer implementation. | ||
* | ||
* @covers \PHPMD\Renderer\AnsiRendererTest | ||
*/ | ||
class AnsiRendererTest extends AbstractTest | ||
{ | ||
/** | ||
* testRendererOutputsForReportWithContents | ||
* | ||
* @return void | ||
*/ | ||
public function testRendererOutputsForReportWithContents() | ||
{ | ||
$writer = new WriterStub(); | ||
|
||
$violations = array( | ||
$this->getRuleViolationMock('/bar.php', 1), | ||
$this->getRuleViolationMock('/foo.php', 2), | ||
$this->getRuleViolationMock('/foo.php', 3), | ||
); | ||
|
||
$errors = array( | ||
$this->getErrorMock(), | ||
); | ||
|
||
$report = $this->getReportMock(0); | ||
$report->expects($this->atLeastOnce()) | ||
->method('getRuleViolations') | ||
->will($this->returnValue(new \ArrayIterator($violations))); | ||
$report->expects($this->atLeastOnce()) | ||
->method('isEmpty') | ||
->will($this->returnValue(false)); | ||
$report->expects($this->atLeastOnce()) | ||
->method('hasErrors') | ||
->will($this->returnValue(true)); | ||
$report->expects($this->atLeastOnce()) | ||
->method('getErrors') | ||
->will($this->returnValue(new \ArrayIterator($errors))); | ||
$report->expects($this->once()) | ||
->method('getElapsedTimeInMillis') | ||
->will($this->returnValue(200)); | ||
|
||
$renderer = new AnsiRenderer(); | ||
$renderer->setWriter($writer); | ||
|
||
$renderer->start(); | ||
$renderer->renderReport($report); | ||
$renderer->end(); | ||
|
||
$expectedChunks = [ | ||
PHP_EOL . "FILE: /bar.php" . PHP_EOL . "--------------" . PHP_EOL, | ||
" 1 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL, | ||
PHP_EOL . "FILE: /foo.php" . PHP_EOL . "--------------" . PHP_EOL, | ||
" 2 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL, | ||
" 3 | \e[31mVIOLATION\e[0m | Test description" . PHP_EOL, | ||
PHP_EOL . "\e[33mERROR\e[0m while parsing /foo/baz.php" . PHP_EOL . "--------------------------------" . PHP_EOL, | ||
"Error in file \"/foo/baz.php\"" . PHP_EOL, | ||
PHP_EOL . "Found 3 violations and 1 error in 200ms" . PHP_EOL, | ||
]; | ||
|
||
foreach($writer->getChunks() as $i => $chunk) { | ||
$this->assertEquals( | ||
$expectedChunks[$i], | ||
$chunk, | ||
sprintf('Chunk %s did not match expected string', $i) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* testRendererOutputsForReportWithoutContents | ||
* | ||
* @return void | ||
*/ | ||
public function testRendererOutputsForReportWithoutContents() | ||
{ | ||
$writer = new WriterStub(); | ||
|
||
$report = $this->getReportMock(0); | ||
$report->expects($this->atLeastOnce()) | ||
->method('getRuleViolations') | ||
->will($this->returnValue(new \ArrayIterator([]))); | ||
$report->expects($this->atLeastOnce()) | ||
->method('isEmpty') | ||
->will($this->returnValue(true)); | ||
$report->expects($this->atLeastOnce()) | ||
->method('hasErrors') | ||
->will($this->returnValue(false)); | ||
$report->expects($this->atLeastOnce()) | ||
->method('getErrors') | ||
->will($this->returnValue(new \ArrayIterator([]))); | ||
$report->expects($this->once()) | ||
->method('getElapsedTimeInMillis') | ||
->will($this->returnValue(200)); | ||
|
||
$renderer = new AnsiRenderer(); | ||
$renderer->setWriter($writer); | ||
|
||
$renderer->start(); | ||
$renderer->renderReport($report); | ||
$renderer->end(); | ||
|
||
$expectedChunks = [ | ||
PHP_EOL . "Found 0 violations and 0 errors in 200ms" . PHP_EOL, | ||
PHP_EOL . "\e[32mNo mess detected\e[0m" . PHP_EOL, | ||
]; | ||
|
||
foreach($writer->getChunks() as $i => $chunk) { | ||
$this->assertEquals( | ||
$expectedChunks[$i], | ||
$chunk, | ||
sprintf('Chunk %s did not match expected string', $i) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters