-
Notifications
You must be signed in to change notification settings - Fork 4
Allow to specify pattern in ValidVariableNameSniff #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
eb8db2e
Port ValidVariableNameSniff
simPod 8cbe101
Apply CS so something can be coded, actually. Also, use `strpos()` an…
simPod 6a1ed60
Refactor ValidVariableNameSniffTest for better maintenance
simPod 6e4a2bd
Add two test errors on line 150
simPod 38b50a9
Use regex pattern instead of `Common::isCamelCaps()` - parity with or…
simPod 821b348
Allow names marking unused variable (`$_`, `$__` etc.)
simPod 4457275
Include Cdn77.NamingConventions.ValidVariableName in ruleset
simPod 641462e
Remove PrivateNoUnderscore since it's ancient and nobody writes php l…
simPod 278cc68
Do not juggle with stripping `_`. Just don't use it for prefixing. We…
simPod ce7eda7
Use baseline to ignore a bug in loading constant from phpcs
simPod af69a6e
Rename error codes and use proper messages. Also allow specifying pat…
simPod 1fb8d2f
Method doc culture
simPod f7ce1a3
Cleanup test
simPod 8aeb62c
Add potato case
simPod 81b865c
Allow unused vars only in DoesNotMatchPattern
simPod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,12 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Constant T_OPEN_PARENTHESIS not found\\.$#" | ||
count: 1 | ||
path: src/Cdn77/Sniffs/NamingConventions/ValidVariableNameSniff.php | ||
|
||
- | ||
message: "#^Used constant T_OPEN_PARENTHESIS not found\\.$#" | ||
count: 1 | ||
path: src/Cdn77/Sniffs/NamingConventions/ValidVariableNameSniff.php | ||
|
This file contains hidden or 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
172 changes: 172 additions & 0 deletions
172
src/Cdn77/Sniffs/NamingConventions/ValidVariableNameSniff.php
This file contains hidden or 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,172 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cdn77\Sniffs\NamingConventions; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; | ||
|
||
use function assert; | ||
use function ltrim; | ||
use function preg_match; | ||
use function preg_match_all; | ||
use function sprintf; | ||
|
||
use const T_DOUBLE_COLON; | ||
use const T_NULLSAFE_OBJECT_OPERATOR; | ||
use const T_OBJECT_OPERATOR; | ||
use const T_OPEN_PARENTHESIS; | ||
use const T_STRING; | ||
use const T_WHITESPACE; | ||
|
||
class ValidVariableNameSniff extends AbstractVariableSniff | ||
{ | ||
public const CODE_DOES_NOT_MATCH_PATTERN = 'DoesNotMatchPattern'; | ||
public const CODE_MEMBER_DOES_NOT_MATCH_PATTERN = 'MemberDoesNotMatchPattern'; | ||
public const CODE_STRING_DOES_NOT_MATCH_PATTERN = 'StringDoesNotMatchPattern'; | ||
private const PATTERN_CAMEL_CASE = '\b([a-zA-Z][a-zA-Z0-9]*?([A-Z][a-zA-Z0-9]*?)*?)\b'; | ||
private const PATTERN_CAMEL_CASE_OR_UNUSED = '\b(([a-zA-Z][a-zA-Z0-9]*?([A-Z][a-zA-Z0-9]*?)*?)|_+)\b'; | ||
|
||
public string $pattern = self::PATTERN_CAMEL_CASE_OR_UNUSED; | ||
public string $memberPattern = self::PATTERN_CAMEL_CASE; | ||
public string $stringPattern = self::PATTERN_CAMEL_CASE; | ||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the stack passed in $tokens. | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
*/ | ||
protected function processVariable(File $phpcsFile, $stackPtr): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$varName = ltrim($tokens[$stackPtr]['content'], '$'); | ||
|
||
// If it's a php reserved var, then its ok. | ||
if (isset($this->phpReservedVars[$varName]) === true) { | ||
return; | ||
} | ||
|
||
$objOperator = $phpcsFile->findNext([T_WHITESPACE], $stackPtr + 1, null, true); | ||
assert($objOperator !== false); | ||
|
||
if ( | ||
$tokens[$objOperator]['code'] === T_OBJECT_OPERATOR | ||
|| $tokens[$objOperator]['code'] === T_NULLSAFE_OBJECT_OPERATOR | ||
) { | ||
// Check to see if we are using a variable from an object. | ||
$var = $phpcsFile->findNext([T_WHITESPACE], $objOperator + 1, null, true); | ||
assert($var !== false); | ||
|
||
if ($tokens[$var]['code'] === T_STRING) { | ||
$bracket = $phpcsFile->findNext([T_WHITESPACE], $var + 1, null, true); | ||
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) { | ||
$objVarName = $tokens[$var]['content']; | ||
|
||
if (! $this->matchesRegex($objVarName, $this->memberPattern)) { | ||
$error = sprintf('Member variable "%%s" does not match pattern "%s"', $this->memberPattern); | ||
baci266 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$data = [$objVarName]; | ||
$phpcsFile->addError($error, $var, self::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, $data); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true); | ||
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) { | ||
if (! $this->matchesRegex($varName, $this->memberPattern)) { | ||
$error = sprintf('Member variable "%%s" does not match pattern "%s"', $this->memberPattern); | ||
$data = [$tokens[$stackPtr]['content']]; | ||
$phpcsFile->addError($error, $stackPtr, self::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, $data); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ($this->matchesRegex($varName, $this->pattern)) { | ||
return; | ||
} | ||
|
||
$error = sprintf('Variable "%%s" does not match pattern "%s"', $this->pattern); | ||
$data = [$varName]; | ||
$phpcsFile->addError($error, $stackPtr, self::CODE_DOES_NOT_MATCH_PATTERN, $data); | ||
} | ||
|
||
/** | ||
* Processes class member variables. | ||
* | ||
* @param File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in the stack passed in $tokens. | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
*/ | ||
protected function processMemberVar(File $phpcsFile, $stackPtr): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$varName = ltrim($tokens[$stackPtr]['content'], '$'); | ||
$memberProps = $phpcsFile->getMemberProperties($stackPtr); | ||
if ($memberProps === []) { | ||
// 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 = [$varName]; | ||
|
||
if ($this->matchesRegex($varName, $this->memberPattern)) { | ||
return; | ||
} | ||
|
||
$error = sprintf('Member variable "%%s" does not match pattern "%s"', $this->memberPattern); | ||
$phpcsFile->addError($error, $stackPtr, self::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, $errorData); | ||
} | ||
|
||
/** | ||
* Processes the variable found within a double quoted string. | ||
* | ||
* @param File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the double quoted string. | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
*/ | ||
protected function processVariableInString(File $phpcsFile, $stackPtr): void | ||
baci266 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
if ( | ||
preg_match_all( | ||
'|[^\\\]\${?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', | ||
$tokens[$stackPtr]['content'], | ||
$matches | ||
) === 0 | ||
) { | ||
return; | ||
} | ||
|
||
foreach ($matches[1] as $varName) { | ||
// If it's a php reserved var, then its ok. | ||
if (isset($this->phpReservedVars[$varName]) === true) { | ||
continue; | ||
} | ||
|
||
if ($this->matchesRegex($varName, $this->stringPattern)) { | ||
continue; | ||
} | ||
|
||
$error = sprintf('Variable "%%s" does not match pattern "%s"', $this->stringPattern); | ||
$data = [$varName]; | ||
$phpcsFile->addError($error, $stackPtr, self::CODE_STRING_DOES_NOT_MATCH_PATTERN, $data); | ||
} | ||
} | ||
|
||
private function matchesRegex(string $varName, string $pattern): bool | ||
{ | ||
return preg_match(sprintf('~%s~', $pattern), $varName) === 1; | ||
} | ||
} |
This file contains hidden or 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
82 changes: 82 additions & 0 deletions
82
tests/Sniffs/NamingConventions/ValidVariableNameSniffTest.php
This file contains hidden or 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,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cdn77\Sniffs\NamingConventions; | ||
|
||
use Cdn77\TestCase; | ||
|
||
use function array_keys; | ||
use function is_array; | ||
use function json_encode; | ||
|
||
use const JSON_THROW_ON_ERROR; | ||
|
||
class ValidVariableNameSniffTest extends TestCase | ||
{ | ||
public function testErrors(): void | ||
{ | ||
$file = self::checkFile(__DIR__ . '/data/ValidVariableNameSniffTest.inc'); | ||
|
||
$errorTypesPerLine = [ | ||
3 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
5 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
10 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
12 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
15 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
17 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
19 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
20 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
21 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
26 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
28 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
31 => ValidVariableNameSniff::CODE_STRING_DOES_NOT_MATCH_PATTERN, | ||
32 => ValidVariableNameSniff::CODE_STRING_DOES_NOT_MATCH_PATTERN, | ||
34 => ValidVariableNameSniff::CODE_STRING_DOES_NOT_MATCH_PATTERN, | ||
37 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
39 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
48 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
50 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
53 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
55 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
57 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
58 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
59 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
62 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
76 => ValidVariableNameSniff::CODE_STRING_DOES_NOT_MATCH_PATTERN, | ||
100 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
101 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
102 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
117 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
118 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
128 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
132 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
134 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
135 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
140 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
142 => ValidVariableNameSniff::CODE_MEMBER_DOES_NOT_MATCH_PATTERN, | ||
144 => [ | ||
ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
], | ||
146 => ValidVariableNameSniff::CODE_DOES_NOT_MATCH_PATTERN, | ||
]; | ||
$possibleLines = array_keys($errorTypesPerLine); | ||
|
||
$errors = $file->getErrors(); | ||
foreach ($errors as $line => $error) { | ||
self::assertContains($line, $possibleLines, json_encode($error, JSON_THROW_ON_ERROR)); | ||
|
||
$errorTypes = $errorTypesPerLine[$line]; | ||
if (! is_array($errorTypes)) { | ||
$errorTypes = [$errorTypes]; | ||
} | ||
|
||
foreach ($errorTypes as $errorType) { | ||
self::assertSniffError($file, $line, $errorType); | ||
} | ||
} | ||
|
||
self::assertSame(41, $file->getErrorCount()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.