Skip to content

Commit 2af613f

Browse files
committed
Squiz.Classes.ClassDeclaration no longer enforces spacing rules when a class is followed by a function (ref #2033)
1 parent e39a44a commit 2af613f

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3838
- PSR2.Namespaces.UseDeclaration now supports commas at the end of group use declarations
3939
-- Also improves checking and fixing for use statements containing parse errors
4040
-- Thanks to Juliette Reinders Folmer for the patch
41+
- Squiz.Classes.ClassDeclaration no longer enforces spacing rules when a class is followed by a function
42+
-- Fixes a conflict between this sniff and the Squiz.WhiteSpace.FunctionSpacing sniff
4143
- Squiz.Commenting.InlineComment no longer enforces spacing rules when an inline comment is followed by a docblock
4244
-- Fixes a conflict between this sniff and the Squiz.WhiteSpace.FunctionSpacing sniff
4345
- Squiz.WhiteSpace.SuperfluousWhitespace now recognizes unicode whitespace at the start and end of a file

src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function processClose(File $phpcsFile, $stackPtr)
106106

107107
// Check that the closing brace has one blank line after it.
108108
for ($nextContent = ($closeBrace + 1); $nextContent < $phpcsFile->numTokens; $nextContent++) {
109-
// Ignore comments on the same lines as the brace.
109+
// Ignore comments on the same line as the brace.
110110
if ($tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
111111
&& ($tokens[$nextContent]['code'] === T_WHITESPACE
112112
|| $tokens[$nextContent]['code'] === T_COMMENT
@@ -166,6 +166,13 @@ public function processClose(File $phpcsFile, $stackPtr)
166166
}//end if
167167

168168
if ($difference !== -1 && $difference !== 1) {
169+
if ($tokens[$nextContent]['code'] === T_DOC_COMMENT_OPEN_TAG) {
170+
$next = $phpcsFile->findNext(T_WHITESPACE, ($tokens[$nextContent]['comment_closer'] + 1), null, true);
171+
if ($next !== false && $tokens[$next]['code'] === T_FUNCTION) {
172+
return;
173+
}
174+
}
175+
169176
$error = 'Closing brace of a %s must be followed by a single blank line; found %s';
170177
$data = [
171178
$tokens[$stackPtr]['content'],

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ interface MyInterface
9494

9595
<?php
9696

97+
class MyClass2
98+
{
99+
var $x;
100+
}
101+
102+
103+
/**
104+
* No error.
105+
*/
106+
function example() {}
107+
97108
class CorrectClassDeclaration
98109
{
99110

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ interface MyInterface
102102

103103
<?php
104104

105+
class MyClass2
106+
{
107+
var $x;
108+
}
109+
110+
111+
/**
112+
* No error.
113+
*/
114+
function example() {}
115+
105116
class CorrectClassDeclaration
106117
{
107118

src/Standards/Squiz/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ public function getErrorList()
5656
89 => 1,
5757
92 => 1,
5858
97 => 1,
59-
103 => 1,
60-
105 => 1,
61-
107 => 1,
62-
110 => 1,
59+
108 => 1,
60+
114 => 1,
61+
116 => 1,
62+
118 => 1,
63+
121 => 1,
6364
];
6465

6566
}//end getErrorList()

0 commit comments

Comments
 (0)