cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions OpeningFunctionBraceBsdAllmanSniff.php
OpeningFunctionBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods OpeningMethodBraceBsdAllmanSniff.php
OpeningMethodBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions OpeningFunctionBraceBsdAllmanUnitTest.inc
OpeningFunctionBraceBsdAllmanUnitTest.php OpeningFunctionBraceKernighanRitchieUnitTest.inc OpeningFunctionBraceKernighanRitchieUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods OpeningMethodBraceBsdAllmanUnitTest.inc OpeningMethodBraceBsdAllmanUnitTest.php
OpeningMethodBraceKernighanRitchieUnitTest.inc OpeningMethodBraceKernighanRitchieUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions FunctionCallArgumentSpacingSniff.php OpeningFunctionBraceSniff.php
ValidDefaultValueSniff.php /PHP_CodeSniffer/CodeSniffer/Standards! /PEAR/Sniffs/Methods MethodCallArgumentSpacingSniff.php
OpeningMethodBraceSniff.php ValidDefaultValueSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions
FunctionCallArgumentSpacingUnitTest.inc FunctionCallArgumentSpacingUnitTest.php ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Methods MethodCallArgumentSpacingUnitTest.inc MethodCallArgumentSpacingUnitTest.php
ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php

From: Date: Wed, 27 Dec 2006 23:19:18 +0000
Subject: cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions OpeningFunctionBraceBsdAllmanSniff.php
OpeningFunctionBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Methods OpeningMethodBraceBsdAllmanSniff.php
OpeningMethodBraceKernighanRitchieSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions OpeningFunctionBraceBsdAllmanUnitTest.inc
OpeningFunctionBraceBsdAllmanUnitTest.php OpeningFunctionBraceKernighanRitchieUnitTest.inc OpeningFunctionBraceKernighanRitchieUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Methods OpeningMethodBraceBsdAllmanUnitTest.inc OpeningMethodBraceBsdAllmanUnitTest.php
OpeningMethodBraceKernighanRitchieUnitTest.inc OpeningMethodBraceKernighanRitchieUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions FunctionCallArgumentSpacingSniff.php OpeningFunctionBraceSniff.php
ValidDefaultValueSniff.php /PHP_CodeSniffer/CodeSniffer/Standards! /PEAR/Sniffs/Methods MethodCallArgumentSpacingSniff.php
OpeningMethodBraceSniff.php ValidDefaultValueSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions
FunctionCallArgumentSpacingUnitTest.inc FunctionCallArgumentSpacingUnitTest.php ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php
/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Methods MethodCallArgumentSpacingUnitTest.inc MethodCallArgumentSpacingUnitTest.php
ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-43850@lists.php.net to get a copy of this message
squiz Wed Dec 27 23:19:18 2006 UTC Added files: /pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions OpeningFunctionBraceBsdAllmanSniff.php OpeningFunctionBraceKernighanRitchieSniff.php /pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions OpeningFunctionBraceBsdAllmanUnitTest.inc OpeningFunctionBraceBsdAllmanUnitTest.php OpeningFunctionBraceKernighanRitchieUnitTest.inc OpeningFunctionBraceKernighanRitchieUnitTest.php /pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions FunctionCallArgumentSpacingSniff.php OpeningFunctionBraceSniff.php ValidDefaultValueSniff.php /pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions FunctionCallArgumentSpacingUnitTest.inc FunctionCallArgumentSpacingUnitTest.php ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php Removed 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 /pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Methods MethodCallArgumentSpacingSniff.php OpeningMethodBraceSniff.php ValidDefaultValueSniff.php /pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Methods MethodCallArgumentSpacingUnitTest.inc MethodCallArgumentSpacingUnitTest.php ValidDefaultValueUnitTest.inc ValidDefaultValueUnitTest.php Log: Getting rid of the METHODS directory and replacing it with FUNCTIONS because these sniffs are generic to all functions, not just class methods.

http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php <?php /** * Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: OpeningFunctionBraceBsdAllmanSniff.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff. * * Checks that the opening brace of a function is on the line after the * function declaration. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff 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/Functions/OpeningFunctionBraceKernighanRitchieSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php <?php /** * Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: OpeningFunctionBraceKernighanRitchieSniff.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff. * * Checks that the opening brace of a function is on the same line * as the function declaration. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff 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/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.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/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php <?php /** * Unit test class for the OpeningFunctionBraceBsdAllman sniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: OpeningFunctionBraceBsdAllmanUnitTest.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the OpeningFunctionBraceBsdAllman 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. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Generic_Tests_Functions_OpeningFunctionBraceBsdAllmanUnitTest 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/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.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/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php <?php /** * Unit test class for the OpeningFunctionBraceKernighanRitchie sniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: OpeningFunctionBraceKernighanRitchieUnitTest.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the OpeningFunctionBraceKernighanRitchie 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. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Generic_Tests_Functions_OpeningFunctionBraceKernighanRitchieUnitTest 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/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php <?php /** * PEAR_Sniffs_Functions_FunctionCallArgumentSpacingSniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: FunctionCallArgumentSpacingSniff.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * PEAR_Sniffs_Functions_FunctionCallArgumentSpacingSniff. * * Checks that calls to methods and functions are spaced correctly. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class PEAR_Sniffs_Functions_FunctionCallArgumentSpacingSniff implements PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. * * @return array */ public function register() { return array(T_STRING); }//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(); // Skip tokens that are the names of functions or classes // within their definitions. For example: // function myFunction... // "myFunction" is T_STRING but we should skip because it is not a // function or method *call*. $functionName = $stackPtr; $functionKeyword = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) { return; } // If the next non-whitespace token after the function or method call // is not an opening parenthesis then it cant really be a *call*. $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($functionName + 1), null, true); if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { return; } $closeBracket = $tokens[$openBracket]['parenthesis_closer']; $nextSeperator = $openBracket; while (($nextSeperator = $phpcsFile->findNext(array(T_COMMA, T_VARIABLE), ($nextSeperator + 1), $closeBracket)) !== false) { if ($tokens[$nextSeperator]['code'] === T_COMMA) { if ($tokens[$nextSeperator - 1]['code'] === T_WHITESPACE) { $error = 'Space found before comma in function call.'; $phpcsFile->addError($error, $stackPtr); } if ($tokens[$nextSeperator + 1]['code'] !== T_WHITESPACE) { $error = 'No space found after comma in function call.'; $phpcsFile->addError($error, $stackPtr); } else { // If there is a newline in the space, then the must be formatting // each argument on a newline, which is valid, so ignore it. if (strpos($tokens[$nextSeperator + 1]['content'], "\n") === false) { $space = strlen($tokens[$nextSeperator + 1]['content']); if ($space > 1) { $error = 'Expected 1 space after comma in function call, '; $error .= $space.' found.'; $phpcsFile->addError($error, $stackPtr); } } } } else { // Token is a variable. $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextSeperator + 1, $closeBracket, true); if ($nextToken !== false) { if ($tokens[$nextToken]['code'] === T_EQUAL) { if (($tokens[$nextToken - 1]['code']) !== T_WHITESPACE) { $error = 'Expected one space before = sign of default value.'; $phpcsFile->addError($error, $stackPtr); } if ($tokens[$nextToken + 1]['code'] !== T_WHITESPACE) { $error = 'Expected one space after = sign of default value.'; $phpcsFile->addError($error, $stackPtr); } } } } }//end while }//end process() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/OpeningFunctionBraceSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/OpeningFunctionBraceSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/OpeningFunctionBraceSniff.php <?php /** * PEAR_Sniffs_Functions_OpeningFunctionBraceSniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: OpeningFunctionBraceSniff.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php'; /** * PEAR_Sniffs_Functions_OpeningFunctionBraceSniff. * * Checks that the opening brace of a function is on the line after the * function declaration. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class PEAR_Sniffs_Functions_OpeningFunctionBraceSniff extends Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff { }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php <?php /** * PEAR_Sniffs_Functions_ValidDefaultValueSniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: ValidDefaultValueSniff.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Sniff.php'; /** * PEAR_Sniffs_Functions_ValidDefaultValueSniff. * * A Sniff to ensure that parameters defined for a function that have a default * value come at the end of the function signature. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class PEAR_Sniffs_Functions_ValidDefaultValueSniff implements PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. * * @return array */ 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(); $argStart = $tokens[$stackPtr]['parenthesis_opener']; $argEnd = $tokens[$stackPtr]['parenthesis_closer']; // Flag for when we have found a default in our arg list. // If there is a value without a default after this, it is an error. $defaultFound = false; $nextArg = $argStart; while (($nextArg = $phpcsFile->findNext(T_VARIABLE, $nextArg + 1, $argEnd)) !== false) { $argHasDefault = self::_argHasDefault($phpcsFile, $nextArg); if (($argHasDefault === false) && ($defaultFound === true)) { $error = 'Arguments with default values must be at the end'; $error .= ' of the argument list.'; $phpcsFile->addError($error, $nextArg); return; } if ($argHasDefault === true) { $defaultFound = true; } } }//end process() /** * Returns true if the passed argument has a default value. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $argPtr The position of the argument * in the stack. * * @return bool */ private static function _argHasDefault(PHP_CodeSniffer_File $phpcsFile, $argPtr) { $tokens = $phpcsFile->getTokens(); $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $argPtr + 1, null, true); if ($tokens[$nextToken]['code'] !== T_EQUAL) { return false; } return true; }//end _argHasDefault() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc <?php $result = myFunction(); $result = myFunction($arg1, $arg2); $result = myFunction($arg1,$arg2); $result = myFunction($arg1 , $arg2); $result = myFunction($arg1 , $arg2); $result = myFunction($arg1, $arg2, $arg3,$arg4, $arg5); $result = myFunction($arg1, $arg2, $arg3, $arg4, $arg5); $result = myFunction($arg1, $arg2 = array()); $result = myFunction($arg1 , $arg2 =array()); $result = myFunction($arg1 , $arg2= array()); $result = myFunction($arg1 , $arg2=array()); $result = myFunction($arg1, $arg2 = array(), $arg3, $arg4, $arg5); throw new Exception("This is some massive string for a message", $cause); // Function definitions are ignored function myFunction($arg1,$arg2) { } function myFunction ($arg1,$arg2) { } function myFunction($arg1=1,$arg2=2) { } function myFunction($arg1 = 1,$arg2 = 2) { } $key = array_search($this->getArray(),$this->arrayMap); $this->error($obj->getCode(),$obj->getMessage(),$obj->getFile(),$obj->getLine()); make_foo($string /*the string*/ , true /*test*/); make_foo($string/*the string*/ , /*test*/ true); make_foo($string /*the string*/, /*test*/ true); ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php <?php /** * Unit test class for the FunctionCallArgumentSpacing sniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: FunctionCallArgumentSpacingUnitTest.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the FunctionCallArgumentSpacing 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. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class PEAR_Tests_Functions_FunctionCallArgumentSpacingUnitTest 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( 5 => 1, 6 => 1, 7 => 2, 8 => 1, 11 => 2, 12 => 2, 13 => 3, 42 => 1, 43 => 3, 45 => 1, 46 => 2, ); }//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/PEAR/Tests/Functions/ValidDefaultValueUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.inc <?php // No args. function myFunction() { } // No default args. function myFunction($arg1) { } // Valid function myFunction($arg1, $arg2='hello') { } // Valid with lots of args function myFunction($arg1, $arg2, $arg3, $arg4='hello', $arg5=array(), $arg6='hello') { } // Valid type hints function myFunction(array $arg1, array $arg2=array()) { } // Invalid function myFunction($arg2='hello', $arg1) { } // Invalid with lots of args function myFunction($arg1, $arg2, $arg3, $arg4='hello', $arg5, $arg6='hello') { } // Invalid type hints function myFunction(array $arg2=array(), array $arg1) { } class myClass() { // No args. function myFunction() { } // No default args. function myFunction($arg1) { } // Valid function myFunction($arg1, $arg2='hello') { } // Valid with lots of args function myFunction($arg1, $arg2, $arg3, $arg4='hello', $arg5=array(), $arg6='hello') { } // Valid type hints function myFunction(array $arg1, array $arg2=array()) { } // Invalid function myFunction($arg2='hello', $arg1) { } // Invalid with lots of args function myFunction($arg1, $arg2, $arg3, $arg4='hello', $arg5, $arg6='hello') { } // Invalid type hints function myFunction(array $arg2=array(), array $arg1) { } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.php <?php /** * Unit test class for the ValidDefaultValue sniff. * * PHP version 5 * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version CVS: $Id: ValidDefaultValueUnitTest.php,v 1.1 2006/12/27 23:19:18 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the ValidDefaultValue 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. * * @category PHP * @package PHP_CodeSniffer * @author Greg Sherwood <gsherwood@squiz.net> * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence * @version Release: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class PEAR_Tests_Functions_ValidDefaultValueUnitTest 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( 29 => 1, 34 => 1, 39 => 1, 71 => 1, 76 => 1, 81 => 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 ?>
« previous php.pear.cvs (#43850) next »