aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpcs/OpenCart/Sniffs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpcs/OpenCart/Sniffs')
-rw-r--r--tests/phpcs/OpenCart/Sniffs/Spacing/ConcatenationSniff.php34
-rw-r--r--tests/phpcs/OpenCart/Sniffs/Spacing/OpenBracketSpacingSniff.php69
-rw-r--r--tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php209
3 files changed, 312 insertions, 0 deletions
diff --git a/tests/phpcs/OpenCart/Sniffs/Spacing/ConcatenationSniff.php b/tests/phpcs/OpenCart/Sniffs/Spacing/ConcatenationSniff.php
new file mode 100644
index 0000000..432c921
--- /dev/null
+++ b/tests/phpcs/OpenCart/Sniffs/Spacing/ConcatenationSniff.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Makes sure there are the needed spaces between the concatenation operator (.) and
+ * the strings being concatenated.
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Peter Philipp <peter.philipp@cando-image.com>
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ * @Licence http://www.gnu.org/licenses/gpl-2.0.html
+ */
+class OpenCart_Sniffs_Spacing_ConcatenationSniff implements PHP_CodeSniffer_Sniff {
+ /**
+ * Returns an array of tokens this test wants to listen for.
+ *
+ * @return array
+ */
+ public function register() {
+ return array(T_STRING_CONCAT);
+
+ }//end register()
+
+
+ /**
+ * Processes this test, when one of its tokens is encountered.
+ */
+ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
+ $tokens = $phpcsFile->getTokens();
+ if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE || $tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
+ $message = 'PHP concat operator must be surrounded by spaces';
+ $phpcsFile->addError($message, $stackPtr, 'Missing');
+ }
+ }
+}//end class \ No newline at end of file
diff --git a/tests/phpcs/OpenCart/Sniffs/Spacing/OpenBracketSpacingSniff.php b/tests/phpcs/OpenCart/Sniffs/Spacing/OpenBracketSpacingSniff.php
new file mode 100644
index 0000000..924b873
--- /dev/null
+++ b/tests/phpcs/OpenCart/Sniffs/Spacing/OpenBracketSpacingSniff.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Checks that there is no white space after an opening bracket, for "(" and "{".
+ * Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ * @Licence http://www.gnu.org/licenses/gpl-2.0.html
+ */
+class OpenCart_Sniffs_Spacing_OpenBracketSpacingSniff 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_OPEN_CURLY_BRACKET,
+ T_OPEN_PARENTHESIS,
+ );
+
+ }//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();
+
+ // Ignore curly brackets in javascript files.
+ if ($tokens[$stackPtr]['code'] === T_OPEN_CURLY_BRACKET
+ && $phpcsFile->tokenizerType === 'JS'
+ ) {
+ return;
+ }
+
+ if (isset($tokens[($stackPtr + 1)]) === true
+ && $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
+ && strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false
+ ) {
+ $error = 'There should be no white space after an opening "%s"';
+ $phpcsFile->addError(
+ $error,
+ ($stackPtr + 1),
+ 'OpeningWhitespace',
+ array($tokens[$stackPtr]['content'])
+ );
+ }
+
+ }//end process()
+}//end class \ No newline at end of file
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