Skip to content

Commit

Permalink
Control structure sniffs for the Squiz coding standard
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@225434 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
gsherwood committed Dec 21, 2006
1 parent bce506d commit ad96ebb
Show file tree
Hide file tree
Showing 21 changed files with 1,699 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Verifies that control statements conform to their coding standards.
*
* 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$
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once 'PHP/CodeSniffer/Standards/AbstractPatternSniff.php';

/**
* Verifies that control statements conform to their coding standards.
*
* @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 Squiz_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSniffer_Standards_AbstractPatternSniff
{


/**
* Returns the patterns that this test wishes to verify.
*
* @return array(string)
*/
protected function getPatterns()
{
return array(
"try {\n...} catch (...) {\n",
"do {\n...} while (...);\n",
"while (...) {\n",
"for (...) {\n",
"if (...) {\n",
"foreach (...) {\n",
"} else if (...) {\n",
"} else {\n",
"do {\n",
);

}//end getPatterns()


}//end class

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* 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$
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once 'PHP/CodeSniffer/Sniff.php';

/**
* Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff.
*
* Verifies that there are not elseif statements. The else and the if should
* be separated by a space.
*
* @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 Squiz_Sniffs_ControlStructures_ElseIfDeclarationSniff implements PHP_CodeSniffer_Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_ELSEIF);

}//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)
{
$error = 'Usage of ELSEIF not allowed. Use ELSE IF instead.';
$phpcsFile->addError($error, $stackPtr);

}//end process()


}//end class


?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
*
* 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$
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once 'PHP/CodeSniffer/Sniff.php';

/**
* Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff.
*
* Verifies that there is a spce between each condition of foreach loops.
*
* @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 Squiz_Sniffs_ControlStructures_ForEachLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_FOREACH,
);

}//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();

$errors = array();

$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];

if ($tokens[$openingBracket + 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found after opening bracket of FOREACH loop';
}

if ($tokens[$closingBracket - 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found before closing bracket of FOREACH loop';
}

$asToken = $phpcsFile->findNext(T_AS, $openingBracket);
$doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $openingBracket, $closingBracket);

if ($doubleArrow !== false) {
if ($tokens[$doubleArrow - 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space before "=>"; 0 found';
} else {
if (strlen($tokens[$doubleArrow - 1]['content']) !== 1) {
$spaces = strlen($tokens[$doubleArrow - 1]['content']);
$errors[] = "Expected 1 space before \"=>\"; $spaces found";
}

}

if ($tokens[$doubleArrow + 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space after "=>"; 0 found';
} else {
if (strlen($tokens[$doubleArrow + 1]['content']) !== 1) {
$spaces = strlen($tokens[$doubleArrow + 1]['content']);
$errors[] = "Expected 1 space after \"=>\"; $spaces found";
}

}

}//end if

if ($tokens[$asToken + 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space before "as"; 0 found';
} else {
if (strlen($tokens[$asToken - 1]['content']) !== 1) {
$spaces = strlen($tokens[$asToken - 1]['content']);
$errors[] = "Expected 1 space before \"as\"; $spaces found";
}
}

if ($tokens[$asToken + 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space after "as"; 0 found';
} else {
if (strlen($tokens[$asToken + 1]['content']) !== 1) {
$spaces = strlen($tokens[$asToken + 1]['content']);
$errors[] = "Expected 1 space after \"as\"; $spaces found";
}

}

foreach ($errors as $error) {
$phpcsFile->addError($error, $stackPtr);
}

}//end process()


}//end class

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
*
* 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$
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

require_once 'PHP/CodeSniffer/Sniff.php';

/**
* Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff.
*
* Verifies that there is a spce between each condition of for loops.
*
* @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 Squiz_Sniffs_ControlStructures_ForLoopDeclarationSniff implements PHP_CodeSniffer_Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_FOR,
);

}//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();

$errors = array();

$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];

if ($tokens[$openingBracket + 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found after opening bracket of FOR loop';
}

if ($tokens[$closingBracket - 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found before closing bracket of FOR loop';
}

$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket);
$secondSemicolon = $phpcsFile->findNext(T_SEMICOLON, $firstSemicolon + 1);

// Check whitespace around each of the tokens.
if ($tokens[$firstSemicolon - 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found before first semicolon of FOR loop';
}

if ($tokens[$firstSemicolon + 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space after first semicolon of FOR loop; 0 found';
} else {
if (strlen($tokens[$firstSemicolon + 1]['content']) !== 1) {
$spaces = strlen($tokens[$firstSemicolon + 1]['content']);
$errors[] = "Expected 1 space after first semicolon of FOR loop; $spaces found";
}
}

if ($tokens[$secondSemicolon - 1]['code'] === T_WHITESPACE) {
$errors[] = 'Space found before second semicolon of FOR loop';
}

if ($tokens[$secondSemicolon + 1]['code'] !== T_WHITESPACE) {
$errors[] = 'Expected 1 space after second semicolon of FOR loop; 0 found';
} else {
if (strlen($tokens[$secondSemicolon + 1]['content']) !== 1) {
$spaces = strlen($tokens[$firstSemicolon + 1]['content']);
$errors[] = "Expected 1 space after second semicolon of FOR loop; $spaces found";
}
}

foreach ($errors as $error) {
$phpcsFile->addError($error, $stackPtr);
}

}//end process()


}//end class

?>
Loading

0 comments on commit ad96ebb

Please sign in to comment.