Skip to content

Commit fbb04ca

Browse files
committed
Squiz/EmptyStyleDefinition: report error when the value is a comment
The `Squiz.CSS.EmptyStyleDefinition` sniff checks for CSS styles being defined without value. This sniff would fail to report an issue if there is no value, but a comment is encountered instead. Includes unit tests.
1 parent 935c8b5 commit fbb04ca

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Standards/Squiz/Sniffs/CSS/EmptyStyleDefinitionSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Sniffs\Sniff;
1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
class EmptyStyleDefinitionSniff implements Sniff
1617
{
@@ -47,8 +48,11 @@ public function register()
4748
public function process(File $phpcsFile, $stackPtr)
4849
{
4950
$tokens = $phpcsFile->getTokens();
50-
$next = $phpcsFile->findNext([T_WHITESPACE, T_COLON], ($stackPtr + 1), null, true);
5151

52+
$ignore = Tokens::$emptyTokens;
53+
$ignore[] = T_COLON;
54+
55+
$next = $phpcsFile->findNext($ignore, ($stackPtr + 1), null, true);
5256
if ($next === false || $tokens[$next]['code'] === T_SEMICOLON || $tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
5357
$error = 'Style definition is empty';
5458
$phpcsFile->addError($error, $stackPtr, 'Found');

src/Standards/Squiz/Tests/CSS/EmptyStyleDefinitionUnitTest.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
margin-right:
44
float: ;
55
}
6+
7+
#MetadataAdminScreen-addField-fieldType li {
8+
margin-right: /* @todo */
9+
margin-left: 10px;
10+
float: /* Some comment. */ ;
11+
}

src/Standards/Squiz/Tests/CSS/EmptyStyleDefinitionUnitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class EmptyStyleDefinitionUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
3 => 1,
30-
4 => 1,
29+
3 => 1,
30+
4 => 1,
31+
8 => 1,
32+
10 => 1,
3133
];
3234

3335
}//end getErrorList()

0 commit comments

Comments
 (0)