Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dv336699/6155b9cf18f70f960895e7fa40ea0bdd to your computer and use it in GitHub Desktop.
Save dv336699/6155b9cf18f70f960895e7fa40ea0bdd to your computer and use it in GitHub Desktop.
<?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