Skip to content

Commit

Permalink
Squiz FunctionSpacingSniff now has a setting to specify how many line…
Browse files Browse the repository at this point in the history
…s there should between functions (request #19843)
  • Loading branch information
gsherwood committed Mar 25, 2013
1 parent 9ed3794 commit 8e1e523
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
class Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff implements PHP_CodeSniffer_Sniff class Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff implements PHP_CodeSniffer_Sniff
{ {


/**
* The limit that the length of a line should not exceed.
*
* @var int
*/
public $spacing = 2;



/** /**
* Returns an array of tokens this test wants to listen for. * Returns an array of tokens this test wants to listen for.
Expand All @@ -54,7 +61,8 @@ public function register()
*/ */
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{ {
$tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;


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


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


if ($foundLines !== 2) { if ($foundLines !== $this->spacing) {
$error = 'Expected 2 blank lines after function; %s found'; $error = 'Expected %s blank line';
$data = array($foundLines); if ($this->spacing !== 1) {
$error .= 's';
}

$error .= ' after function; %s found';
$data = array(
$this->spacing,
$foundLines
);
$phpcsFile->addError($error, $closer, 'After', $data); $phpcsFile->addError($error, $closer, 'After', $data);
} }


Expand Down Expand Up @@ -156,9 +171,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}//end while }//end while
}//end if }//end if


if ($foundLines !== 2) { if ($foundLines !== $this->spacing) {
$error = 'Expected 2 blank lines before function; %s found'; $error = 'Expected %s blank line';
$data = array($foundLines); if ($this->spacing !== 1) {
$error .= 's';
}

$error .= ' before function; %s found';
$data = array(
$this->spacing,
$foundLines
);
$phpcsFile->addError($error, $stackPtr, 'Before', $data); $phpcsFile->addError($error, $stackPtr, 'Before', $data);
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -185,6 +185,57 @@ class MyClass


}//end class }//end class


// @codingStandardsChangeSetting Squiz.WhiteSpace.FunctionSpacing spacing 1

interface MyInterface
{

/**
* Function comment.
*
* @return boolean
*/
function func1();


/**
* Function comment.
*
* @return boolean
*/
function func2();

/**
* Function comment.
*
* @return boolean
*/
function func3();


}//end interface

class MyClass
{

protected $tag = '';

protected $tag = '';

/**
* Function comment.
*
* @return boolean
*/
function func1() {

}//end func1()


}//end class

// @codingStandardsChangeSetting Squiz.WhiteSpace.FunctionSpacing spacing 2

// Closures should be ignored. // Closures should be ignored.
preg_replace_callback( preg_replace_callback(
'~-([a-z])~', '~-([a-z])~',
Expand All @@ -198,4 +249,4 @@ $callback = function ($bar) use ($foo)
{ {
$bar += $foo; $bar += $foo;
}; };
?> ?>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function getErrorList()
134 => 1, 134 => 1,
147 => 2, 147 => 2,
164 => 1, 164 => 1,
198 => 1,
213 => 1,
232 => 1,
); );


}//end getErrorList() }//end getErrorList()
Expand Down
11 changes: 11 additions & 0 deletions package.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<notes> <notes>
- Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are defined in lowercase - Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are defined in lowercase
-- The PSR2 and Squiz standards now use this sniff -- The PSR2 and Squiz standards now use this sniff
- Added Generic SAPIUsageSniff to ensure the PHP_SAPI constant is used instead of php_sapi_name() (request #19863)
- Squiz FunctionSpacingSniff now has a setting to specify how many lines there should between functions (request #19843)
-- Default remains at 2
-- Override the "spacing" setting in a ruleset.xml file to change
- Squiz LowercasePHPFunctionSniff no longer throws errors for the limited set of PHP keywords it was checking - Squiz LowercasePHPFunctionSniff no longer throws errors for the limited set of PHP keywords it was checking
-- Add a rule for Generic.PHP.LowerCaseKeyword to your ruleset to replicate this functionality -- Add a rule for Generic.PHP.LowerCaseKeyword to your ruleset to replicate this functionality
- Added support for the PHP 5.4 T_CALLABLE token so it can be used in lower PHP versions - Added support for the PHP 5.4 T_CALLABLE token so it can be used in lower PHP versions
Expand Down Expand Up @@ -421,6 +425,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="NoSilencedErrorsSniff.php" role="php"> <file baseinstalldir="PHP" name="NoSilencedErrorsSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" /> <tasks:replace from="@package_version@" to="version" type="package-info" />
</file> </file>
<file baseinstalldir="PHP" name="SAPIUsageSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="PHP" name="UpperCaseConstantSniff.php" role="php"> <file baseinstalldir="PHP" name="UpperCaseConstantSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" /> <tasks:replace from="@package_version@" to="version" type="package-info" />
</file> </file>
Expand Down Expand Up @@ -652,6 +659,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.php" role="test"> <file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" /> <tasks:replace from="@package_version@" to="version" type="package-info" />
</file> </file>
<file baseinstalldir="PHP" name="SAPIUsageUnitTest.inc" role="test" />
<file baseinstalldir="PHP" name="SAPIUsageUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.inc" role="test" /> <file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.inc" role="test" />
<file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.php" role="test"> <file baseinstalldir="PHP" name="UpperCaseConstantUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" /> <tasks:replace from="@package_version@" to="version" type="package-info" />
Expand Down

0 comments on commit 8e1e523

Please sign in to comment.