Skip to content

✨ Add operatorSpacing sniff #80

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
Dec 15, 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
4 changes: 3 additions & 1 deletion TwigCS/Report/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Report
const MESSAGE_TYPE_NOTICE = 0;
const MESSAGE_TYPE_WARNING = 1;
const MESSAGE_TYPE_ERROR = 2;
const MESSAGE_TYPE_FATAL = 3;

/**
* @var SniffViolation[]
Expand Down Expand Up @@ -61,6 +62,7 @@ public function addMessage(SniffViolation $sniffViolation)
++$this->totalWarnings;
break;
case self::MESSAGE_TYPE_ERROR:
case self::MESSAGE_TYPE_FATAL:
++$this->totalErrors;
break;
}
Expand All @@ -77,7 +79,7 @@ public function addMessage(SniffViolation $sniffViolation)
*/
public function getMessages(array $filters = [])
{
if (empty($filters)) {
if (0 === count($filters)) {
// Return all messages, without filtering.
return $this->messages;
}
Expand Down
36 changes: 24 additions & 12 deletions TwigCS/Report/SniffViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace TwigCS\Report;

use \LogicException;
use TwigCS\Sniff\SniffInterface;

/**
* Wrapper class that represents a violation to a sniff with context.
*/
class SniffViolation
{
const LEVEL_NOTICE = 'NOTICE';
const LEVEL_WARNING = 'WARNING';
const LEVEL_ERROR = 'ERROR';
const LEVEL_FATAL = 'FATAL';

/**
* Level of the message among `notice`, `warning`, `error`
*
Expand Down Expand Up @@ -86,13 +92,16 @@ public function getLevel()
public function getLevelAsString()
{
switch ($this->level) {
case SniffInterface::MESSAGE_TYPE_NOTICE:
return 'NOTICE';
case SniffInterface::MESSAGE_TYPE_WARNING:
return 'WARNING';
case SniffInterface::MESSAGE_TYPE_ERROR:
case Report::MESSAGE_TYPE_NOTICE:
return self::LEVEL_NOTICE;
case Report::MESSAGE_TYPE_WARNING:
return self::LEVEL_WARNING;
case Report::MESSAGE_TYPE_ERROR:
return self::LEVEL_ERROR;
case Report::MESSAGE_TYPE_FATAL:
return self::LEVEL_FATAL;
default:
return 'ERROR';
throw new LogicException();
}
}

Expand All @@ -106,13 +115,16 @@ public function getLevelAsString()
public static function getLevelAsInt(string $level)
{
switch (strtoupper($level)) {
case 'NOTICE':
return SniffInterface::MESSAGE_TYPE_NOTICE;
case 'WARNING':
return SniffInterface::MESSAGE_TYPE_WARNING;
case 'ERROR':
case self::LEVEL_NOTICE:
return Report::MESSAGE_TYPE_NOTICE;
case self::LEVEL_WARNING:
return Report::MESSAGE_TYPE_WARNING;
case self::LEVEL_ERROR:
return Report::MESSAGE_TYPE_ERROR;
case self::LEVEL_FATAL:
return Report::MESSAGE_TYPE_FATAL;
default:
return SniffInterface::MESSAGE_TYPE_ERROR;
throw new LogicException();
}
}

Expand Down
3 changes: 1 addition & 2 deletions TwigCS/Ruleset/Generic/BlankEOFSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function process(int $tokenPosition, array $tokens)

if (1 !== $i) {
// Either 0 or 2+ blank lines.
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
$fix = $this->addFixableError(
sprintf('A file must end with 1 blank line; found %d', $i),
$token
);
Expand Down
112 changes: 14 additions & 98 deletions TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,35 @@

namespace TwigCS\Ruleset\Generic;

use \Exception;
use TwigCS\Sniff\AbstractSniff;
use TwigCS\Sniff\AbstractSpacingSniff;
use TwigCS\Token\Token;

/**
* Ensure there is one space before and after a delimiter {{, {%, {#, }}, %} and #}
* Ensure there is one space before {{, {%, {#, and after }}, %} and #}
*/
class DelimiterSpacingSniff extends AbstractSniff
class DelimiterSpacingSniff extends AbstractSpacingSniff
{
/**
* @param int $tokenPosition
* @param Token[] $tokens
* @param Token $token
*
* @return Token
*
* @throws Exception
* @return bool
*/
public function process(int $tokenPosition, array $tokens)
protected function shouldHaveSpaceBefore(Token $token)
{
$token = $tokens[$tokenPosition];

if ($this->isTokenMatching($token, Token::VAR_START_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE)
) {
$this->processStart($tokenPosition, $tokens);
}

if ($this->isTokenMatching($token, Token::VAR_END_TYPE)
return $this->isTokenMatching($token, Token::VAR_END_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_END_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE)
) {
$this->processEnd($tokenPosition, $tokens);
}

return $token;
}

/**
* @param int $tokenPosition
* @param Token[] $tokens
*
* @throws Exception
*/
public function processStart(int $tokenPosition, array $tokens)
{
$token = $tokens[$tokenPosition];

// Ignore new line
$next = $this->findNext(Token::WHITESPACE_TYPE, $tokens, $tokenPosition + 1, true);
if ($this->isTokenMatching($tokens[$next], Token::EOL_TYPE)) {
return;
}

if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::WHITESPACE_TYPE)) {
$count = strlen($tokens[$tokenPosition + 1]->getValue());
} else {
$count = 0;
}

if (1 !== $count) {
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count),
$token
);

if ($fix) {
if (0 === $count) {
$this->fixer->addContent($tokenPosition, ' ');
} else {
$this->fixer->replaceToken($tokenPosition + 1, ' ');
}
}
}
|| $this->isTokenMatching($token, Token::COMMENT_END_TYPE);
}

/**
* @param int $tokenPosition
* @param Token[] $tokens
* @param Token $token
*
* @throws Exception
* @return bool
*/
public function processEnd(int $tokenPosition, array $tokens)
protected function shouldHaveSpaceAfter(Token $token)
{
$token = $tokens[$tokenPosition];

// Ignore new line
$previous = $this->findPrevious(Token::WHITESPACE_TYPE, $tokens, $tokenPosition - 1, true);
if ($this->isTokenMatching($tokens[$previous], Token::EOL_TYPE)) {
return;
}

if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::WHITESPACE_TYPE)) {
$count = strlen($tokens[$tokenPosition - 1]->getValue());
} else {
$count = 0;
}

if (1 !== $count) {
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count),
$token
);

if ($fix) {
if (0 === $count) {
$this->fixer->addContentBefore($tokenPosition, ' ');
} else {
$this->fixer->replaceToken($tokenPosition - 1, ' ');
}
}
}
return $this->isTokenMatching($token, Token::VAR_START_TYPE)
|| $this->isTokenMatching($token, Token::BLOCK_START_TYPE)
|| $this->isTokenMatching($token, Token::COMMENT_START_TYPE);
}
}
3 changes: 1 addition & 2 deletions TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function process(int $tokenPosition, array $tokens)
}

if ($found) {
$this->addMessage(
$this::MESSAGE_TYPE_WARNING,
$this->addWarning(
'Probable commented code found; keeping commented code is not advised',
$token
);
Expand Down
3 changes: 1 addition & 2 deletions TwigCS/Ruleset/Generic/EmptyLinesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function process(int $tokenPosition, array $tokens)
}

if (1 < $i) {
$fix = $this->addFixableMessage(
$this::MESSAGE_TYPE_ERROR,
$fix = $this->addFixableError(
sprintf('More than 1 empty lines are not allowed, found %d', $i),
$token
);
Expand Down
32 changes: 32 additions & 0 deletions TwigCS/Ruleset/Generic/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace TwigCS\Ruleset\Generic;

use TwigCS\Sniff\AbstractSpacingSniff;
use TwigCS\Token\Token;

/**
* Ensure there is one space before and after an operator
*/
class OperatorSpacingSniff extends AbstractSpacingSniff
{
/**
* @param Token $token
*
* @return bool
*/
protected function shouldHaveSpaceBefore(Token $token)
{
return $this->isTokenMatching($token, Token::OPERATOR_TYPE);
}

/**
* @param Token $token
*
* @return bool
*/
protected function shouldHaveSpaceAfter(Token $token)
{
return $this->isTokenMatching($token, Token::OPERATOR_TYPE);
}
}
2 changes: 1 addition & 1 deletion TwigCS/Runner/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function rollbackChangeset()
$this->inChangeset = false;
$this->inConflict = false;

if (empty($this->changeset) === false) {
if (count($this->changeset) > 0) {
$this->changeset = [];
}
}
Expand Down
8 changes: 4 additions & 4 deletions TwigCS/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function processTemplate(string $file, Ruleset $ruleset, Report $report)
$this->env->parse($this->env->tokenize($twigSource));
} catch (Error $e) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_ERROR,
Report::MESSAGE_TYPE_FATAL,
$e->getRawMessage(),
$e->getSourceContext()->getName(),
$e->getTemplateLine()
Expand All @@ -142,8 +142,8 @@ public function processTemplate(string $file, Ruleset $ruleset, Report $report)
$stream = $this->tokenizer->tokenize($twigSource);
} catch (Exception $e) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_ERROR,
sprintf('Unable to tokenize file'),
Report::MESSAGE_TYPE_FATAL,
sprintf('Unable to tokenize file: %s', $e->getMessage()),
$file
);

Expand All @@ -170,7 +170,7 @@ protected function setErrorHandler(Report $report, string $file = null)
set_error_handler(function ($type, $message) use ($report, $file) {
if (E_USER_DEPRECATED === $type) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_NOTICE,
Report::MESSAGE_TYPE_NOTICE,
$message,
$file
);
Expand Down
Loading