Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,77 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_CastSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_CastSpacingSniff.
*
* Ensure cast statements dont contain whitespace.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_CastSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return PHP_CodeSniffer_Tokens::$castTokens;
}//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();
$content = $tokens[$stackPtr]['content'];
$expected = str_replace(' ', '', $content);
$expected = str_replace("\t", '', $expected);
if ($content !== $expected) {
$error = 'Cast statements must not contain whitespace; expected "%s" but found "%s"';
$data = array(
$expected,
$content,
);
$phpcsFile->addError($error, $stackPtr, 'ContainsWhiteSpace', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,211 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
*
* Checks that control structures have the correct spacing around brackets.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_IF,
T_WHILE,
T_FOREACH,
T_FOR,
T_SWITCH,
T_DO,
T_ELSE,
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)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
$parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
$parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) {
$gap = strlen($tokens[($parenOpener + 1)]['content']);
$error = 'Expected 0 spaces after opening bracket; %s found';
$data = array($gap);
$phpcsFile->addError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
}
if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']
&& $tokens[($parenCloser - 1)]['code'] === T_WHITESPACE
) {
$gap = strlen($tokens[($parenCloser - 1)]['content']);
$error = 'Expected 0 spaces before closing bracket; %s found';
$data = array($gap);
$phpcsFile->addError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
}
}//end if
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
$scopeCloser = $tokens[$stackPtr]['scope_closer'];
$firstContent = $phpcsFile->findNext(
T_WHITESPACE,
($scopeOpener + 1),
null,
true
);
if ($tokens[$firstContent]['line'] !== ($tokens[$scopeOpener]['line'] + 1)) {
$error = 'Blank line found at start of control structure';
$phpcsFile->addError($error, $scopeOpener, 'SpacingBeforeOpen');
}
$lastContent = $phpcsFile->findPrevious(
T_WHITESPACE,
($scopeCloser - 1),
null,
true
);
if ($tokens[$lastContent]['line'] !== ($tokens[$scopeCloser]['line'] - 1)) {
$errorToken = $scopeCloser;
for ($i = ($scopeCloser - 1); $i > $lastContent; $i--) {
if ($tokens[$i]['line'] < $tokens[$scopeCloser]['line']) {
$errorToken = $i;
break;
}
}
$error = 'Blank line found at end of control structure';
$phpcsFile->addError($error, $errorToken, 'SpacingAfterClose');
}
$trailingContent = $phpcsFile->findNext(
T_WHITESPACE,
($scopeCloser + 1),
null,
true
);
if ($tokens[$trailingContent]['code'] === T_ELSE) {
if ($tokens[$stackPtr]['code'] === T_IF) {
// IF with ELSE.
return;
}
}
if ($tokens[$trailingContent]['code'] === T_COMMENT) {
if ($tokens[$trailingContent]['line'] === $tokens[$scopeCloser]['line']) {
if (substr($tokens[$trailingContent]['content'], 0, 5) === '//end') {
// There is an end comment, so we have to get the next piece
// of content.
$trailingContent = $phpcsFile->findNext(
T_WHITESPACE,
($trailingContent + 1),
null,
true
);
}
}
}
if ($tokens[$trailingContent]['code'] === T_BREAK) {
// If this BREAK is closing a CASE, we don't need the
// blank line after this control structure.
if (isset($tokens[$trailingContent]['scope_condition']) === true) {
$condition = $tokens[$trailingContent]['scope_condition'];
if ($tokens[$condition]['code'] === T_CASE
|| $tokens[$condition]['code'] === T_DEFAULT
) {
return;
}
}
}
if ($tokens[$trailingContent]['code'] === T_CLOSE_TAG) {
// At the end of the script or embedded code.
return;
}
if ($tokens[$trailingContent]['code'] === T_CLOSE_CURLY_BRACKET) {
// Another control structure's closing brace.
if (isset($tokens[$trailingContent]['scope_condition']) === true) {
$owner = $tokens[$trailingContent]['scope_condition'];
if ($tokens[$owner]['code'] === T_FUNCTION) {
// The next content is the closing brace of a function
// so normal function rules apply and we can ignore it.
return;
}
}
if ($tokens[$trailingContent]['line'] !== ($tokens[$scopeCloser]['line'] + 1)) {
$error = 'Blank line found after control structure';
$phpcsFile->addError($error, $scopeCloser, 'LineAfterClose');
}
} else {
if ($tokens[$trailingContent]['line'] === ($tokens[$scopeCloser]['line'] + 1)) {
$error = 'No blank line found after control structure';
$phpcsFile->addError($error, $scopeCloser, 'NoLineAfterClose');
}
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,116 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_FunctionClosingBraceSpaceSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_FunctionClosingBraceSpaceSniff.
*
* Checks that there is one empty line before the closing brace of a function.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_FunctionClosingBraceSpaceSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* 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();
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// Probably an interface method.
return;
}
$closeBrace = $tokens[$stackPtr]['scope_closer'];
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
// Special case for empty JS functions
if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']) {
// In this case, the opening and closing brace must be
// right next to each other.
if ($tokens[$stackPtr]['scope_closer'] !== ($tokens[$stackPtr]['scope_opener'] + 1)) {
$error = 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
$phpcsFile->addError($error, $closeBrace, 'SpacingBetween');
}
return;
}
$braceLine = $tokens[$closeBrace]['line'];
$prevLine = $tokens[$prevContent]['line'];
$found = ($braceLine - $prevLine - 1);
if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true || isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
// Nested function.
if ($found < 0) {
$error = 'Closing brace of nested function must be on a new line';
$phpcsFile->addError($error, $closeBrace, 'ContentBeforeClose');
} else if ($found > 0) {
$error = 'Expected 0 blank lines before closing brace of nested function; %s found';
$data = array($found);
$phpcsFile->addError($error, $closeBrace, 'SpacingBeforeNestedClose', $data);
}
} else {
if ($found !== 1) {
$error = 'Expected 1 blank line before closing function brace; %s found';
$data = array($found);
$phpcsFile->addError($error, $closeBrace, 'SpacingBeforeClose', $data);
}
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,128 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
*
* Checks that there is no empty line after the opening brace of a function.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* 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();
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
// Probably an interface method.
return;
}
$openBrace = $tokens[$stackPtr]['scope_opener'];
$nextContent = $phpcsFile->findNext(T_WHITESPACE, ($openBrace + 1), null, true);
if ($nextContent === $tokens[$stackPtr]['scope_closer']) {
// The next bit of content is the closing brace, so this
// is an empty function and should have a blank line
// between the opening and closing braces.
return;
}
$braceLine = $tokens[$openBrace]['line'];
$nextLine = $tokens[$nextContent]['line'];
$found = ($nextLine - $braceLine - 1);
if ($found > 0) {
$error = 'Expected 0 blank lines after opening function brace; %s found';
$data = array($found);
$phpcsFile->addError($error, $openBrace, 'SpacingAfter', $data);
}
if ($phpcsFile->tokenizerType === 'JS') {
// Do some additional checking before the function brace.
$nestedFunction = ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true || isset($tokens[$stackPtr]['nested_parenthesis']) === true);
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
$lineDifference = ($braceLine - $functionLine);
if ($nestedFunction === true) {
if ($lineDifference > 0) {
$error = 'Expected 0 blank lines before opening brace of nested function; %s found';
$data = array($found);
$phpcsFile->addError($error, $openBrace, 'SpacingAfterNested', $data);
}
} else {
if ($lineDifference === 0) {
$error = 'Opening brace should be on a new line';
$phpcsFile->addError($error, $openBrace, 'ContentBefore');
return;
}
if ($lineDifference > 1) {
$error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
$data = array(($lineDifference - 1));
$phpcsFile->addError($error, $openBrace, 'SpacingBefore', $data);
return;
}
}//end if
}//end if
}//end process()
}//end class
?>

View File

@@ -0,0 +1,170 @@
<?php
/**
* Squiz_Sniffs_Formatting_FunctionSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff.
*
* Checks the separation between methods in a class or interface.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_FunctionSpacingSniff 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 sniff, 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();
/*
Check the number of blank lines
after the function.
*/
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
// Must be an interface method, so the closer is the semi-colon.
$closer = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);
} else {
$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) {
continue;
} else {
$nextLineToken = ($i + 1);
break;
}
}
if (is_null($nextLineToken) === true) {
// Never found the next line, which means
// there are 0 blank lines after the function.
$foundLines = 0;
} else {
$nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($nextLineToken + 1), null, true);
if ($nextContent === false) {
// We are at the end of the file.
$foundLines = 0;
} else {
$foundLines = ($tokens[$nextContent]['line'] - $tokens[$nextLineToken]['line']);
}
}
if ($foundLines !== 2) {
$error = 'Expected 2 blank lines after function; %s found';
$data = array($foundLines);
$phpcsFile->addError($error, $closer, 'After', $data);
}
/*
Check the number of blank lines
before the function.
*/
$prevLineToken = null;
for ($i = $stackPtr; $i > 0; $i--) {
if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
continue;
} else {
$prevLineToken = $i;
break;
}
}
if (is_null($prevLineToken) === true) {
// Never found the previous line, which means
// there are 0 blank lines before the function.
$foundLines = 0;
} else {
$prevContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevLineToken, null, true);
// Before we throw an error, check that we are not throwing an error
// for another function. We don't want to error for no blank lines after
// the previous function and no blank lines before this one as well.
$currentLine = $tokens[$stackPtr]['line'];
$prevLine = ($tokens[$prevContent]['line'] - 1);
$i = ($stackPtr - 1);
$foundLines = 0;
while ($currentLine != $prevLine && $currentLine > 1 && $i > 0) {
if (isset($tokens[$i]['scope_condition']) === true) {
$scopeCondition = $tokens[$i]['scope_condition'];
if ($tokens[$scopeCondition]['code'] === T_FUNCTION) {
// Found a previous function.
return;
}
} else if ($tokens[$i]['code'] === T_FUNCTION) {
// Found another interface function.
return;
}
$currentLine = $tokens[$i]['line'];
if ($currentLine === $prevLine) {
break;
}
if ($tokens[($i - 1)]['line'] < $currentLine && $tokens[($i + 1)]['line'] > $currentLine) {
// This token is on a line by itself. If it is whitespace, the line is empty.
if ($tokens[$i]['code'] === T_WHITESPACE) {
$foundLines++;
}
}
$i--;
}//end while
}//end if
if ($foundLines !== 2) {
$error = 'Expected 2 blank lines before function; %s found';
$data = array($foundLines);
$phpcsFile->addError($error, $stackPtr, 'Before', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,96 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_LanguageConstructSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_LanguageConstructSpacingSniff.
*
* Ensures all language constructs (without brackets) contain a
* single space between themselves and their content.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_LanguageConstructSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_ECHO,
T_PRINT,
T_RETURN,
T_INCLUDE,
T_INCLUDE_ONCE,
T_REQUIRE,
T_REQUIRE_ONCE,
T_NEW,
);
}//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 ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) {
// No content for this language construct.
return;
}
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
$content = $tokens[($stackPtr + 1)]['content'];
$contentLength = strlen($content);
if ($contentLength !== 1) {
$error = 'Language constructs must be followed by a single space; expected 1 space but found %s';
$data = array($contentLength);
$phpcsFile->addError($error, $stackPtr, 'IncorrectSingle', $data);
}
} else {
$error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"';
$data = array(
$tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'],
$tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'],
);
$phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,106 @@
<?php
/**
* Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
*
* Verifies that operators have valid spacing surrounding them.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_LogicalOperatorSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return PHP_CodeSniffer_Tokens::$booleanOperators;
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
* @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();
// Check there is one space before the operator.
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space before logical operator; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore');
} else {
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$stackPtr]['line'] === $tokens[$prev]['line']
&& strlen($tokens[($stackPtr - 1)]['content']) !== 1
) {
$found = strlen($tokens[($stackPtr - 1)]['content']);
$error = 'Expected 1 space before logical operator; %s found';
$data = array($found);
$phpcsFile->addError($error, $stackPtr, 'TooMuchSpaceBefore', $data);
}
}
// Check there is one space after the operator.
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after logical operator; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfter');
} else {
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$stackPtr]['line'] === $tokens[$next]['line']
&& strlen($tokens[($stackPtr + 1)]['content']) !== 1
) {
$found = strlen($tokens[($stackPtr + 1)]['content']);
$error = 'Expected 1 space after logical operator; %s found';
$data = array($found);
$phpcsFile->addError($error, $stackPtr, 'TooMuchSpaceAfter', $data);
}
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,122 @@
<?php
/**
* Verifies that class members are spaced correctly.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
}
/**
* Verifies that class members are spaced correctly.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_MemberVarSpacingSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
{
/**
* Processes the function tokens within the class.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// There needs to be 1 blank line before the var, not counting comments.
$prevLineToken = null;
for ($i = ($stackPtr - 1); $i > 0; $i--) {
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) {
// Skip comments.
continue;
} else if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) {
// Not the end of the line.
continue;
} else {
// If this is a WHITESPACE token, and the token right before
// it is a DOC_COMMENT, then it is just the newline after the
// member var's comment, and can be skipped.
if ($tokens[$i]['code'] === T_WHITESPACE && in_array($tokens[($i - 1)]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) {
continue;
}
$prevLineToken = $i;
break;
}
}
if (is_null($prevLineToken) === true) {
// Never found the previous line, which means
// there are 0 blank lines before the member var.
$foundLines = 0;
} else {
$prevContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT), $prevLineToken, null, true);
$foundLines = ($tokens[$prevLineToken]['line'] - $tokens[$prevContent]['line']);
}//end if
if ($foundLines !== 1) {
$error = 'Expected 1 blank line before member var; %s found';
$data = array($foundLines);
$phpcsFile->addError($error, $stackPtr, 'After', $data);
}
}//end processMemberVar()
/**
* Processes normal variables.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// We don't care about normal variables.
return;
}//end processVariable()
/**
* Processes variables in double quoted strings.
*
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
// We don't care about normal variables.
return;
}//end processVariableInString()
}//end class
?>

View File

@@ -0,0 +1,76 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff.
*
* Ensure there is no whitespace before a semicolon.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_OBJECT_OPERATOR);
}//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();
$prevType = $tokens[($stackPtr - 1)]['code'];
if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$error = 'Space found before object operator';
$phpcsFile->addError($error, $stackPtr, 'Before');
}
$nextType = $tokens[($stackPtr + 1)]['code'];
if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$error = 'Space found after object operator';
$phpcsFile->addError($error, $stackPtr, 'After');
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,212 @@
<?php
/**
* Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
*
* Verifies that operators have valid spacing surrounding them.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
$comparison = PHP_CodeSniffer_Tokens::$comparisonTokens;
$operators = PHP_CodeSniffer_Tokens::$operators;
$assignment = PHP_CodeSniffer_Tokens::$assignmentTokens;
return array_unique(array_merge($comparison, $operators, $assignment));
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
* @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 default values in function declarations.
if ($tokens[$stackPtr]['code'] === T_EQUAL
|| $tokens[$stackPtr]['code'] === T_MINUS
) {
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
$bracket = array_pop($parenthesis);
if (isset($tokens[$bracket]['parenthesis_owner']) === true) {
$function = $tokens[$bracket]['parenthesis_owner'];
if ($tokens[$function]['code'] === T_FUNCTION) {
return;
}
}
}
}
if ($tokens[$stackPtr]['code'] === T_EQUAL) {
// Skip for '=&' case.
if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] === T_BITWISE_AND) {
return;
}
}
if ($tokens[$stackPtr]['code'] === T_BITWISE_AND) {
// If its not a reference, then we expect one space either side of the
// bitwise operator.
if ($phpcsFile->isReference($stackPtr) === false) {
// Check there is one space before the & operator.
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space before "&" operator; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeAmp');
} else {
if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
$found = strlen($tokens[($stackPtr - 1)]['content']);
$error = 'Expected 1 space before "&" operator; %s found';
$data = array($found);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeAmp', $data);
}
}
// Check there is one space after the & operator.
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$error = 'Expected 1 space after "&" operator; 0 found';
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterAmp');
} else {
if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
$found = strlen($tokens[($stackPtr + 1)]['content']);
$error = 'Expected 1 space after "&" operator; %s found';
$data = array($found);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterAmp', $data);
}
}
}//end if
} else {
if ($tokens[$stackPtr]['code'] === T_MINUS) {
// Check minus spacing, but make sure we aren't just assigning
// a minus value or returning one.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_RETURN) {
// Just returning a negative value; eg. return -1.
return;
}
if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$operators) === true) {
// Just trying to operate on a negative value; eg. ($var * -1).
return;
}
if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens) === true) {
// Just trying to compare a negative value; eg. ($var === -1).
return;
}
// A list of tokens that indicate that the token is not
// part of an arithmetic operation.
$invalidTokens = array(
T_COMMA,
T_OPEN_PARENTHESIS,
T_OPEN_SQUARE_BRACKET,
T_DOUBLE_ARROW,
T_COLON,
);
if (in_array($tokens[$prev]['code'], $invalidTokens) === true) {
// Just trying to use a negative value; eg. myFunction($var, -2).
return;
}
$number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if (in_array($tokens[$number]['code'], array(T_LNUMBER, T_VARIABLE)) === true) {
$semi = $phpcsFile->findNext(T_WHITESPACE, ($number + 1), null, true);
if ($tokens[$semi]['code'] === T_SEMICOLON) {
if ($prev !== false && (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true)) {
// This is a negative assignment.
return;
}
}
}
}//end if
$operator = $tokens[$stackPtr]['content'];
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
$error = "Expected 1 space before \"$operator\"; 0 found";
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore');
} else if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
// Don't throw an error for assignments, because other standards allow
// multiple spaces there to align multiple assignments.
if (in_array($tokens[$stackPtr]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === false) {
$found = strlen($tokens[($stackPtr - 1)]['content']);
$error = 'Expected 1 space before "%s"; %s found';
$data = array(
$operator,
$found,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
}
}
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
$error = "Expected 1 space after \"$operator\"; 0 found";
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfter');
} else if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
$found = strlen($tokens[($stackPtr + 1)]['content']);
$error = 'Expected 1 space after "%s"; %s found';
$data = array(
$operator,
$found,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfter', $data);
}
}//end if
}//end process()
}//end class
?>

View File

@@ -0,0 +1,85 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff.
*
* Ensures that the colon in a property or label definition has a single
* space after it and no space before it.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array('JS');
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_PROPERTY,
T_LABEL,
);
}//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();
$colon = $phpcsFile->findNext(T_COLON, ($stackPtr + 1));
if ($colon !== ($stackPtr + 1)) {
$error = 'There must be no space before the colon in a property/label declaration';
$phpcsFile->addError($error, $stackPtr, 'Before');
}
if ($tokens[($colon + 1)]['code'] !== T_WHITESPACE || $tokens[($colon + 1)]['content'] !== ' ') {
$error = 'There must be a single space after the colon in a property/label declaration';
$phpcsFile->addError($error, $stackPtr, 'After');
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,110 @@
<?php
/**
* Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
*
* Checks that the closing braces of scopes are aligned correctly.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_ScopeClosingBraceSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return PHP_CodeSniffer_Tokens::$scopeOpeners;
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
* @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 this is an inline condition (ie. there is no scope opener), then
// return, as this is not a new scope.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
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 - 1);
for ($lineStart; $lineStart > 0; $lineStart--) {
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
break;
}
}
// We found a new line, now go forward and find the
// first non-whitespace token.
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), ($lineStart + 1), null, true);
$startColumn = $tokens[$lineStart]['column'];
$scopeStart = $tokens[$stackPtr]['scope_opener'];
$scopeEnd = $tokens[$stackPtr]['scope_closer'];
// Check that the closing brace is on it's own line.
$lastContent = $phpcsFile->findPrevious(array(T_WHITESPACE), ($scopeEnd - 1), $scopeStart, true);
if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
$error = 'Closing brace must be on a line by itself';
$phpcsFile->addError($error, $scopeEnd, 'ContentBefore');
return;
}
// Check now that the closing brace is lined up correctly.
$braceIndent = $tokens[$scopeEnd]['column'];
if (in_array($tokens[$stackPtr]['code'], array(T_CASE, T_DEFAULT)) === false) {
if ($braceIndent !== $startColumn) {
$error = 'Closing brace indented incorrectly; expected %s spaces, found %s';
$data = array(
($startColumn - 1),
($braceIndent - 1),
);
$phpcsFile->addError($error, $scopeEnd, 'Indent', $data);
}
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,87 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_ScopeKeywordSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_ScopeKeywordSpacingSniff.
*
* Ensure there is a single space after scope keywords.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_ScopeKeywordSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
$register = PHP_CodeSniffer_Tokens::$scopeModifiers;
$register[] = T_STATIC;
return $register;
}//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();
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$stackPtr]['code'] === T_STATIC
&& ($tokens[$nextToken]['code'] === T_DOUBLE_COLON
|| $tokens[$prevToken]['code'] === T_NEW)
) {
// Late static binding, e.g., static:: OR new static() usage.
return;
}
$nextToken = $tokens[($stackPtr + 1)];
if ($nextToken['code'] !== T_WHITESPACE
|| strlen($nextToken['content']) !== 1
|| $nextToken['content'] === $phpcsFile->eolChar
) {
$error = 'Scope keyword "%s" must be followed by a single space';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,86 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_SemicolonSpacingSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_SemicolonSpacingSniff.
*
* Ensure there is no whitespace before a semicolon.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_SemicolonSpacingSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_SEMICOLON);
}//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();
$prevType = $tokens[($stackPtr - 1)]['code'];
if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
$nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true);
$expected = $tokens[$nonSpace]['content'].';';
$found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).';';
$error = 'Space found before semicolon; expected "%s" but found "%s"';
$data = array(
$expected,
$found,
);
$phpcsFile->addError($error, $stackPtr, 'Incorrect', $data);
}
}//end process()
}//end class
?>

View File

@@ -0,0 +1,204 @@
<?php
/**
* Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
*
* Checks that no whitespace proceeds the first content of the file, exists
* after the last content of the file, resides after content on any line, or
* are two empty lines in functions.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.3
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
'CSS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_OPEN_TAG,
T_CLOSE_TAG,
T_WHITESPACE,
T_COMMENT,
);
}//end register()
/**
* Processes this sniff, 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 ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
/*
Check for start of file whitespace.
*/
if ($phpcsFile->tokenizerType !== 'PHP') {
// The first token is always the open tag inserted when tokenizsed
// and the second token is always the first piece of content in
// the file. If the second token is whitespace, there was
// whitespace at the start of the file.
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
return;
}
} else {
// If its the first token, then there is no space.
if ($stackPtr === 0) {
return;
}
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
// If we find something that isn't inline html then there is something previous in the file.
if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
return;
}
// If we have ended up with inline html make sure it isn't just whitespace.
$tokenContent = trim($tokens[$i]['content']);
if ($tokenContent !== '') {
return;
}
}
}//end if
$phpcsFile->addError('Additional whitespace found at start of file', $stackPtr, 'StartFile');
} else if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
/*
Check for end of file whitespace.
*/
if ($phpcsFile->tokenizerType === 'JS') {
// The last token is always the close tag inserted when tokenizsed
// and the second last token is always the last piece of content in
// the file. If the second last token is whitespace, there was
// whitespace at the end of the file.
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
return;
}
} else if ($phpcsFile->tokenizerType === 'CSS') {
// The last two tokens are always the close tag and whitespace
// inserted when tokenizsed and the third last token is always the
// last piece of content in the file. If the third last token is
// whitespace, there was whitespace at the end of the file.
if ($tokens[($stackPtr - 3)]['code'] !== T_WHITESPACE) {
return;
}
// Adjust the pointer to give the correct line number for the error.
$stackPtr -= 2;
} else {
if (isset($tokens[($stackPtr + 1)]) === false) {
// The close PHP token is the last in the file.
return;
}
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
// If we find something that isn't inline html then there
// is more to the file.
if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
return;
}
// If we have ended up with inline html make sure it
// isn't just whitespace.
$tokenContent = trim($tokens[$i]['content']);
if (empty($tokenContent) === false) {
return;
}
}
}
$phpcsFile->addError('Additional whitespace found at end of file', $stackPtr, 'EndFile');
} else {
/*
Check for end of line whitespace.
*/
if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) {
return;
}
$tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar);
if (empty($tokenContent) === false) {
if (preg_match('|^.*\s+$|', $tokenContent) !== 0) {
$phpcsFile->addError('Whitespace found at end of line', $stackPtr, 'EndLine');
}
}
/*
Check for multiple blanks lines in a function.
*/
if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
if ($tokens[($stackPtr - 1)]['line'] < $tokens[$stackPtr]['line'] && $tokens[($stackPtr - 2)]['line'] === $tokens[($stackPtr - 1)]['line']) {
// This is an empty line and the line before this one is not
// empty, so this could be the start of a multiple empty
// line block.
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
$lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line'];
if ($lines > 1) {
$error = 'Functions must not contain multiple empty lines in a row; found %s empty lines';
$data = array($lines);
$phpcsFile->addError($error, $stackPtr, 'EmptyLines', $data);
}
}
}
}//end if
}//end process()
}//end class
?>