Created
November 9, 2016 14:58
-
-
Save marcovtwout/129d1ef5b17eca0d7ade89e4d55da360 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Sstalle\php7cc; | |
use PhpParser\PrettyPrinter\Standard as StandardPrettyPrinter; | |
use Sstalle\php7cc\CompatibilityViolation\CheckMetadata; | |
use Sstalle\php7cc\CompatibilityViolation\ContextInterface; | |
use Sstalle\php7cc\CompatibilityViolation\Message; | |
class CLIResultPrinter implements ResultPrinterInterface | |
{ | |
/** | |
* @var CLIOutputInterface | |
*/ | |
protected $output; | |
/** | |
* @var StandardPrettyPrinter | |
*/ | |
protected $prettyPrinter; | |
/** | |
* @var NodeStatementsRemover | |
*/ | |
protected $nodeStatementsRemover; | |
private static $levelLabels = [ | |
Message::LEVEL_INFO => 'info', | |
Message::LEVEL_WARNING => 'warning', | |
Message::LEVEL_ERROR => 'error', | |
]; | |
/** | |
* @param CLIOutputInterface $output | |
* @param StandardPrettyPrinter $prettyPrinter | |
* @param NodeStatementsRemover $nodeStatementsRemover | |
*/ | |
public function __construct( | |
CLIOutputInterface $output, | |
StandardPrettyPrinter $prettyPrinter, | |
NodeStatementsRemover $nodeStatementsRemover | |
) { | |
$this->output = $output; | |
$this->prettyPrinter = $prettyPrinter; | |
$this->nodeStatementsRemover = $nodeStatementsRemover; | |
$this->output->writeln("File\tLine\tLevel\tMessage\tCode"); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function printContext(ContextInterface $context) | |
{ | |
$file = sprintf('%s', $context->getCheckedResourceName()); | |
foreach ($context->getMessages() as $message) { | |
$this->output->writeln( | |
$file . "\t" . $this->formatMessage($message) | |
); | |
} | |
foreach ($context->getErrors() as $error) { | |
$this->output->writeln( | |
$file . "\t\t\t" . $error->getText() | |
); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function printMetadata(CheckMetadata $metadata) | |
{ | |
} | |
/** | |
* @param Message $message | |
* | |
* @return string | |
*/ | |
private function formatMessage(Message $message) | |
{ | |
$nodes = $this->nodeStatementsRemover->removeInnerStatements($message->getNodes()); | |
$prettyPrintedNodes = str_replace("\n", "\n ", $this->prettyPrinter->prettyPrint($nodes)); | |
$text = $message->getRawText(); | |
return sprintf( | |
"%s\t%s\t%s\t%s", | |
$message->getLine(), | |
self::$levelLabels[$message->getLevel()], | |
$text, | |
str_replace(["\n", "\t"], "", $prettyPrintedNodes) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment