diff --git a/PSR12NeutronRuleset/Sniffs/NamingConventions/MeaningfulVariableNameSniff.php b/PSR12NeutronRuleset/Sniffs/NamingConventions/MeaningfulVariableNameSniff.php new file mode 100644 index 0000000..ceca02d --- /dev/null +++ b/PSR12NeutronRuleset/Sniffs/NamingConventions/MeaningfulVariableNameSniff.php @@ -0,0 +1,116 @@ + + */ + public array $forbiddenNames = []; + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int|string $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + protected function processVariable(File $phpcsFile, $stackPtr): void + { + $tokens = $phpcsFile->getTokens(); + $varName = ltrim($tokens[$stackPtr]['content'], '$'); + + if ($this->checkForProhibitedVariableNames($varName)) { + $error = 'Variable "%s" has not very meaningful, searchable or pronounceable name'; + $phpcsFile->addError($error, $stackPtr, 'NotMeaningfulVariableName', [$varName]); + return; + } + } + + /** + * Processes class member variables. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int|string $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + protected function processMemberVar(File $phpcsFile, $stackPtr): void + { + $tokens = $phpcsFile->getTokens(); + $varName = ltrim($tokens[$stackPtr]['content'], '$'); + + if ($this->checkForProhibitedVariableNames($varName)) { + $error = 'Variable "%s" has not very meaningful, searchable or pronounceable name'; + $phpcsFile->addError($error, $stackPtr, 'NotMeaningfulVariableName', [$varName]); + return; + } + } + + /** + * Processes the variable found within a double-quoted string. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int|string $stackPtr The position of the double-quoted string. + * + * @return void + */ + protected function processVariableInString(File $phpcsFile, $stackPtr): void + { + $tokens = $phpcsFile->getTokens(); + + 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 it's ok. + if (isset($this->phpReservedVars[$varName])) { + continue; + } + + if ($this->checkForProhibitedVariableNames($varName)) { + $error = 'Variable "%s" has not very meaningful or searchable name'; + $phpcsFile->addError($error, $stackPtr, 'NotMeaningfulVariableName', [$varName]); + return; + } + } + } + } + + /** + * Check variable name. + * + * @param string $varName Variable name to check. + * + * @return bool + */ + private function checkForProhibitedVariableNames(string $varName): bool + { + return isset($this->forbiddenNames[$varName]); + } +} diff --git a/PSR12NeutronRuleset/Sniffs/Strings/ConcatenationUsageSniff.php b/PSR12NeutronRuleset/Sniffs/Strings/ConcatenationUsageSniff.php index d0432e4..323c7ff 100644 --- a/PSR12NeutronRuleset/Sniffs/Strings/ConcatenationUsageSniff.php +++ b/PSR12NeutronRuleset/Sniffs/Strings/ConcatenationUsageSniff.php @@ -6,6 +6,8 @@ * @package PSR12NeutronRuleset */ +declare(strict_types=1); + namespace PSR12NeutronRuleset\Sniffs\Strings; use PHP_CodeSniffer\Files\File; @@ -30,8 +32,8 @@ public function register() /** * Process sniff. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile 1 - * @param int $stackPtr 2 + * @param \PHP_CodeSniffer\Files\File $phpcsFile File name. + * @param int $stackPtr Stack pointer. * * @return void */ diff --git a/PSR12NeutronRuleset/ruleset.xml b/PSR12NeutronRuleset/ruleset.xml index 1c67fd4..39cafd5 100644 --- a/PSR12NeutronRuleset/ruleset.xml +++ b/PSR12NeutronRuleset/ruleset.xml @@ -235,9 +235,50 @@ warning - + 5 warning + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +