Initial Commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
|
||||
*
|
||||
* Ensures class and interface names start with a capital letter
|
||||
* and use _ separators.
|
||||
*
|
||||
* @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 PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_CLASS,
|
||||
T_INTERFACE,
|
||||
);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
|
||||
* @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();
|
||||
|
||||
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
$name = trim($tokens[$className]['content']);
|
||||
$errorData = array(ucfirst($tokens[$stackPtr]['content']));
|
||||
|
||||
// Make sure the first letter is a capital.
|
||||
if (preg_match('|^[A-Z]|', $name) === 0) {
|
||||
$error = '%s name must begin with a capital letter';
|
||||
$phpcsFile->addError($error, $stackPtr, 'StartWithCaptial', $errorData);
|
||||
}
|
||||
|
||||
// Check that each new word starts with a capital as well, but don't
|
||||
// check the first word, as it is checked above.
|
||||
$validName = true;
|
||||
$nameBits = explode('_', $name);
|
||||
$firstBit = array_shift($nameBits);
|
||||
foreach ($nameBits as $bit) {
|
||||
if ($bit === '' || $bit{0} !== strtoupper($bit{0})) {
|
||||
$validName = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($validName === false) {
|
||||
// Strip underscores because they cause the suggested name
|
||||
// to be incorrect.
|
||||
$nameBits = explode('_', trim($name, '_'));
|
||||
$firstBit = array_shift($nameBits);
|
||||
if ($firstBit === '') {
|
||||
$error = '%s name is not valid';
|
||||
$phpcsFile->addError($error, $stackPtr, 'Invalid', $errorData);
|
||||
} else {
|
||||
$newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
|
||||
foreach ($nameBits as $bit) {
|
||||
if ($bit !== '') {
|
||||
$newName .= strtoupper($bit{0}).substr($bit, 1).'_';
|
||||
}
|
||||
}
|
||||
|
||||
$newName = rtrim($newName, '_');
|
||||
$error = '%s name is not valid; consider %s instead';
|
||||
$data = $errorData;
|
||||
$data[] = $newName;
|
||||
$phpcsFile->addError($error, $stackPtr, 'Invalid', $data);
|
||||
}
|
||||
}//end if
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
|
||||
*
|
||||
* 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_AbstractScopeSniff', true) === false) {
|
||||
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
|
||||
}
|
||||
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
|
||||
*
|
||||
* Ensures method names are correct depending on whether they are public
|
||||
* or private, and that functions are named 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 PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* A list of all PHP magic methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $magicMethods = array(
|
||||
'construct',
|
||||
'destruct',
|
||||
'call',
|
||||
'callStatic',
|
||||
'get',
|
||||
'set',
|
||||
'isset',
|
||||
'unset',
|
||||
'sleep',
|
||||
'wakeup',
|
||||
'toString',
|
||||
'set_state',
|
||||
'clone',
|
||||
'invoke',
|
||||
);
|
||||
|
||||
/**
|
||||
* A list of all PHP magic functions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $magicFunctions = array('autoload');
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
|
||||
|
||||
}//end __construct()
|
||||
|
||||
|
||||
/**
|
||||
* Processes the tokens within the scope.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
|
||||
* @param int $stackPtr The position where this token was
|
||||
* found.
|
||||
* @param int $currScope The position of the current scope.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
|
||||
{
|
||||
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
if ($methodName === null) {
|
||||
// Ignore closures.
|
||||
return;
|
||||
}
|
||||
|
||||
$className = $phpcsFile->getDeclarationName($currScope);
|
||||
$errorData = array($className.'::'.$methodName);
|
||||
|
||||
// Is this a magic method. IE. is prefixed with "__".
|
||||
if (preg_match('|^__|', $methodName) !== 0) {
|
||||
$magicPart = substr($methodName, 2);
|
||||
if (in_array($magicPart, $this->magicMethods) === false) {
|
||||
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
|
||||
|
||||
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// PHP4 constructors are allowed to break our rules.
|
||||
if ($methodName === $className) {
|
||||
return;
|
||||
}
|
||||
|
||||
// PHP4 destructors are allowed to break our rules.
|
||||
if ($methodName === '_'.$className) {
|
||||
return;
|
||||
}
|
||||
|
||||
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
|
||||
$isPublic = ($methodProps['scope'] === 'private') ? false : true;
|
||||
$scope = $methodProps['scope'];
|
||||
$scopeSpecified = $methodProps['scope_specified'];
|
||||
|
||||
// If it's a private method, it must have an underscore on the front.
|
||||
if ($isPublic === false && $methodName{0} !== '_') {
|
||||
$error = 'Private method name "%s" must be prefixed with an underscore';
|
||||
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData);
|
||||
return;
|
||||
}
|
||||
|
||||
// If it's not a private method, it must not have an underscore on the front.
|
||||
if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
|
||||
$error = '%s method name "%s" must not be prefixed with an underscore';
|
||||
$data = array(
|
||||
ucfirst($scope),
|
||||
$errorData[0],
|
||||
);
|
||||
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the scope was specified on the method, then the method must be
|
||||
// camel caps and an underscore should be checked for. If it wasn't
|
||||
// specified, treat it like a public method and remove the underscore
|
||||
// prefix if there is one because we cant determine if it is private or
|
||||
// public.
|
||||
$testMethodName = $methodName;
|
||||
if ($scopeSpecified === false && $methodName{0} === '_') {
|
||||
$testMethodName = substr($methodName, 1);
|
||||
}
|
||||
|
||||
if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
|
||||
if ($scopeSpecified === true) {
|
||||
$error = '%s method name "%s" is not in camel caps format';
|
||||
$data = array(
|
||||
ucfirst($scope),
|
||||
$errorData[0],
|
||||
);
|
||||
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
|
||||
} else {
|
||||
$error = 'Method name "%s" is not in camel caps format';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}//end processTokenWithinScope()
|
||||
|
||||
|
||||
/**
|
||||
* Processes the tokens outside the scope.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being processed.
|
||||
* @param int $stackPtr The position where this token was
|
||||
* found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$functionName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
if ($functionName === null) {
|
||||
// Ignore closures.
|
||||
return;
|
||||
}
|
||||
|
||||
$errorData = array($functionName);
|
||||
|
||||
// Is this a magic function. IE. is prefixed with "__".
|
||||
if (preg_match('|^__|', $functionName) !== 0) {
|
||||
$magicPart = substr($functionName, 2);
|
||||
if (in_array($magicPart, $this->magicFunctions) === false) {
|
||||
$error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
|
||||
$phpcsFile->addError($error, $stackPtr, 'FunctionDoubleUnderscore', $errorData);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Function names can be in two parts; the package name and
|
||||
// the function name.
|
||||
$packagePart = '';
|
||||
$camelCapsPart = '';
|
||||
$underscorePos = strrpos($functionName, '_');
|
||||
if ($underscorePos === false) {
|
||||
$camelCapsPart = $functionName;
|
||||
} else {
|
||||
$packagePart = substr($functionName, 0, $underscorePos);
|
||||
$camelCapsPart = substr($functionName, ($underscorePos + 1));
|
||||
|
||||
// We don't care about _'s on the front.
|
||||
$packagePart = ltrim($packagePart, '_');
|
||||
}
|
||||
|
||||
// If it has a package part, make sure the first letter is a capital.
|
||||
if ($packagePart !== '') {
|
||||
if ($functionName{0} === '_') {
|
||||
$error = 'Function name "%s" is invalid; only private methods should be prefixed with an underscore';
|
||||
$phpcsFile->addError($error, $stackPtr, 'FunctionUnderscore', $errorData);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($functionName{0} !== strtoupper($functionName{0})) {
|
||||
$error = 'Function name "%s" is prefixed with a package name but does not begin with a capital letter';
|
||||
$phpcsFile->addError($error, $stackPtr, 'FunctionNoCaptial', $errorData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If it doesn't have a camel caps part, it's not valid.
|
||||
if (trim($camelCapsPart) === '') {
|
||||
$error = 'Function name "%s" is not valid; name appears incomplete';
|
||||
$phpcsFile->addError($error, $stackPtr, 'FunctionInvalid', $errorData);
|
||||
return;
|
||||
}
|
||||
|
||||
$validName = true;
|
||||
$newPackagePart = $packagePart;
|
||||
$newCamelCapsPart = $camelCapsPart;
|
||||
|
||||
// Every function must have a camel caps part, so check that first.
|
||||
if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
|
||||
$validName = false;
|
||||
$newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
|
||||
}
|
||||
|
||||
if ($packagePart !== '') {
|
||||
// Check that each new word starts with a capital.
|
||||
$nameBits = explode('_', $packagePart);
|
||||
foreach ($nameBits as $bit) {
|
||||
if ($bit{0} !== strtoupper($bit{0})) {
|
||||
$newPackagePart = '';
|
||||
foreach ($nameBits as $bit) {
|
||||
$newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
|
||||
}
|
||||
|
||||
$validName = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($validName === false) {
|
||||
$newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
|
||||
if ($newPackagePart === '') {
|
||||
$newName = $newCamelCapsPart;
|
||||
} else {
|
||||
$newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
|
||||
}
|
||||
|
||||
$error = 'Function name "%s" is invalid; consider "%s" instead';
|
||||
$data = $errorData;
|
||||
$data[] = $newName;
|
||||
$phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid', $data);
|
||||
}
|
||||
|
||||
}//end processTokenOutsideScope()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
|
||||
$error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
|
||||
throw new PHP_CodeSniffer_Exception($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* Checks the naming of member variables.
|
||||
*
|
||||
* @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 PEAR_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Processes class member variables.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
|
||||
if (empty($memberProps) === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
|
||||
$isPublic = ($memberProps['scope'] === 'private') ? false : true;
|
||||
$scope = $memberProps['scope'];
|
||||
$scopeSpecified = $memberProps['scope_specified'];
|
||||
|
||||
// If it's a private member, it must have an underscore on the front.
|
||||
if ($isPublic === false && $memberName{0} !== '_') {
|
||||
$error = 'Private member variable "%s" must be prefixed with an underscore';
|
||||
$data = array($memberName);
|
||||
$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
// If it's not a private member, it must not have an underscore on the front.
|
||||
if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
|
||||
$error = '%s member variable "%s" must not be prefixed with an underscore';
|
||||
$data = array(
|
||||
ucfirst($scope),
|
||||
$memberName,
|
||||
);
|
||||
$phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
|
||||
return;
|
||||
}
|
||||
|
||||
}//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
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user