Skip to content

Commit

Permalink
Merge pull request #15 from martin-helmich/bugfix/issue-13
Browse files Browse the repository at this point in the history
OperatorWhitespaceSniff complains about space after unary operators
  • Loading branch information
martin-helmich authored Dec 6, 2016
2 parents 5acf5f8 + 3b21758 commit 3247981
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,32 @@ trait TokenInspections
* @return bool
*/
private static function isOperator(TokenInterface $token)
{
return static::isUnaryOperator($token) || static::isBinaryOperator($token);
}

/**
* Tests whether a token is a unary operator
*
* @param TokenInterface $token
* @return bool
*/
private static function isUnaryOperator(TokenInterface $token)
{
return $token->getType() === TokenInterface::TYPE_OPERATOR_DELETE;
}

/**
* Tests whether a token is a binary operator
*
* @param TokenInterface $token
* @return bool
*/
private static function isBinaryOperator(TokenInterface $token)
{
return in_array($token->getType(), [
TokenInterface::TYPE_OPERATOR_ASSIGNMENT,
TokenInterface::TYPE_OPERATOR_COPY,
TokenInterface::TYPE_OPERATOR_DELETE,
TokenInterface::TYPE_OPERATOR_MODIFY,
TokenInterface::TYPE_OPERATOR_REFERENCE,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function sniff(array $tokens, File $file, LinterConfiguration $configurat
// Scan forward until we find the actual operator
for ($j = 0; $j < $count && !self::isOperator($tokensInLine[$j]); $j ++);

if (isset($tokensInLine[$j + 1])) {
if (isset($tokensInLine[$j + 1]) && self::isBinaryOperator($tokensInLine[$j])) {
if (!self::isWhitespace($tokensInLine[$j + 1])) {
$file->addWarning(new Warning(
$tokensInLine[$j]->getLine(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo >
bar = 1
25 changes: 15 additions & 10 deletions tests/functional/Helmich/TypoScriptLint/Tests/Linter/LinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,24 @@ public function testLinterCreatesExpectedOutput($typoscriptFile, array $expected
new NullOutput()
);

$printActualWarnings = function() use ($report) {
$actualWarnings = $report->getFiles()[0]->getWarnings();
$content = "";
foreach ($actualWarnings as $warning) {
$content .= $warning->getLine() . ";" . $warning->getColumn() . ";" . $warning->getMessage() . ";" .
$warning->getSeverity() . ";" . $warning->getSource() . "\n";
}
return $content;
};

if (count($expectedWarnings) === 0 && count($report->getFiles()) > 0) {
$this->fail($printActualWarnings());
}

$this->assertCount(count($expectedWarnings) > 0 ? 1 : 0, $report->getFiles());
if (count($expectedWarnings) > 0) {
$actualWarnings = $report->getFiles()[0]->getWarnings();
try {
$this->assertEquals($expectedWarnings, $actualWarnings);
} catch (\PHPUnit_Framework_AssertionFailedError $error) {
foreach ($actualWarnings as $warning) {
echo $warning->getLine() . ";" . $warning->getColumn() . ";" . $warning->getMessage() . ";" .
$warning->getSeverity() . ";" . $warning->getSource() . "\n";
}

throw $error;
}
$this->assertEquals($expectedWarnings, $actualWarnings, $printActualWarnings());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getValidTokenSequences()
yield [
[
new Token(TokenInterface::TYPE_OBJECT_IDENTIFIER, "foo", 1),
new Token(TokenInterface::TYPE_OPERATOR_DELETE, "<", 1),
new Token(TokenInterface::TYPE_OPERATOR_COPY, "<", 1),
new Token(TokenInterface::TYPE_WHITESPACE, " ", 1),
new Token(TokenInterface::TYPE_RIGHTVALUE, "bar", 1)
],
Expand Down

0 comments on commit 3247981

Please sign in to comment.