Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

Commit

Permalink
Fixed issue where a false positive occured for colors (#32)
Browse files Browse the repository at this point in the history
* Fixed issue where a false positive occured for colors

* Codestyle

* Line length
  • Loading branch information
yannickl88 authored and nicoschoenmaker committed Feb 26, 2018
1 parent 5463fac commit 1300c56
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
Binary file added css-sniffer.phar
Binary file not shown.
Binary file added phar-composer.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Output/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function format(array $files): string
);
$out .= "--------------------------------------------------------------------------------\n";

$size = array_reduce($violations, function (int $total, Violation $v) {
$size = array_reduce($violations, function (int $total, Violation $v) {
return max($total, strlen((string) $v->getLine()));
}, 0);
$message_size = 80 - ($size + 4);
Expand Down
21 changes: 19 additions & 2 deletions src/Sniff/ColorSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ public function process(File $file, int $stack_ptr): void
$token = $file->get($stack_ptr);

if (in_array($token->chars, self::CSS_RULES, true) || 1 === preg_match('/-color$/', $token->chars)) {
$t = $file->findNext(Token::T_WORD, $stack_ptr + 1);
$end_of_line = $file->findNext(Token::T_SEMICOLON, $stack_ptr + 1);
$t = $file->findNext(Token::T_WORD, $stack_ptr + 1);

if (null !== $t && $t->chars[0] === '#' && 1 !== preg_match('/^#[0-9a-f]{6}$/', $t->chars)) {
if ($this->isBefore($t, $end_of_line)
&& $t->chars[0] === '#'
&& 1 !== preg_match('/^#[0-9a-f]{6}$/', $t->chars)
) {
$file->addViolation(
self::class,
'Colors should always be 6 characters hex values.',
Expand All @@ -48,4 +52,17 @@ public function process(File $file, int $stack_ptr): void
}
}
}

private function isBefore(?Token $token, ?Token $ref): bool
{
if (null === $token) {
return false;
}

if (null === $ref) {
return true;
}

return $token->lines[0] <= $ref->lines[0] && $token->offsets[0] <= max($ref->offsets);
}
}
2 changes: 1 addition & 1 deletion test/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FileTest extends TestCase
{
public function testGeneric()
{
$file = new File('phpunit', [
$file = new File('phpunit', [
$token_1 = new Token(Token::T_WORD, 'foobar', 1, 0),
$token_2 = new Token(Token::T_WHITESPACE, ' ', 1, 6),
$token_3 = new Token(Token::T_WORD, 'barbaz', 1, 7),
Expand Down
1 change: 1 addition & 0 deletions test/Sniff/ColorSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testSniffVariants()
new Violation(ColorSniff::class, 'Colors should always be 6 characters hex values.', 13, 17, 21),
new Violation(ColorSniff::class, 'Colors should always be 6 characters hex values.', 14, 23, 27),
new Violation(ColorSniff::class, 'Colors should always be 6 characters hex values.', 15, 17, 21),
new Violation(ColorSniff::class, 'Colors should always be 6 characters hex values.', 36, 26, 30),
], $file->getViolations());
}
}
4 changes: 4 additions & 0 deletions test/Sniff/fixtures/color_variants.less
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ good {
border-top-color: #ffffff;
box-shadow: #ffffff;
}

#foo { background: #ffffff; }
#bar { background-color: #ffffff; }
#baz { background-color: #fff; }

0 comments on commit 1300c56

Please sign in to comment.