diff options
author | Jesús <heckyel@hyperbola.info> | 2019-08-18 21:14:58 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-08-18 21:14:58 -0500 |
commit | 2eed7b082f83630301e51f57ca8394de228a8605 (patch) | |
tree | 1d19962d22d30f99317d9276e4bae7744fc93fc2 /tests/phpcs/OpenCart | |
download | librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.lz librecart-2eed7b082f83630301e51f57ca8394de228a8605.tar.xz librecart-2eed7b082f83630301e51f57ca8394de228a8605.zip |
first commit
Diffstat (limited to 'tests/phpcs/OpenCart')
-rw-r--r-- | tests/phpcs/OpenCart/IDE/README.md | 11 | ||||
-rw-r--r-- | tests/phpcs/OpenCart/IDE/opencart_phpstorm.xml | 22 | ||||
-rw-r--r-- | tests/phpcs/OpenCart/Sniffs/Spacing/ConcatenationSniff.php | 34 | ||||
-rw-r--r-- | tests/phpcs/OpenCart/Sniffs/Spacing/OpenBracketSpacingSniff.php | 69 | ||||
-rw-r--r-- | tests/phpcs/OpenCart/Sniffs/Variables/ValidVariableNameSniff.php | 209 | ||||
-rw-r--r-- | tests/phpcs/OpenCart/ruleset.xml | 102 |
6 files changed, 447 insertions, 0 deletions
diff --git a/tests/phpcs/OpenCart/IDE/README.md b/tests/phpcs/OpenCart/IDE/README.md new file mode 100644 index 0000000..6bdc746 --- /dev/null +++ b/tests/phpcs/OpenCart/IDE/README.md @@ -0,0 +1,11 @@ +## IDE auto formatters + +* PHPStorm +Copy the file ```opencart_phpstorm.xml``` to: + - Mac ```~/Library/Preferences/<PRODUCT><VERSION>/codestyles/OpenCart.xml``` + - Windows ```<User home>\.<PRODUCT><VERSION>\config\codestyles\OpenCart.xml``` + - Linux ```~/.<PRODUCT><VERSION>/config/codestyles/OpenCart.xml``` + +### References +* PHPStorm https://www.jetbrains.com/phpstorm/webhelp/code-style-xml.html +* PHPStorm https://www.jetbrains.com/help/phpstorm/2016.1/directories-used-by-phpstorm-to-store-settings-caches-plugins-and-logs.html diff --git a/tests/phpcs/OpenCart/IDE/opencart_phpstorm.xml b/tests/phpcs/OpenCart/IDE/opencart_phpstorm.xml new file mode 100644 index 0000000..d1ab9b9 --- /dev/null +++ b/tests/phpcs/OpenCart/IDE/opencart_phpstorm.xml @@ -0,0 +1,22 @@ +<code_scheme name="OpenCart"> + <PHPCodeStyleSettings> + <option name="ALIGN_KEY_VALUE_PAIRS" value="true" /> + <option name="ALIGN_PHPDOC_PARAM_NAMES" value="true" /> + <option name="ALIGN_PHPDOC_COMMENTS" value="true" /> + <option name="PHPDOC_BLANK_LINES_AROUND_PARAMETERS" value="true" /> + <option name="UPPER_CASE_BOOLEAN_CONST" value="true" /> + <option name="UPPER_CASE_NULL_CONST" value="true" /> + <option name="BLANK_LINE_BEFORE_RETURN_STATEMENT" value="true" /> + <option name="ALIGN_CLASS_CONSTANTS" value="true" /> + </PHPCodeStyleSettings> + <codeStyleSettings language="PHP"> + <option name="CLASS_BRACE_STYLE" value="1" /> + <option name="METHOD_BRACE_STYLE" value="1" /> + <option name="SPECIAL_ELSE_IF_TREATMENT" value="true" /> + <option name="IF_BRACE_FORCE" value="3" /> + <indentOptions> + <option name="USE_TAB_CHARACTER" value="true" /> + <option name="SMART_TABS" value="true" /> + </indentOptions> + </codeStyleSettings> +</code_scheme>
\ No newline at end of file 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 diff --git a/tests/phpcs/OpenCart/ruleset.xml b/tests/phpcs/OpenCart/ruleset.xml new file mode 100644 index 0000000..7823ffb --- /dev/null +++ b/tests/phpcs/OpenCart/ruleset.xml @@ -0,0 +1,102 @@ +<?xml version="1.0"?> +<ruleset name="OpenCart"> + <description>The Code Sniffer rule set for OpenCart</description> + + <exclude-pattern>*/system/vendor/*</exclude-pattern> + + <rule ref="Squiz.ControlStructures.InlineIfDeclaration" /> + + <!-- Closing PHP tags are not allowed --> + <rule ref="Zend.Files.ClosingTag"> + <severity>5</severity> + <type>error</type> + </rule> + + <!-- Test the spacing of inline control statements --> + <rule ref="Squiz.ControlStructures.InlineIfDeclaration" /> + + <!-- Check for whitespace after lines of code and checks for spaces/indents on empty lines --> + <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"> + <severity>1</severity> + <type>warning</type> + </rule> + <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile" /> + <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile" /> + <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines" /> + + <!-- Check to ensure no PHP deprecated functions have been used --> + <rule ref="Generic.PHP.DeprecatedFunctions"> + <severity>5</severity> + <type>error</type> + </rule> + + <!-- PHP opening tag must be full <?php, no shorthand or ASP tags --> + <rule ref="Generic.PHP.DisallowShortOpenTag"> + <severity>5</severity> + <type>error</type> + </rule> + + <!-- In PHP files make sure there is no character before the opening tag --> + <rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/> + + <!-- true, false, null etc should all be lowercase only --> + <rule ref="Generic.PHP.LowerCaseConstant" /> + + <!-- Type casting should be immediately followed by the variable, no space --> + <rule ref="Generic.Formatting.NoSpaceAfterCast" /> + + <!-- Pass by reference is now only supported in the method/function params --> + <rule ref="Generic.Functions.CallTimePassByReference" /> + + <!-- keep the spacing between function/method params space after comma --> + <rule ref="Generic.Functions.FunctionCallArgumentSpacing" /> + + <!-- method names should always be camel case --> + <rule ref="Generic.NamingConventions.CamelCapsFunctionName"/> + + <!-- constants should always be uppercase --> + <rule ref="Generic.NamingConventions.UpperCaseConstantName"/> + + <!-- Detect BOMs to avoid curruptions --> + <rule ref="Generic.Files.ByteOrderMark"/> + + <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"> + <exclude-pattern>*.tpl</exclude-pattern> + <exclude-pattern>*.css</exclude-pattern> + <exclude-pattern>*.html</exclude-pattern> + <exclude-pattern>*.ini</exclude-pattern> + <exclude-pattern>*.txt</exclude-pattern> + <severity>1</severity> + <type>warning</type> + </rule> + + <!-- To do comments should be reported and completed --> + <rule ref="Generic.Commenting.Todo.CommentFound"> + <message>Please review this TODO comment: %s</message> + <severity>3</severity> + <type>warning</type> + </rule> + + <!-- Fix me comments should be reported and fixed --> + <rule ref="Generic.Commenting.Todo.Fixme"> + <message>Please review this FIXME comment: %s</message> + <severity>5</severity> + <type>warning</type> + </rule> + + <!-- Check that line endings are only \n --> + <rule ref="Generic.Files.LineEndings"> + <properties> + <property name="eolChar" value="\n"/> + </properties> + </rule> + + <!-- <rule ref="Squiz.ControlStructures.ControlSignature" /> --> + <!-- <rule ref="Generic.ControlStructures.InlineControlStructure"></rule> --> + + <!-- exclude the actual tests directory from being tested! --> + <exclude-pattern>*/tests/*</exclude-pattern> +</ruleset> + +<!-- @todo - A Sniff test needs to be added to ensure short echo tags are not used --> +<!-- @todo - A Sniff test to allow helper functions (normal functions) to be snake case -->
\ No newline at end of file |