cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods OpeningMethodBraceBsdAllmanSniff.php
OpeningMethodBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods OpeningMethodBraceBsdAllmanUnitTest.inc
OpeningMethodBraceBsdAllmanUnitTest.php OpeningMethodBraceKernighanRitchieUnitTest.inc OpeningMethodBraceKernighanRitchieUnitTest.php
| From: | Greg Sherwood | Date: | Sun, 01 Oct 2006 01:47:52 +0000 |
| Subject: | cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods OpeningMethodBraceBsdAllmanSniff.php OpeningMethodBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods OpeningMethodBraceBsdAllmanUnitTest.inc OpeningMethodBraceBsdAllmanUnitTest.php OpeningMethodBraceKernighanRitchieUnitTest.inc OpeningMethodBraceKernighanRitchieUnitTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-42327@lists.php.net to get a copy of this message | ||
squiz Sun Oct 1 01:47:52 2006 UTC
Added files:
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods
OpeningMethodBraceBsdAllmanSniff.php
OpeningMethodBraceKernighanRitchieSniff.php
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods
OpeningMethodBraceBsdAllmanUnitTest.inc
OpeningMethodBraceBsdAllmanUnitTest.php
OpeningMethodBraceKernighanRitchieUnitTest.inc
OpeningMethodBraceKernighanRitchieUnitTest.php
Log:
New generic sniffs for checking the opening braces of functions and method. Two types of checks so
far; BSDAllman (brace on next line) and KernighanRitchie (brace on same line).
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff. * * Checks that the opening brace of a function is on the line after the * function declaration. * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ class Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff { /** * Registers the tokens that this sniff wants to listen for. * * @return void */ public function register() { return array(T_FUNCTION); }//end register() /** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (!isset($tokens[$stackPtr]['scope_opener'])) { return; } $openingBrace = $tokens[$stackPtr]['scope_opener']; // The end of the function occurs at the end of the argument list. Its // like this because some people like to break long function declarations // over multiple lines. $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; $braceLine = $tokens[$openingBrace]['line']; $lineDifference = $braceLine - $functionLine; if ($lineDifference === 0) { $error = 'Opening function brace should be on a new line.'; $phpcsFile->addError($error, $openingBrace); return; } if ($lineDifference > 1) { $ender = (($lineDifference - 1) === 1) ? 'line' : 'lines'; $error = 'Opening function brace should be on the line after the declaration. Found '.($lineDifference - 1).' blank '.$ender.'.'; $phpcsFile->addError($error, $openingBrace); return; } // We need to actually find the first piece of content on this line, // as if this is a method with tokens before it (public, static etc) // or an if with an else before it, then we need to start the scope // checking from there, rather than the current token. $lineStart = $stackPtr; while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) { if (strpos($tokens[$lineStart]['content'], "\n") !== false) { break; } } // We found a new line, now go forward and find the first non-whitespace // token. $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true); // The opening brace is on the correct line, now it needs to be // checked to be correctly indented. $startColumn = $tokens[$lineStart]['column']; $braceIndent = $tokens[$openingBrace]['column']; if ($braceIndent !== $startColumn) { $error = 'Opening Brace Indented Incorrectly. Expected '.($startColumn - 1).' spaces, but found '.($braceIndent - 1).'.'; $phpcsFile->addError($error, $openingBrace); } }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Methods_OpeningMethodBraceKernighanRitchieSniff. * * Checks that the opening brace of a function is on the same line * as the function declaration. * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ class Generic_Sniffs_Methods_OpeningMethodBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff { /** * Registers the tokens that this sniff wants to listen for. * * @return void */ public function register() { return array(T_FUNCTION); }//end register() /** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (!isset($tokens[$stackPtr]['scope_opener'])) { return; } $openingBrace = $tokens[$stackPtr]['scope_opener']; // The end of the function occurs at the end of the argument list. Its // like this because some people like to break long function declarations // over multiple lines. $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; $braceLine = $tokens[$openingBrace]['line']; $lineDifference = $braceLine - $functionLine; if ($lineDifference > 0) { $error = 'Opening function brace should be on the same line as the declaration.'; $phpcsFile->addError($error, $openingBrace); return; } // Checks that the closing parenthesis and the opening brace are // separated by a whitespace character. $closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column']; $braceColumn = $tokens[$openingBrace]['column']; $columnDifference = $braceColumn - $closerColumn; if ($columnDifference != 2) { $error = 'Expected 1 space between the closing parenthesis and the opening brace, but found '.($columnDifference - 1).'.'; $phpcsFile->addError($error, $openingBrace); return; } // Check that a tab was not used instead of a space. $spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1); $spaceContent = $tokens[$spaceTokenPtr]['content']; if ($spaceContent !== ' ') { $error = 'Expected a single space character between the closing parenthesis and the opening brace, but found "'.$spaceContent.'".'; $phpcsFile->addError($error, $openingBrace); return; } }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc <?php // Brace should be on new line. function myFunction() { } // Good. function myFunction() { } // Too many spaces. function myFunction() { } // Too many newlines. function myFunction() { } // Space before brace. function myFunction() { } class myClass() { // Brace should be on new line. function myFunction() { } // Good. function myFunction() { } // No aligned correctly. function myFunction() { } // Too many spaces. function myFunction() { } // Too many newlines. function myFunction() { } // Space before brace. function myFunction() { } } /* Multi-line declarations */ // Brace should be on new line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many newlines. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Space before brace. function myFunction($variable1, $variable2, $variable3, $variable4) { } class myClass() { // Brace should be on new line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // No aligned correctly. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many newlines. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Space before brace. function myFunction($variable1, $variable2, $variable3, $variable4) { } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ /** * Unit test class for the OpeningMethodBraceBsdAllman sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ class Generic_Tests_Methods_OpeningMethodBraceBsdAllmanUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 4 => 1, 13 => 1, 19 => 1, 24 => 1, 30 => 1, 40 => 1, 44 => 1, 50 => 1, 55 => 1, 67 => 1, 78 => 1, 85 => 1, 91 => 1, 98 => 1, 110 => 1, 115 => 1, 122 => 1, 128 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc <?php // Good. function myFunction() { } // Brace should be on same line. function myFunction() { } // Too many spaces. function myFunction() { } // Uses tab. function myFunction() { } class myClass() { // Good. function myFunction() { } // Brace should be on same line. function myFunction() { } // Too many spaces. function myFunction() { } // Uses tab. function myFunction() { } } /* Multi-line declarations */ // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { } class myClass() { // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ /** * Unit test class for the OpeningMethodBraceKernighanRitchie sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ class Generic_Tests_Methods_OpeningMethodBraceKernighanRitchieUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 9 => 1, 13 => 1, 17 => 1, 29 => 1, 33 => 1, 37 => 1, 53 => 1, 58 => 1, 63 => 1, 77 => 1, 82 => 1, 87 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?>
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceBsdAllmanSniff.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff. * * Checks that the opening brace of a function is on the line after the * function declaration. * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ class Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff { /** * Registers the tokens that this sniff wants to listen for. * * @return void */ public function register() { return array(T_FUNCTION); }//end register() /** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (!isset($tokens[$stackPtr]['scope_opener'])) { return; } $openingBrace = $tokens[$stackPtr]['scope_opener']; // The end of the function occurs at the end of the argument list. Its // like this because some people like to break long function declarations // over multiple lines. $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; $braceLine = $tokens[$openingBrace]['line']; $lineDifference = $braceLine - $functionLine; if ($lineDifference === 0) { $error = 'Opening function brace should be on a new line.'; $phpcsFile->addError($error, $openingBrace); return; } if ($lineDifference > 1) { $ender = (($lineDifference - 1) === 1) ? 'line' : 'lines'; $error = 'Opening function brace should be on the line after the declaration. Found '.($lineDifference - 1).' blank '.$ender.'.'; $phpcsFile->addError($error, $openingBrace); return; } // We need to actually find the first piece of content on this line, // as if this is a method with tokens before it (public, static etc) // or an if with an else before it, then we need to start the scope // checking from there, rather than the current token. $lineStart = $stackPtr; while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) { if (strpos($tokens[$lineStart]['content'], "\n") !== false) { break; } } // We found a new line, now go forward and find the first non-whitespace // token. $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true); // The opening brace is on the correct line, now it needs to be // checked to be correctly indented. $startColumn = $tokens[$lineStart]['column']; $braceIndent = $tokens[$openingBrace]['column']; if ($braceIndent !== $startColumn) { $error = 'Opening Brace Indented Incorrectly. Expected '.($startColumn - 1).' spaces, but found '.($braceIndent - 1).'.'; $phpcsFile->addError($error, $openingBrace); } }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods/OpeningMethodBraceKernighanRitchieSniff.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Methods_OpeningMethodBraceKernighanRitchieSniff. * * Checks that the opening brace of a function is on the same line * as the function declaration. * * @package PHP_CodeSniffer * @category Generic_Coding_Standards * @author Squiz Pty Ltd */ class Generic_Sniffs_Methods_OpeningMethodBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff { /** * Registers the tokens that this sniff wants to listen for. * * @return void */ public function register() { return array(T_FUNCTION); }//end register() /** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (!isset($tokens[$stackPtr]['scope_opener'])) { return; } $openingBrace = $tokens[$stackPtr]['scope_opener']; // The end of the function occurs at the end of the argument list. Its // like this because some people like to break long function declarations // over multiple lines. $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; $braceLine = $tokens[$openingBrace]['line']; $lineDifference = $braceLine - $functionLine; if ($lineDifference > 0) { $error = 'Opening function brace should be on the same line as the declaration.'; $phpcsFile->addError($error, $openingBrace); return; } // Checks that the closing parenthesis and the opening brace are // separated by a whitespace character. $closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column']; $braceColumn = $tokens[$openingBrace]['column']; $columnDifference = $braceColumn - $closerColumn; if ($columnDifference != 2) { $error = 'Expected 1 space between the closing parenthesis and the opening brace, but found '.($columnDifference - 1).'.'; $phpcsFile->addError($error, $openingBrace); return; } // Check that a tab was not used instead of a space. $spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1); $spaceContent = $tokens[$spaceTokenPtr]['content']; if ($spaceContent !== ' ') { $error = 'Expected a single space character between the closing parenthesis and the opening brace, but found "'.$spaceContent.'".'; $phpcsFile->addError($error, $openingBrace); return; } }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.inc <?php // Brace should be on new line. function myFunction() { } // Good. function myFunction() { } // Too many spaces. function myFunction() { } // Too many newlines. function myFunction() { } // Space before brace. function myFunction() { } class myClass() { // Brace should be on new line. function myFunction() { } // Good. function myFunction() { } // No aligned correctly. function myFunction() { } // Too many spaces. function myFunction() { } // Too many newlines. function myFunction() { } // Space before brace. function myFunction() { } } /* Multi-line declarations */ // Brace should be on new line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many newlines. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Space before brace. function myFunction($variable1, $variable2, $variable3, $variable4) { } class myClass() { // Brace should be on new line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // No aligned correctly. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many newlines. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Space before brace. function myFunction($variable1, $variable2, $variable3, $variable4) { } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceBsdAllmanUnitTest.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ /** * Unit test class for the OpeningMethodBraceBsdAllman sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ class Generic_Tests_Methods_OpeningMethodBraceBsdAllmanUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 4 => 1, 13 => 1, 19 => 1, 24 => 1, 30 => 1, 40 => 1, 44 => 1, 50 => 1, 55 => 1, 67 => 1, 78 => 1, 85 => 1, 91 => 1, 98 => 1, 110 => 1, 115 => 1, 122 => 1, 128 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.inc <?php // Good. function myFunction() { } // Brace should be on same line. function myFunction() { } // Too many spaces. function myFunction() { } // Uses tab. function myFunction() { } class myClass() { // Good. function myFunction() { } // Brace should be on same line. function myFunction() { } // Too many spaces. function myFunction() { } // Uses tab. function myFunction() { } } /* Multi-line declarations */ // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { } class myClass() { // Good. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods/OpeningMethodBraceKernighanRitchieUnitTest.php <?php /** * +------------------------------------------------------------------------+ * | BSD Licence | * +------------------------------------------------------------------------+ * | This software is available to you under the BSD license, | * | available in the LICENSE file accompanying this software. | * | You may obtain a copy of the License at | * | | * | http://matrix.squiz.net/developer/tools/php_cs/licence | * | | * | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | * | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | * | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | * | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * +------------------------------------------------------------------------+ * | Copyright (c), 2006 Squiz Pty Ltd (ABN 77 084 670 600). | * | All rights reserved. | * +------------------------------------------------------------------------+ * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ /** * Unit test class for the OpeningMethodBraceKernighanRitchie sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @package PHP_CodeSniffer * @author Squiz Pty Ltd */ class Generic_Tests_Methods_OpeningMethodBraceKernighanRitchieUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 9 => 1, 13 => 1, 17 => 1, 29 => 1, 33 => 1, 37 => 1, 53 => 1, 58 => 1, 63 => 1, 77 => 1, 82 => 1, 87 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?>