Skip to content

✨ Improve display of deprecation warning #61

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

Merged
merged 1 commit into from
May 29, 2019
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
40 changes: 23 additions & 17 deletions TwigCS/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ public function run($files, Ruleset $ruleset)

// setUp
$report = new Report();
set_error_handler(function ($type, $msg) use ($report) {
if (E_USER_DEPRECATED === $type) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_NOTICE,
$msg,
null,
null
);

$report->addMessage($sniffViolation);
}
});

foreach ($ruleset->getSniffs() as $sniff) {
if ($sniff instanceof PostParserSniffInterface) {
$this->sniffsExtension->addSniff($sniff);
Expand All @@ -93,14 +80,15 @@ public function run($files, Ruleset $ruleset)

// Process
foreach ($files as $file) {
$this->setErrorHandler($report, $file);
$this->processTemplate($file, $ruleset, $report);

// Add this file to the report.
$report->addFile($file);
}
restore_error_handler();

// tearDown
restore_error_handler();
foreach ($ruleset->getSniffs() as $sniff) {
if ($sniff instanceof PostParserSniffInterface) {
$this->sniffsExtension->removeSniff($sniff);
Expand Down Expand Up @@ -132,8 +120,8 @@ public function processTemplate($file, $ruleset, $report)
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_ERROR,
$e->getRawMessage(),
$e->getTemplateLine(),
$e->getSourceContext()->getName()
$e->getSourceContext()->getName(),
$e->getTemplateLine()
);

$report->addMessage($sniffViolation);
Expand All @@ -148,7 +136,6 @@ public function processTemplate($file, $ruleset, $report)
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_ERROR,
sprintf('Unable to tokenize file'),
null,
(string) $file
);

Expand All @@ -167,4 +154,23 @@ public function processTemplate($file, $ruleset, $report)

return true;
}

/**
* @param Report $report
* @param string|null $file
*/
protected function setErrorHandler(Report $report, $file = null)
{
set_error_handler(function ($type, $message) use ($report, $file) {
if (E_USER_DEPRECATED === $type) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_NOTICE,
$message,
$file
);

$report->addMessage($sniffViolation);
}
});
}
}
14 changes: 7 additions & 7 deletions TwigCS/Report/SniffViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SniffViolation
/**
* Line number for the violation.
*
* @var int
* @var int|null
*/
protected $line;

Expand All @@ -52,12 +52,12 @@ class SniffViolation
protected $sniff;

/**
* @param int $level
* @param string $message
* @param int $line
* @param string $filename
* @param int $level
* @param string $message
* @param string $filename
* @param int|null $line
*/
public function __construct($level, $message, $line, $filename)
public function __construct($level, $message, $filename, $line = null)
{
$this->level = $level;
$this->message = $message;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function getMessage()
/**
* Get the line number where this violation occured.
*
* @return int
* @return int|null
*/
public function getLine()
{
Expand Down
4 changes: 2 additions & 2 deletions TwigCS/Sniff/AbstractPostParserSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function addMessage($messageType, $message, Node $node)
$sniffViolation = new SniffViolation(
$messageType,
$message,
$this->getTemplateLine($node),
$this->getTemplateName($node)
$this->getTemplateName($node),
$this->getTemplateLine($node)
);

$this->getReport()->addMessage($sniffViolation);
Expand Down
7 changes: 6 additions & 1 deletion TwigCS/Sniff/AbstractPreParserSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ public function isTokenMatching(Token $token, $type, $value = null)
*/
public function addMessage($messageType, $message, Token $token)
{
$sniffViolation = new SniffViolation($messageType, $message, $token->getLine(), $token->getFilename());
$sniffViolation = new SniffViolation(
$messageType,
$message,
$token->getFilename(),
$token->getLine()
);
$sniffViolation->setLinePosition($token->getPosition());

$this->getReport()->addMessage($sniffViolation);
Expand Down