-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce MeaningfulVariableNameSniff
- Loading branch information
1 parent
0def324
commit 05502dd
Showing
3 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
PSR12NeutronRuleset/Sniffs/NamingConventions/MeaningfulVariableNameSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/** | ||
* Prohibit usage of bad variable names. | ||
* | ||
* @package PSR12NeutronRuleset | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PSR12NeutronRuleset\Sniffs\NamingConventions; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; | ||
|
||
/** | ||
* Checks the meaningful naming of variables and member variables. | ||
* | ||
* Modified copy from https://github.com/InteractionDesignFoundation/coding-standard | ||
*/ | ||
final class MeaningfulVariableNameSniff extends AbstractVariableSniff | ||
{ | ||
|
||
/** | ||
* Forbidden variable names. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
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鈥檚 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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters