Skip to content

Commit f091519

Browse files
committed
A bunch of new rules to deal with nested class definitions. It boils down to: 1 blank line required after open brace of a nesting definition and 1 blank line before the closing brace. Inside a nesting area, all other rules apply as normal.
1 parent 440d99d commit f091519

11 files changed

+104
-9
lines changed

CodeSniffer/Standards/Squiz/Sniffs/CSS/ClassDefinitionClosingBraceSpaceSniff.php

+12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7575
}
7676
}
7777

78+
// Ignore nested style definitions from here on. The spacing before the closing brace
79+
// (a single blank line) will be enforced by the above check, which ensures there is a
80+
// blank line after the last nested class.
81+
$found = $phpcsFile->findPrevious(
82+
T_CLOSE_CURLY_BRACKET,
83+
($stackPtr - 1),
84+
$tokens[$stackPtr]['bracket_opener']
85+
);
86+
if ($found !== false) {
87+
return;
88+
}
89+
7890
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
7991
if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
8092
$num = ($tokens[$stackPtr]['line'] - $tokens[$prev]['line'] - 1);

CodeSniffer/Standards/Squiz/Sniffs/CSS/ClassDefinitionNameSpacingSniff.php

+7
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6161
{
6262
$tokens = $phpcsFile->getTokens();
6363

64+
// Do not check nested style definitions as, for example, in @media style rules.
65+
$nested = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
66+
if ($nested !== false) {
67+
return;
68+
}
69+
6470
// Find the first blank line before this openning brace, unless we get
6571
// to another style definition, comment or the start of the file.
6672
$endTokens = array(
73+
T_OPEN_CURLY_BRACKET,
6774
T_CLOSE_CURLY_BRACKET,
6875
T_COMMENT,
6976
T_DOC_COMMENT,

CodeSniffer/Standards/Squiz/Sniffs/CSS/ClassDefinitionOpeningBraceSpaceSniff.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,34 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
8080
}//end if
8181

8282
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
83-
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$stackPtr]['line'] + 1)) {
84-
$num = ($tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1);
85-
$error = 'Expected 0 blank lines after opening brace of class definition; %s found';
86-
$data = array($num);
87-
$phpcsFile->addError($error, $stackPtr, 'After', $data);
83+
if ($next === false) {
84+
return;
85+
}
86+
87+
// Check for nested class definitions.
88+
$nested = false;
89+
$found = $phpcsFile->findNext(
90+
T_OPEN_CURLY_BRACKET,
91+
($stackPtr + 1),
92+
$tokens[$stackPtr]['bracket_closer']
93+
);
94+
if ($found !== false) {
95+
$nested = true;
96+
}
97+
98+
$foundLines = ($tokens[$next]['line'] - $tokens[$stackPtr]['line'] - 1);
99+
if ($nested === true) {
100+
if ($foundLines !== 1) {
101+
$error = 'Expected 1 blank line after opening brace of nesting class definition; %s found';
102+
$data = array($foundLines);
103+
$phpcsFile->addError($error, $stackPtr, 'AfterNesting', $data);
104+
}
105+
} else {
106+
if ($foundLines !== 0) {
107+
$error = 'Expected 0 blank lines after opening brace of class definition; %s found';
108+
$data = array($foundLines);
109+
$phpcsFile->addError($error, $stackPtr, 'After', $data);
110+
}
88111
}
89112

90113
}//end process()

CodeSniffer/Standards/Squiz/Sniffs/CSS/IndentationSniff.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6464
$numTokens = (count($tokens) - 2);
6565
$currentLine = 0;
6666
$indentLevel = 0;
67+
$nested = false;
6768
for ($i = 1; $i < $numTokens; $i++) {
6869
if ($tokens[$i]['code'] === T_COMMENT) {
6970
// Dont check the indent of comments.
@@ -72,6 +73,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7273

7374
if ($tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) {
7475
$indentLevel++;
76+
77+
// Check for nested style definitions as, for example, in @media style rules.
78+
$found = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($i + 1), $tokens[$i]['bracket_closer']);
79+
if ($found === false) {
80+
$nested = false;
81+
} else {
82+
$nested = true;
83+
}
7584
} else if ($tokens[($i + 1)]['code'] === T_CLOSE_CURLY_BRACKET) {
7685
$indentLevel--;
7786
}
@@ -90,8 +99,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9099

91100
$expectedIndent = ($indentLevel * 4);
92101
if ($expectedIndent > 0 && strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
93-
$error = 'Blank lines are not allowed in class definitions';
94-
$phpcsFile->addError($error, $i, 'BlankLine');
102+
if ($nested === false) {
103+
$error = 'Blank lines are not allowed in class definitions';
104+
$phpcsFile->addError($error, $i, 'BlankLine');
105+
}
95106
} else if ($foundIndent !== $expectedIndent) {
96107
$error = 'Line indented incorrectly; expected %s spaces, found %s';
97108
$data = array(

CodeSniffer/Standards/Squiz/Tests/CSS/ClassDefinitionClosingBraceSpaceUnitTest.css

+15
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@
2828

2929
.AssetLineageWidgetType-item.selected {
3030
}
31+
32+
@media screen and (max-device-width: 769px) {
33+
34+
header #logo img {
35+
max-width: 100%;
36+
}
37+
38+
}
39+
40+
@media screen and (max-device-width: 769px) {
41+
42+
header #logo img {
43+
max-width: 100%;
44+
}
45+
}

CodeSniffer/Standards/Squiz/Tests/CSS/ClassDefinitionClosingBraceSpaceUnitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function getErrorList()
4444
2 => 1,
4545
11 => 1,
4646
19 => 1,
47+
44 => 1,
4748
);
4849

4950
}//end getErrorList()

CodeSniffer/Standards/Squiz/Tests/CSS/ClassDefinitionNameSpacingUnitTest.css

+9
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@ td.TableWidgetType-header.TableWidgetType-header-userName {
4646
padding-left: 12px;
4747
width: 150px;
4848
}
49+
50+
@media screen and (max-device-width: 769px) {
51+
52+
header #logo img {
53+
max-width: 100%;
54+
padding: 20px;
55+
margin: 40px;
56+
}
57+
}

CodeSniffer/Standards/Squiz/Tests/CSS/ClassDefinitionOpeningBraceSpaceUnitTest.css

+15
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@
1414

1515
float: left;
1616
}
17+
18+
@media screen and (max-device-width: 769px) {
19+
20+
header #logo img {
21+
max-width: 100%;
22+
}
23+
24+
}
25+
26+
@media screen and (max-device-width: 769px) {
27+
header #logo img {
28+
max-width: 100%;
29+
}
30+
31+
}

CodeSniffer/Standards/Squiz/Tests/CSS/ClassDefinitionOpeningBraceSpaceUnitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getErrorList()
4545
7 => 1,
4646
10 => 1,
4747
13 => 1,
48+
26 => 1,
4849
);
4950

5051
}//end getErrorList()

CodeSniffer/Standards/Squiz/Tests/CSS/IndentationUnitTest.css

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ td {
2323
*/
2424

2525
@media screen and (max-device-width: 769px) {
26+
2627
header #logo img {
2728
max-width: 100%;
2829
padding: 20px;

CodeSniffer/Standards/Squiz/Tests/CSS/IndentationUnitTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function getErrorList()
4646
6 => 1,
4747
7 => 1,
4848
12 => 1,
49-
27 => 1,
50-
29 => 1,
49+
28 => 1,
50+
30 => 1,
5151
);
5252

5353
}//end getErrorList()

0 commit comments

Comments
 (0)