aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpcs/OpenCart/Sniffs/Variables
diff options
context:
space:
mode:
authorJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
committerJesús <heckyel@hyperbola.info>2019-08-18 21:14:58 -0500
commit2eed7b082f83630301e51f57ca8394de228a8605 (patch)
tree1d19962d22d30f99317d9276e4bae7744fc93fc2 /tests/phpcs/OpenCart/Sniffs/Variables
downloadlibrecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.lz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.xz
librecart-2eed7b082f83630301e51f57ca8394de228a8605.zip
first commit
Diffstat (limited to 'tests/phpcs/OpenCart/Sniffs/Variables')
-rw-r--r--tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php209
1 files changed, 209 insertions, 0 deletions
diff --git a/tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php b/tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php
new file mode 100644
index 0000000..0e5be78
--- /dev/null
+++ b/tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php
@@ -0,0 +1,209 @@
+<?php
+/**
+ * Validates variable names.
+ *
+ * @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://opensource.org/licenses/BSD-3-Clause
+ * @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');
+}
+
+/**
+ * Squiz_Sniffs_NamingConventions_ValidVariableNameSniff.
+ *
+ * Checks the naming of variables and member variables.
+ *
+ * @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 OpenCart_Sniffs_Variables_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff {
+
+ /**
+ * Tokens to ignore so that we can find a DOUBLE_COLON.
+ *
+ * @var array
+ */
+ private $_ignore = array(
+ T_WHITESPACE,
+ T_COMMENT,
+ );
+
+ /**
+ * Regex to match valid underscore names for variables
+ *
+ * @var string
+ */
+ private static $underscore_var = '/^(_|[a-z](?:_?[a-z0-9]+)*)$/';
+
+ /**
+ * Complementary regex to just exclude camel casing
+ * @var string
+ */
+ private static $camelcase = '/[a-z][A-Z]/';
+
+ /**
+ * 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
+ */
+ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
+ $tokens = $phpcsFile->getTokens();
+ $varName = ltrim($tokens[$stackPtr]['content'], '$');
+
+ $phpReservedVars = array(
+ '_SERVER',
+ '_GET',
+ '_POST',
+ '_REQUEST',
+ '_SESSION',
+ '_ENV',
+ '_COOKIE',
+ '_FILES',
+ 'GLOBALS',
+ );
+
+ // If it's a php reserved var, then its ok.
+ if (in_array($varName, $phpReservedVars) === true) {
+ return;
+ }
+
+ if ($tokens[$stackPtr - 1]['code'] === T_PAAMAYIM_NEKUDOTAYIM) {
+ // static vars just ensure no camelcase (caps allowed)
+ if (preg_match(self::$camelcase, $varName)) {
+ $error = 'Variable "%s" is not in valid underscore format';
+ $phpcsFile->addError($error, $stackPtr, 'NotUnderscore', array($varName));
+ }
+ return;
+ }
+
+ $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
+ if ($tokens[$objOperator]['code'] === T_OBJECT_OPERATOR) {
+ // Check to see if we are using a variable from an object.
+ $var = $phpcsFile->findNext(array(T_WHITESPACE), ($objOperator + 1), null, true);
+ if ($tokens[$var]['code'] === T_STRING) {
+ $bracket = $objOperator = $phpcsFile->findNext(array(T_WHITESPACE), ($var + 1), null, true);
+ if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
+ $objVarName = $tokens[$var]['content'];
+
+ if (preg_match(self::$underscore_var, $objVarName) === 0) {
+ $phpcsFile->addError(
+ 'Variable "%s" is not in valid underscore format',
+ $var,
+ 'NotUnderscore',
+ array($objVarName)
+ );
+ }
+ }//end if
+ }//end if
+ }//end if
+
+ if (preg_match(self::$underscore_var, $varName) === 0) {
+ $error = 'Variable "%s" is not in valid underscore format';
+ $phpcsFile->addError($error, $stackPtr, 'NotUnderscore', array($varName));
+ }
+ }//end processVariable()
+
+ /**
+ * 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();
+
+ $varName = ltrim($tokens[$stackPtr]['content'], '$');
+ $memberProps = $phpcsFile->getMemberProperties($stackPtr);
+ if (empty($memberProps) === true) {
+ // Couldn't get any info about this variable, which
+ // generally means it is invalid or possibly has a parse
+ // error. Any errors will be reported by the core, so
+ // we can ignore it.
+ return;
+ }
+
+ $errorData = array($varName);
+
+ if (
+ ($memberProps['is_static'] && preg_match(self::$camelcase, $varName))
+ || (!$memberProps['is_static'] && preg_match(self::$underscore_var, $varName) === 0)
+ ) {
+ $error = 'Variable "%s" is not in valid underscore format';
+ $phpcsFile->addError($error, $stackPtr, 'MemberNotUnderscore', $errorData);
+ }
+
+ }//end processMemberVar()
+
+
+ /**
+ * Processes the variable found within a double quoted string.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the double quoted
+ * string.
+ *
+ * @return void
+ */
+ protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
+ $tokens = $phpcsFile->getTokens();
+
+ $phpReservedVars = array(
+ '_SERVER',
+ '_GET',
+ '_POST',
+ '_REQUEST',
+ '_SESSION',
+ '_ENV',
+ '_COOKIE',
+ '_FILES',
+ 'GLOBALS',
+ );
+ if (preg_match_all('|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
+ foreach ($matches[1] as $varName) {
+ // If it's a php reserved var, then its ok.
+ if (in_array($varName, $phpReservedVars) === true) {
+ continue;
+ }
+
+ // There is no way for us to know if the var is public or private,
+ // so we have to ignore a leading underscore if there is one and just
+ // check the main part of the variable name.
+ $originalVarName = $varName;
+ if (substr($varName, 0, 1) === '_') {
+ if ($phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE)) === true) {
+ $varName = substr($varName, 1);
+ }
+ }
+
+ if (preg_match(self::$underscore_var, $varName) === 0) {
+ $varName = $matches[0];
+ $error = 'Variable "%s" is not in valid underscore format';
+ $data = array($originalVarName);
+ $phpcsFile->addError($error, $stackPtr, 'StringNotUnderscore', $data);
+
+ }
+ }
+ }//end if
+
+ }//end processVariableInString()
+}//end class \ No newline at end of file