Permalink
Browse files

Squiz FunctionSpacingSniff now has a setting to specify how many line…

…s there should between functions (request #19843)
  • Loading branch information...
1 parent 9ed3794 commit 8e1e523592a180c63fe4c8f4704f6ca5d787d525 @gsherwood gsherwood committed Mar 25, 2013
@@ -30,6 +30,13 @@
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.
@@ -54,7 +61,8 @@ public function register()
*/
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
@@ -68,7 +76,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$closer = $tokens[$stackPtr]['scope_closer'];
}
- // There needs to be 2 blank lines after the closer.
$nextLineToken = null;
for ($i = $closer; $i < $phpcsFile->numTokens; $i++) {
if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
@@ -93,9 +100,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}
- if ($foundLines !== 2) {
- $error = 'Expected 2 blank lines after function; %s found';
- $data = array($foundLines);
+ if ($foundLines !== $this->spacing) {
+ $error = 'Expected %s blank line';
+ if ($this->spacing !== 1) {
+ $error .= 's';
+ }
+
+ $error .= ' after function; %s found';
+ $data = array(
+ $this->spacing,
+ $foundLines
+ );
$phpcsFile->addError($error, $closer, 'After', $data);
}
@@ -156,9 +171,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}//end while
}//end if
- if ($foundLines !== 2) {
- $error = 'Expected 2 blank lines before function; %s found';
- $data = array($foundLines);
+ if ($foundLines !== $this->spacing) {
+ $error = 'Expected %s blank line';
+ if ($this->spacing !== 1) {
+ $error .= 's';
+ }
+
+ $error .= ' before function; %s found';
+ $data = array(
+ $this->spacing,
+ $foundLines
+ );
$phpcsFile->addError($error, $stackPtr, 'Before', $data);
}
@@ -185,6 +185,57 @@ class MyClass
}//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.
preg_replace_callback(
'~-([a-z])~',
@@ -198,4 +249,4 @@ $callback = function ($bar) use ($foo)
{
$bar += $foo;
};
-?>
+?>
@@ -60,6 +60,9 @@ public function getErrorList()
134 => 1,
147 => 2,
164 => 1,
+ 198 => 1,
+ 213 => 1,
+ 232 => 1,
);
}//end getErrorList()
View
@@ -28,6 +28,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<notes>
- Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are defined in lowercase
-- 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
-- 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
@@ -421,6 +425,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="NoSilencedErrorsSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</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">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
@@ -652,6 +659,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</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.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 comments on commit 8e1e523

Please sign in to comment.