Skip to content

Commit 8e1e523

Browse files
committed
Squiz FunctionSpacingSniff now has a setting to specify how many lines there should between functions (request #19843)
1 parent 9ed3794 commit 8e1e523

File tree

4 files changed

+97
-9
lines changed

4 files changed

+97
-9
lines changed

CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
class Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff implements PHP_CodeSniffer_Sniff
3131
{
3232

33+
/**
34+
* The limit that the length of a line should not exceed.
35+
*
36+
* @var int
37+
*/
38+
public $spacing = 2;
39+
3340

3441
/**
3542
* Returns an array of tokens this test wants to listen for.
@@ -54,7 +61,8 @@ public function register()
5461
*/
5562
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5663
{
57-
$tokens = $phpcsFile->getTokens();
64+
$tokens = $phpcsFile->getTokens();
65+
$this->spacing = (int) $this->spacing;
5866

5967
/*
6068
Check the number of blank lines
@@ -68,7 +76,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6876
$closer = $tokens[$stackPtr]['scope_closer'];
6977
}
7078

71-
// There needs to be 2 blank lines after the closer.
7279
$nextLineToken = null;
7380
for ($i = $closer; $i < $phpcsFile->numTokens; $i++) {
7481
if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
@@ -93,9 +100,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
93100
}
94101
}
95102

96-
if ($foundLines !== 2) {
97-
$error = 'Expected 2 blank lines after function; %s found';
98-
$data = array($foundLines);
103+
if ($foundLines !== $this->spacing) {
104+
$error = 'Expected %s blank line';
105+
if ($this->spacing !== 1) {
106+
$error .= 's';
107+
}
108+
109+
$error .= ' after function; %s found';
110+
$data = array(
111+
$this->spacing,
112+
$foundLines
113+
);
99114
$phpcsFile->addError($error, $closer, 'After', $data);
100115
}
101116

@@ -156,9 +171,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
156171
}//end while
157172
}//end if
158173

159-
if ($foundLines !== 2) {
160-
$error = 'Expected 2 blank lines before function; %s found';
161-
$data = array($foundLines);
174+
if ($foundLines !== $this->spacing) {
175+
$error = 'Expected %s blank line';
176+
if ($this->spacing !== 1) {
177+
$error .= 's';
178+
}
179+
180+
$error .= ' before function; %s found';
181+
$data = array(
182+
$this->spacing,
183+
$foundLines
184+
);
162185
$phpcsFile->addError($error, $stackPtr, 'Before', $data);
163186
}
164187

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.inc

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,57 @@ class MyClass
185185

186186
}//end class
187187

188+
// @codingStandardsChangeSetting Squiz.WhiteSpace.FunctionSpacing spacing 1
189+
190+
interface MyInterface
191+
{
192+
193+
/**
194+
* Function comment.
195+
*
196+
* @return boolean
197+
*/
198+
function func1();
199+
200+
201+
/**
202+
* Function comment.
203+
*
204+
* @return boolean
205+
*/
206+
function func2();
207+
208+
/**
209+
* Function comment.
210+
*
211+
* @return boolean
212+
*/
213+
function func3();
214+
215+
216+
}//end interface
217+
218+
class MyClass
219+
{
220+
221+
protected $tag = '';
222+
223+
protected $tag = '';
224+
225+
/**
226+
* Function comment.
227+
*
228+
* @return boolean
229+
*/
230+
function func1() {
231+
232+
}//end func1()
233+
234+
235+
}//end class
236+
237+
// @codingStandardsChangeSetting Squiz.WhiteSpace.FunctionSpacing spacing 2
238+
188239
// Closures should be ignored.
189240
preg_replace_callback(
190241
'~-([a-z])~',
@@ -198,4 +249,4 @@ $callback = function ($bar) use ($foo)
198249
{
199250
$bar += $foo;
200251
};
201-
?>
252+
?>

CodeSniffer/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function getErrorList()
6060
134 => 1,
6161
147 => 2,
6262
164 => 1,
63+
198 => 1,
64+
213 => 1,
65+
232 => 1,
6366
);
6467

6568
}//end getErrorList()

package.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
2828
<notes>
2929
- Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are defined in lowercase
3030
-- The PSR2 and Squiz standards now use this sniff
31+
- Added Generic SAPIUsageSniff to ensure the PHP_SAPI constant is used instead of php_sapi_name() (request #19863)
32+
- Squiz FunctionSpacingSniff now has a setting to specify how many lines there should between functions (request #19843)
33+
-- Default remains at 2
34+
-- Override the "spacing" setting in a ruleset.xml file to change
3135
- Squiz LowercasePHPFunctionSniff no longer throws errors for the limited set of PHP keywords it was checking
3236
-- Add a rule for Generic.PHP.LowerCaseKeyword to your ruleset to replicate this functionality
3337
- Added support for the PHP 5.4 T_CALLABLE token so it can be used in lower PHP versions
@@ -421,6 +425,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
421425
<file baseinstalldir="PHP" name="NoSilencedErrorsSniff.php" role="php">
422426
<tasks:replace from="@package_version@" to="version" type="package-info" />
423427
</file>
428+
<file baseinstalldir="PHP" name="SAPIUsageSniff.php" role="php">
429+
<tasks:replace from="@package_version@" to="version" type="package-info" />
430+
</file>
424431
<file baseinstalldir="PHP" name="UpperCaseConstantSniff.php" role="php">
425432
<tasks:replace from="@package_version@" to="version" type="package-info" />
426433
</file>
@@ -652,6 +659,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
652659
<file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.php" role="test">
653660
<tasks:replace from="@package_version@" to="version" type="package-info" />
654661
</file>
662+
<file baseinstalldir="PHP" name="SAPIUsageUnitTest.inc" role="test" />
663+
<file baseinstalldir="PHP" name="SAPIUsageUnitTest.php" role="test">
664+
<tasks:replace from="@package_version@" to="version" type="package-info" />
665+
</file>
655666
<file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.inc" role="test" />
656667
<file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.php" role="test">
657668
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 commit comments

Comments
 (0)