More code style fixes
This commit is contained in:
parent
22dc29f945
commit
578aad3e2e
@ -6,7 +6,7 @@ test:7:
|
||||
- php composer.phar install --ignore-platform-reqs
|
||||
image: php:7-alpine
|
||||
script:
|
||||
- phpdbg -qrr -- vendor/bin/phpunit --coverage-text --colors=never
|
||||
- phpdbg -qrr -- vendor/bin/phpunit -c build --coverage-text --colors=never
|
||||
|
||||
test:7.1:
|
||||
stage: test
|
||||
@ -17,7 +17,7 @@ test:7.1:
|
||||
- php composer.phar install --ignore-platform-reqs
|
||||
image: php:7.1-alpine
|
||||
script:
|
||||
- phpdbg -qrr -- ./vendor/bin/phpunit --coverage-text --colors=never
|
||||
- phpdbg -qrr -- ./vendor/bin/phpunit -c build --coverage-text --colors=never
|
||||
|
||||
test:7.2:
|
||||
stage: test
|
||||
@ -28,4 +28,4 @@ test:7.2:
|
||||
- php composer.phar install --ignore-platform-reqs
|
||||
image: php:7.2-alpine
|
||||
script:
|
||||
- phpdbg -qrr -- ./vendor/bin/phpunit --coverage-text --colors=never
|
||||
- phpdbg -qrr -- ./vendor/bin/phpunit -c build --coverage-text --colors=never
|
@ -204,7 +204,7 @@ class RoboFile extends \Robo\Tasks {
|
||||
'tests'
|
||||
];
|
||||
|
||||
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
|
||||
$cmd_parts = $report ? $report_cmd_parts : $normal_cmd_parts;
|
||||
|
||||
$this->_run($cmd_parts);
|
||||
}
|
||||
@ -224,7 +224,7 @@ class RoboFile extends \Robo\Tasks {
|
||||
{
|
||||
$this->lint();
|
||||
$this->taskPHPUnit()
|
||||
->configFile('phpunit.xml')
|
||||
->configFile('build/phpunit.xml')
|
||||
->printed(true)
|
||||
->run();
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ class ByteOrderMarkSniff implements Sniff
|
||||
$fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength));
|
||||
if ($fileStartHex === $expectedBomHex) {
|
||||
$error = "File contains a $bomName byte order mark (BOM).";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'no byte order mark');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -70,15 +70,15 @@ class Utf8EncodingSniff implements Sniff
|
||||
$file_content = file_get_contents($file_path);
|
||||
if (false === mb_check_encoding($file_content, 'UTF-8')) {
|
||||
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
$phpcsFile->addError($error, 0, 'utf8OrDie');
|
||||
}
|
||||
if ( ! self::_checkUtf8W3c($file_content)) {
|
||||
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
$phpcsFile->addError($error, 0, 'utf8OrDie1');
|
||||
}
|
||||
if ( ! self::_checkUtf8Rfc3629($file_content)) {
|
||||
$error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.';
|
||||
$phpcsFile->addError($error, 0);
|
||||
$phpcsFile->addError($error, 0, 'utf8OrDie2');
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
@ -67,11 +67,11 @@ class LogicalOperatorAndSniff implements Sniff
|
||||
$error_message = 'Logical operator should be in upper case;'
|
||||
. ' use "' . strtoupper($operator_string)
|
||||
. '" instead of "' . $operator_string . '"';
|
||||
$phpcsFile->addError($error_message, $stackPtr);
|
||||
$phpcsFile->addError($error_message, $stackPtr, 'uppercase operator');
|
||||
}
|
||||
|
||||
$warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"';
|
||||
$phpcsFile->addWarning($warning_message, $stackPtr);
|
||||
$phpcsFile->addWarning($warning_message, $stackPtr, '&& over AND');
|
||||
|
||||
}//end process()
|
||||
|
||||
|
@ -72,7 +72,7 @@ class StrictComparisonOperatorSniff implements Sniff
|
||||
$error_message = '"==" and "!=" are prohibited; use "'
|
||||
. self::$_replacements[$operator_code] . '" instead of "'
|
||||
. $operator_string . '".';
|
||||
$phpcsFile->addError($error_message, $stackPtr);
|
||||
$phpcsFile->addError($error_message, $stackPtr, 'strict type comparison');
|
||||
}//end process()
|
||||
|
||||
|
||||
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Operators_UppercaseLogicalOperatorOrSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Operators_UppercaseLogicalOperatorOrSniff.
|
||||
*
|
||||
* Ensures that the logical operator 'OR' is in upper cases and its symbolic equivalent.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\Operators;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class UppercaseLogicalOperatorOrSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for: literal and symbolic operators or.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_BOOLEAN_OR,
|
||||
T_LOGICAL_OR,
|
||||
);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$operator_token = $tokens[$stackPtr];
|
||||
$operator_string = $operator_token['content'];
|
||||
$operator_code = $operator_token['code'];
|
||||
|
||||
if ($operator_code == T_BOOLEAN_OR) {
|
||||
$error_message = 'Logical operator "' . $operator_string
|
||||
. '" is prohibited; use "OR" instead';
|
||||
$phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR');
|
||||
}
|
||||
// it is literal, if it is not symbolic
|
||||
else if ($operator_string !== strtoupper($operator_string)) {
|
||||
$error_message = 'Logical operator should be in upper case;'
|
||||
. ' use "' . strtoupper($operator_string)
|
||||
. '" instead of "' . $operator_string . '"';
|
||||
$phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr');
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -18,6 +18,8 @@ namespace CodeIgniter\Sniffs\Strings;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Strings_DoubleQuoteUsageSniff.
|
||||
*
|
||||
@ -104,19 +106,19 @@ class VariableUsageSniff implements Sniff
|
||||
$this->_parseVariable($strTokens, $strPtr);
|
||||
} catch (Exception $err) {
|
||||
$error = 'There is no variable, object nor array between curly braces. Please use the escape char for $ or {.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 1987234);
|
||||
}
|
||||
$variableFound = TRUE;
|
||||
if ('}' !== $strTokens[$strPtr]) {
|
||||
$error = 'There is no matching closing curly brace.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 987234);
|
||||
}
|
||||
// don't move forward, since it will be done in the main loop
|
||||
// $strPtr++;
|
||||
} else if (T_VARIABLE === $strToken[0]) {
|
||||
$variableFound = TRUE;
|
||||
$error = "Variable {$strToken[1]} in double-quoted strings should be enclosed with curly braces. Please consider {{$strToken[1]}}";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 29087234);
|
||||
}
|
||||
}
|
||||
$strPtr++;
|
||||
@ -146,7 +148,7 @@ class VariableUsageSniff implements Sniff
|
||||
if (is_array($strToken)) {
|
||||
if (T_VARIABLE === $strToken[0]) {
|
||||
$error = "Variables like {$strToken[1]} should be in double-quoted strings only.";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 12343);
|
||||
}
|
||||
}
|
||||
$strPtr++;
|
||||
@ -460,5 +462,3 @@ class DoubleQuoteUsageSniff extends VariableUsageSniff
|
||||
}//end _hasSpecificSequence()
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
|
@ -77,7 +77,7 @@ class DisallowSpaceIndentSniff implements Sniff
|
||||
|
||||
if (strpos($tokens[$stackPtr]['content'], " ") !== false) {
|
||||
$error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'indent with tabs!');
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
@ -72,7 +72,7 @@ class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff
|
||||
$isFirst = 0 === $stackPtr;
|
||||
if ( ! $isFirst) {
|
||||
$error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'nothing before opening tag');
|
||||
}
|
||||
} else {
|
||||
// if (T_CLOSE_TAG === $php_tag_code)
|
||||
@ -84,7 +84,7 @@ class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff
|
||||
$containsEndTagOnly = strlen($php_tag_string) > 2;
|
||||
if ( ! $isLast || ! $containsEndTagOnly ) {
|
||||
$error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'nothing after closing tag');
|
||||
}
|
||||
}
|
||||
}//end process()
|
||||
|
@ -71,12 +71,10 @@ class ElseOnNewLineSniff implements Sniff
|
||||
$previous_non_blank_token = $tokens[$previous_non_blank_token_ptr];
|
||||
if ($previous_non_blank_token['line'] === $else_token['line']) {
|
||||
$error = '"' . $else_token['content'] . '" should be on a new line.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'else on new line');
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
}//end class
|
@ -65,7 +65,7 @@ class LogicalNotSpacingSniff implements Sniff
|
||||
$next_token = $tokens[$stackPtr + 1];
|
||||
if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) {
|
||||
$error = 'Logical operator ! should always be preceded and followed with a whitespace.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$phpcsFile->addError($error, $stackPtr, 'Logical Not operator');
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
@ -1,104 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_AbstractClosingCommentSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_AbstractClosingCommentSniff.
|
||||
*
|
||||
* Defines some methods used by
|
||||
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff
|
||||
* and CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\Files;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class AbstractClosingCommentSniff implements Sniff
|
||||
{
|
||||
/**
|
||||
* As an abstract class, this sniff is not associated to any token.
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* As an abstract class, this sniff is not dedicated to process a token.
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$error = __CLASS__.'::'.__METHOD__.' is abstract. Please develop this method in a child class.';
|
||||
throw new PHP_CodeSniffer_Exception($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the comment without its delimiter(s) as well as leading
|
||||
* and traling whitespaces.
|
||||
*
|
||||
* It removes the first #, the two first / (i.e. //) or the first /*
|
||||
* and last \*\/. If a comment starts with /**, then the last * will remain
|
||||
* as well as whitespaces between this star and the comment content.
|
||||
*
|
||||
* @param string $comment Comment containing either comment delimiter(s) and
|
||||
* trailing or leading whitspaces to clean.
|
||||
*
|
||||
* @return string Comment without comment delimiter(s) and whitespaces.
|
||||
*/
|
||||
protected static function _getCommentContent ($comment)
|
||||
{
|
||||
if (self::_stringStartsWith($comment, '#')) {
|
||||
$comment = substr($comment, 1);
|
||||
} else if (self::_stringStartsWith($comment, '//')) {
|
||||
$comment = substr($comment, 2);
|
||||
} else if (self::_stringStartsWith($comment, '/*')) {
|
||||
$comment = substr($comment, 2, strlen($comment) - 2 - 2);
|
||||
}
|
||||
$comment = trim($comment);
|
||||
return $comment;
|
||||
}//_getCommentContent()
|
||||
|
||||
|
||||
/**
|
||||
* Binary safe string comparison between $needle and
|
||||
* the beginning of $haystack. Returns true if $haystack starts with
|
||||
* $needle, false otherwise.
|
||||
*
|
||||
* @param string $haystack The string to search in.
|
||||
* @param string $needle The string to search for.
|
||||
*
|
||||
* @return bool true if $haystack starts with $needle, false otherwise.
|
||||
*/
|
||||
protected static function _stringStartsWith ($haystack, $needle)
|
||||
{
|
||||
$startsWith = false;
|
||||
if (strlen($needle) <= strlen($haystack)) {
|
||||
$haystackBeginning = substr($haystack, 0, strlen($needle));
|
||||
if (0 === strcmp($haystackBeginning, $needle)) {
|
||||
$startsWith = true;
|
||||
}
|
||||
}
|
||||
return $startsWith;
|
||||
}//_stringStartsWith()
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff.
|
||||
*
|
||||
* Ensures that a comment containing the file name is available at the end of file.
|
||||
* Only other comments and whitespaces are allowed to follow this specific comment.
|
||||
*
|
||||
* It may be all kind of comment like multi-line and inline C-style comments as
|
||||
* well as PERL-style comments. Any number of white may separate comment delimiters
|
||||
* from comment content. However, content has to be equal to template
|
||||
* "End of file <file_name>". Comparison between content and template is
|
||||
* case-sensitive.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\Files;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class ClosingFileCommentSniff extends AbstractClosingCommentSniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_OPEN_TAG,
|
||||
);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// We are only interested if this is the first open tag.
|
||||
if ($stackPtr !== 0) {
|
||||
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$fullFilename = $phpcsFile->getFilename();
|
||||
$filename = basename($fullFilename);
|
||||
$commentTemplate = "End of file $filename";
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$currentToken = count($tokens) - 1;
|
||||
$hasClosingFileComment = false;
|
||||
$isNotAWhitespaceOrAComment = false;
|
||||
while ($currentToken >= 0
|
||||
&& ! $isNotAWhitespaceOrAComment
|
||||
&& ! $hasClosingFileComment
|
||||
) {
|
||||
$token = $tokens[$currentToken];
|
||||
$tokenCode = $token['code'];
|
||||
if (T_COMMENT === $tokenCode) {
|
||||
$commentString = self::_getCommentContent($token['content']);
|
||||
if (0 === strcmp($commentString, $commentTemplate)) {
|
||||
$hasClosingFileComment = true;
|
||||
}
|
||||
} else if (T_WHITESPACE === $tokenCode) {
|
||||
// Whitespaces are allowed between the closing file comment,
|
||||
// other comments and end of file
|
||||
} else {
|
||||
$isNotAWhitespaceOrAComment = true;
|
||||
}
|
||||
$currentToken--;
|
||||
}
|
||||
|
||||
if ( ! $hasClosingFileComment) {
|
||||
$error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".';
|
||||
$phpcsFile->addError($error, $currentToken);
|
||||
}
|
||||
}//end process()
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,182 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff.
|
||||
*
|
||||
* Ensures that a comment containing the file location exists at the end of file.
|
||||
* Only other comments and whitespaces are allowed between this comment and
|
||||
* the end of file.
|
||||
*
|
||||
* It may be all kind of comment like multi-line and inline C-style comments as
|
||||
* well as PERL-style comments. Any number of white may separate comment delimiters
|
||||
* from comment content. However, content has to be equal to template
|
||||
* "Location: <file_path_relative_to_application_root>".
|
||||
* Comparison between content and template is case-sensitive.
|
||||
*
|
||||
* There are several ways to configure the application root. In order of priority :
|
||||
* - Configuration variable ci_application_root.
|
||||
* - Rule property applicationRoot.
|
||||
* - Default value '/application/'
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2006 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\Files;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Util\Common;
|
||||
|
||||
class ClosingLocationCommentSniff extends AbstractClosingCommentSniff
|
||||
{
|
||||
public $applicationRoot = '/application/';
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_OPEN_TAG
|
||||
);
|
||||
|
||||
}//end register()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// We are only interested if this is the first open tag.
|
||||
if ($stackPtr !== 0) {
|
||||
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$filePath = $phpcsFile->getFilename();
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
// removes the application root from the beginning of the file path
|
||||
$locationPath = self::_getLocationPath($filePath, $this->_getAppRoot());
|
||||
// add an error, if application root doesn't exist in current file path
|
||||
if (false === $locationPath) {
|
||||
$error = 'Unable to find "' . $this->_getAppRoot() . '" in file path "' . $filePath . '". Please set your project\'s application root.';
|
||||
$phpcsFile->addError($error, count($tokens) - 1);
|
||||
return;
|
||||
}
|
||||
// generates the expected comment
|
||||
$commentTemplate = "Location: $locationPath";
|
||||
|
||||
$currentToken = count($tokens) - 1;
|
||||
$hasClosingLocationComment = false;
|
||||
$isNotAWhitespaceOrAComment = false;
|
||||
while ($currentToken >= 0
|
||||
&& ! $isNotAWhitespaceOrAComment
|
||||
&& ! $hasClosingLocationComment
|
||||
) {
|
||||
$token = $tokens[$currentToken];
|
||||
$tokenCode = $token['code'];
|
||||
if (T_COMMENT === $tokenCode) {
|
||||
$commentString = self::_getCommentContent($token['content']);
|
||||
if (0 === strcmp($commentString, $commentTemplate)) {
|
||||
$hasClosingLocationComment = true;
|
||||
}
|
||||
} else if (T_WHITESPACE === $tokenCode) {
|
||||
// Whitespaces are allowed between the closing file comment,
|
||||
//other comments and end of file
|
||||
} else {
|
||||
$isNotAWhitespaceOrAComment = true;
|
||||
}
|
||||
$currentToken--;
|
||||
}
|
||||
|
||||
if ( ! $hasClosingLocationComment) {
|
||||
$error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".';
|
||||
$phpcsFile->addError($error, $currentToken);
|
||||
}
|
||||
}//end process()
|
||||
|
||||
|
||||
/**
|
||||
* Returns the relative path from $appRoot to $filePath, or false if
|
||||
* $appRoot cannot be found in $filePath, because $appRoot is not a parent
|
||||
* of $filePath.
|
||||
*
|
||||
* @param string $filePath Full path to the file being proceed.
|
||||
* @param string $appRoot Partial or full path to the CodeIgniter
|
||||
* application root of the file being proceed. It must not contain the
|
||||
* full path to the application root, but at least the name of the
|
||||
* application root. Parent directory of the application root are allowed
|
||||
* but not mandatory.
|
||||
*
|
||||
* @return string|bool The relative path from $appRoot to $filePath, or
|
||||
* false if $appRoot cannot be found in $filePath.
|
||||
*/
|
||||
private static function _getLocationPath ($filePath, $appRoot)
|
||||
{
|
||||
// removes the path to application root
|
||||
// from the beginning of the file path
|
||||
$appRootAt = strpos($filePath, $appRoot);
|
||||
if (false === $appRootAt) {
|
||||
return false;
|
||||
}
|
||||
$localPath = substr($filePath, $appRootAt + strlen($appRoot));
|
||||
// ensures the location path to be a relative path starting with "./".
|
||||
if ( ! self::_stringStartsWith($localPath, './')) {
|
||||
$localPath = './' . $localPath;
|
||||
} else if ( ! self::_stringStartsWith($localPath, '.')
|
||||
&& self::_stringStartsWith($localPath, '/')
|
||||
) {
|
||||
$localPath = '.' . $localPath;
|
||||
}
|
||||
return $localPath;
|
||||
}//end _getLocationPath()
|
||||
|
||||
|
||||
/**
|
||||
* Returns the application root that should be used first.
|
||||
*
|
||||
* There are several ways to configure the application root.
|
||||
* In order of priority :
|
||||
* - Configuration variable ci_application_root.
|
||||
* - Rule property applicationRoot.
|
||||
* - Default value '/application/'
|
||||
*
|
||||
* @return string Path to your project application root.
|
||||
*/
|
||||
private function _getAppRoot()
|
||||
{
|
||||
$appRoot = Common::getConfigData('ci_application_root');
|
||||
if (null === $appRoot) {
|
||||
$appRoot = $this->applicationRoot;
|
||||
}
|
||||
return $appRoot;
|
||||
}//end _getAppRoot()
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,142 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@gmail.com>
|
||||
* @copyright 2011 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\NamingConventions;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ConstructorNameSniff.
|
||||
*
|
||||
* Favor PHP 4 constructor syntax, which uses "function ClassName()".
|
||||
* Avoid PHP 5 constructor syntax, which uses "function __construct()".
|
||||
*
|
||||
* @todo Try to avoid overly long and verbose names.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@gmail.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class ConstructorNameSniff extends AbstractScopeSniff
|
||||
{
|
||||
|
||||
|
||||
public $php5Constructors = '1';
|
||||
|
||||
|
||||
/**
|
||||
* Constructs the test with the tokens it wishes to listen for.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
|
||||
|
||||
}//end __construct()
|
||||
|
||||
|
||||
/**
|
||||
* Processes this test when one of its tokens is encountered.
|
||||
*
|
||||
* @param File $phpcsFile The current file being scanned.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param int $currScope A pointer to the start of the scope.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processTokenWithinScope(
|
||||
File $phpcsFile,
|
||||
$stackPtr,
|
||||
$currScope
|
||||
) {
|
||||
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
$className = $phpcsFile->getDeclarationName($currScope);
|
||||
|
||||
$isPhp4Constructor = strcasecmp($methodName, $className) === 0;
|
||||
$isPhp5Constructor = strcasecmp($methodName, '__construct') === 0;
|
||||
if ($this->php5Constructors != '0') {
|
||||
if ($isPhp4Constructor) {
|
||||
$error = "PHP4 style constructors are not allowed; use \"__construct\" instead";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
} else {
|
||||
if ($isPhp5Constructor) {
|
||||
$error = "PHP5 style constructors are not allowed; use \"$className\" instead";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
}
|
||||
if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$parentClassName = $phpcsFile->findExtendedClassName($currScope);
|
||||
$wrongConstructor = '';
|
||||
// prepares the error message and wrong constructor
|
||||
if ($this->php5Constructors != '0') {
|
||||
$error = 'PHP4 style calls to parent constructors are not allowed.';
|
||||
$error = "$error Please use \"parent::__construct\" instead.";
|
||||
if (false !== $parentClassName) {
|
||||
$wrongConstructor = $parentClassName;
|
||||
}
|
||||
// Else $wrongConstructor will be empty
|
||||
// and the test expression will always be false.
|
||||
// It doesn't check that no parent method should be called
|
||||
// when no parent class is defined.
|
||||
} else {
|
||||
$error = 'PHP5 style calls to parent constructors are not allowed.';
|
||||
if (false !== $parentClassName) {
|
||||
$error = "$error Please use \"parent::$parentClassName\" instead.";
|
||||
}
|
||||
$wrongConstructor = '__construct';
|
||||
}
|
||||
|
||||
// looks for the use of a wrong constructor.
|
||||
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
|
||||
$doubleColonIndex = $phpcsFile->findNext(
|
||||
array(T_DOUBLE_COLON),
|
||||
$stackPtr,
|
||||
$endFunctionIndex
|
||||
);
|
||||
while ($doubleColonIndex) {
|
||||
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING
|
||||
&& $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor
|
||||
) {
|
||||
$phpcsFile->addError($error, ($doubleColonIndex + 1));
|
||||
}
|
||||
|
||||
$doubleColonIndex = $phpcsFile->findNext(
|
||||
array(T_DOUBLE_COLON),
|
||||
$doubleColonIndex + 1,
|
||||
$endFunctionIndex
|
||||
);
|
||||
}
|
||||
|
||||
}//end processTokenWithinScope()
|
||||
|
||||
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// TODO: Implement processTokenOutsideScope() method.
|
||||
}
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
|
||||
*
|
||||
* Ensures that class and interface names have their first letter uppercase
|
||||
* and that words are separated with an underscore, and not CamelCased.
|
||||
*
|
||||
* @todo Try to avoid overly long and verbose names in using property rule and
|
||||
* configuration variable to set limits. Have a look at
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\NamingConventions;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class ValidClassNameSniff implements 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 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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// get the class name
|
||||
$className = trim($phpcsFile->getDeclarationName($stackPtr));
|
||||
// compute the expected class name
|
||||
// [^_] means "something different from _", but not "nothing or something different from _"
|
||||
$lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className);
|
||||
$expectedClassName
|
||||
= strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1));
|
||||
// ensures that the current class name
|
||||
// and the expected class name are identical
|
||||
if (0 !== strcmp($className, $expectedClassName)) {
|
||||
$error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.';
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
}//end process()
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2011 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidFileNameSniff.
|
||||
*
|
||||
* Tests that the file name matchs the name of the class that it contains in lower case.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baobaz.com>
|
||||
* @copyright 2011 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\NamingConventions;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class ValidFileNameSniff implements 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 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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
// computes the expected filename based on the name of the class or interface that it contains.
|
||||
$decNamePtr = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
$decName = $tokens[$decNamePtr]['content'];
|
||||
$expectedFileName = strtolower($decName);
|
||||
// extracts filename without extension from its path.
|
||||
$fullPath = $phpcsFile->getFilename();
|
||||
$fileNameAndExt = basename($fullPath);
|
||||
$fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.'));
|
||||
|
||||
if ($expectedFileName !== $fileName) {
|
||||
$errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.';
|
||||
$errorMessage = sprintf(
|
||||
$errorTemplate,
|
||||
$fileName,
|
||||
strtolower($tokens[$stackPtr]['content']), // class or interface
|
||||
$decName,
|
||||
$expectedFileName
|
||||
);
|
||||
$phpcsFile->addError($errorMessage, 0);
|
||||
}
|
||||
}//end process()
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,161 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
|
||||
*
|
||||
* Ensures that class methods and functions areentirely lowercased and that
|
||||
* words are separated with an underscore, and not CamelCased.
|
||||
* Ensures that private class methods are prefixed with an underscore and that
|
||||
* all other methods are not prefixed with an underscored.
|
||||
* Ensures that names longer than 50 chars are prohibited. Likewise names longer
|
||||
* than 35 chars raise a warning.
|
||||
*
|
||||
* @todo Use a rule property or a configuration variable to allow users to set
|
||||
* their own maximum lengths for function and method names. Have a look at
|
||||
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff and application root.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Sniffs\NamingConventions;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\AbstactScopeSniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class ValidMethodNameSniff extends AbstractScopeSniff
|
||||
{
|
||||
/**
|
||||
* A list of all PHP magic methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $magicMethods = array(
|
||||
'construct',
|
||||
'destruct',
|
||||
'call',
|
||||
'callStatic',
|
||||
'get',
|
||||
'set',
|
||||
'isset',
|
||||
'unset',
|
||||
'sleep',
|
||||
'wakeup',
|
||||
'toString',
|
||||
'set_state',
|
||||
'clone',
|
||||
);
|
||||
|
||||
/**
|
||||
* Defines which token(s) in which scope(s) will be proceed.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
|
||||
|
||||
}//end __construct()
|
||||
|
||||
|
||||
/**
|
||||
* Processes the tokens within the scope.
|
||||
*
|
||||
* @param 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(File $phpcsFile, $stackPtr, $currScope)
|
||||
{
|
||||
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
if ($methodName === null) {
|
||||
// Ignore closures.
|
||||
return;
|
||||
}
|
||||
|
||||
$className = $phpcsFile->getDeclarationName($currScope);
|
||||
|
||||
// Is this a magic method i.e. is prefixed with "__".
|
||||
if (0 === strcmp(substr($methodName, 0, 2), '__')) {
|
||||
$magicPart = substr($methodName, 2);
|
||||
if (in_array($magicPart, self::$magicMethods) === false) {
|
||||
$error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (0 !== strcmp($methodName, strtolower($methodName))) {
|
||||
$uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName);
|
||||
$expectedMethodName = strtolower($uscrdMethodName);
|
||||
$error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\".";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
return;
|
||||
}
|
||||
|
||||
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
|
||||
$scope = $methodProps['scope'];
|
||||
$scopeSpecified = $methodProps['scope_specified'];
|
||||
|
||||
// If it's a private method, it must have an underscore on the front.
|
||||
if ($scope === 'private' && $methodName{0} !== '_') {
|
||||
$error = "Private method name \"$className::$methodName\" must be prefixed with an underscore";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
return;
|
||||
}
|
||||
|
||||
// If it's not a private method, it must not have an underscore on the front.
|
||||
if ($scope !== 'private' && $methodName{0} === '_') {
|
||||
if (true === $scopeSpecified) {
|
||||
$error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore";
|
||||
} else {
|
||||
$error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore";
|
||||
}
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
return;
|
||||
}
|
||||
|
||||
// If name is too verbose,
|
||||
// then either an error or a warning is displayed.
|
||||
$error_limit = 50;
|
||||
$warning_limit = 35;
|
||||
if (strlen($methodName) > $error_limit) {
|
||||
$error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars.";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
return;
|
||||
} else if (strlen($methodName) > $warning_limit) {
|
||||
$warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars.";
|
||||
$phpcsFile->addWarning($warning, $stackPtr);
|
||||
}
|
||||
}//end processTokenWithinScope()
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
@ -1,562 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
/**
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidVariableNameSniff.
|
||||
*
|
||||
* Ensures that variable names contain only lowercase letters,
|
||||
* use underscore separators.
|
||||
* Ensures that class attribute names are prefixed with an underscore,
|
||||
* only when they are private.
|
||||
* Ensure that variable names are longer than 3 chars except those declared
|
||||
* in for loops.
|
||||
*
|
||||
* @todo Try to avoid overly long and verbose names in using property rule and
|
||||
* configuration variable to set limits. Have a look at
|
||||
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
|
||||
* @todo Use a property rule or a configuration variable to allow users to set
|
||||
* minimum variable name length. Have a look at
|
||||
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff and application root.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Thomas Ernest <thomas.ernest@baoabz.com>
|
||||
* @copyright 2010 Thomas Ernest
|
||||
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
namespace CodeIgniter\Sniffs\NamingConventions;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
|
||||
class ValidVariableNameSniff extends AbstractVariableSniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Processes class member variables.
|
||||
*
|
||||
* @param 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(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// get variable name and properties
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$varTk = $tokens[$stackPtr];
|
||||
$varName = substr($varTk['content'], 1);
|
||||
$varProps = $phpcsFile->getMemberProperties($stackPtr);
|
||||
// check(s)
|
||||
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName) ) {
|
||||
return;
|
||||
}
|
||||
if ( ! $this->checkVisibilityPrefix($phpcsFile, $stackPtr, $varName, $varProps)) {
|
||||
return;
|
||||
}
|
||||
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
}//end processMemberVar()
|
||||
|
||||
|
||||
/**
|
||||
* Processes normal variables.
|
||||
*
|
||||
* @param File $phpcsFile The file where this token was found.
|
||||
* @param int $stackPtr The position where the token was found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processVariable(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// get variable name
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$varTk = $tokens[$stackPtr];
|
||||
$varName = substr($varTk['content'], 1);
|
||||
// skip the current object variable, i.e. $this
|
||||
if (0 === strcmp($varName, 'this')) {
|
||||
return;
|
||||
}
|
||||
// check(s)
|
||||
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) {
|
||||
return;
|
||||
}
|
||||
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
}//end processVariable()
|
||||
|
||||
|
||||
/**
|
||||
* Processes variables in double quoted strings.
|
||||
*
|
||||
* @param File $phpcsFile The file where this token was found.
|
||||
* @param int $stackPtr The position where the token was found.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function processVariableInString(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$stringTk = $tokens[$stackPtr];
|
||||
$stringString = $stringTk['content'];
|
||||
$varAt = self::_getVariablePosition($stringString, 0);
|
||||
while (false !== $varAt) {
|
||||
// get variable name
|
||||
$matches = array();
|
||||
preg_match('/^\$\{?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}?/', substr($stringString, $varAt), $matches);
|
||||
$varName = $matches[1];
|
||||
// check(s)
|
||||
if ( ! $this->checkLowerCase($phpcsFile, $stackPtr, $varName)) {
|
||||
return;
|
||||
}
|
||||
if ( ! $this->checkLength($phpcsFile, $stackPtr, $varName)) {
|
||||
return;
|
||||
}
|
||||
// prepare checking next variable
|
||||
$varAt = self::_getVariablePosition($stringString, $varAt + 1);
|
||||
}
|
||||
|
||||
}//end processVariableInString()
|
||||
|
||||
|
||||
/**
|
||||
* Checks that the variable name is all in lower case, else it add an error
|
||||
* to $phpcsFile. Returns true if variable name is all in lower case, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param File $phpcsFile The current file being processed.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param string $varName The name of the variable to
|
||||
* procced without $, { nor }.
|
||||
*
|
||||
* @return bool true if variable name is all in lower case, false otherwise.
|
||||
*/
|
||||
protected function checkLowerCase(File $phpcsFile, $stackPtr, $varName)
|
||||
{
|
||||
$isInLowerCase = true;
|
||||
if (0 !== strcmp($varName, strtolower($varName))) {
|
||||
// get the expected variable name
|
||||
$varNameWithUnderscores = preg_replace('/([A-Z])/', '_${1}', $varName);
|
||||
$expectedVarName = strtolower(ltrim($varNameWithUnderscores, '_'));
|
||||
// adapts the error message to the error case
|
||||
if (strlen($varNameWithUnderscores) > strlen($varName)) {
|
||||
$error = 'Variables should not use CamelCasing or start with a Capital.';
|
||||
} else {
|
||||
$error = 'Variables should be entirely lowercased.';
|
||||
}
|
||||
$error = $error . 'Please consider "' . $expectedVarName
|
||||
. '" instead of "' . $varName . '".';
|
||||
// adds the error and changes return value
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$isInLowerCase = false;
|
||||
}
|
||||
return $isInLowerCase;
|
||||
}//end checkLowerCase()
|
||||
|
||||
/**
|
||||
* Checks that an underscore is used at the beginning of a variable only if
|
||||
* it is about a private variable. If it isn't a private variable, then it
|
||||
* must not be prefixed with an underscore. Returns true if $varName is
|
||||
* properly prefixed according to the variable visibility provided in
|
||||
* $varProps, false otherwise.
|
||||
*
|
||||
* @param File $phpcsFile The current file being processed.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param string $varName The name of the variable to
|
||||
* procced without $, { nor }.
|
||||
* @param array $varProps Member variable properties like
|
||||
* its visibility.
|
||||
*
|
||||
* @return bool true if variable name is prefixed with an underscore only
|
||||
* when it is about a private variable, false otherwise.
|
||||
*/
|
||||
protected function checkVisibilityPrefix(File $phpcsFile, $stackPtr, $varName, $varProps)
|
||||
{
|
||||
$isVisibilityPrefixRight = true;
|
||||
$scope = $varProps['scope'];
|
||||
// If it's a private variable, it must have an underscore on the front.
|
||||
if ($scope === 'private' && $varName{0} !== '_') {
|
||||
$error = "Private variable name \"$varName\" must be prefixed with an underscore";
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$isVisibilityPrefixRight = false;
|
||||
} else if ($scope !== 'private' && $varName{0} === '_') {
|
||||
// If it's not a private variable,
|
||||
// then it must not start with an underscore.
|
||||
if (isset ($scopeSpecified) && true === $scopeSpecified) {
|
||||
$error = "Public variable name \"$varName\" must not be prefixed with an underscore";
|
||||
} else {
|
||||
$error = ucfirst($scope) . " variable name \"$varName\" must not be prefixed with an underscore";
|
||||
}
|
||||
$phpcsFile->addError($error, $stackPtr);
|
||||
$isVisibilityPrefixRight = false;
|
||||
}
|
||||