Skip to content

Commit b221f6e

Browse files
committed
Use regex pattern instead of Common::isCamelCaps() - parity with original
1 parent 6e4a2bd commit b221f6e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/Cdn77/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
9-
use PHP_CodeSniffer\Util\Common;
109
use PHP_CodeSniffer\Util\Tokens;
1110

1211
use function assert;
1312
use function ltrim;
13+
use function preg_match;
1414
use function preg_match_all;
15+
use function sprintf;
1516
use function strpos;
1617
use function substr;
1718
use function ucfirst;
@@ -25,6 +26,8 @@
2526

2627
class ValidVariableNameSniff extends AbstractVariableSniff
2728
{
29+
public string $pattern = '^[a-zA-Z][a-zA-Z0-9]*?([A-Z][a-zA-Z0-9]*?)*?$';
30+
2831
/**
2932
* Processes this test, when one of its tokens is encountered.
3033
*
@@ -68,7 +71,7 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
6871
$objVarName = substr($objVarName, 1);
6972
}
7073

71-
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
74+
if (! $this->matchesRegex($objVarName)) {
7275
$error = 'Member variable "%s" is not in valid camel caps format';
7376
$data = [$originalVarName];
7477
$phpcsFile->addError($error, $var, 'MemberNotCamelCaps', $data);
@@ -86,7 +89,7 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
8689
$objVarName = substr($objVarName, 1);
8790
}
8891

89-
if (Common::isCamelCaps($objVarName, false, true, false) === false) {
92+
if (!$this->matchesRegex($objVarName)) {
9093
$error = 'Member variable "%s" is not in valid camel caps format';
9194
$data = [$tokens[$stackPtr]['content']];
9295
$phpcsFile->addError($error, $stackPtr, 'MemberNotCamelCaps', $data);
@@ -106,7 +109,7 @@ protected function processVariable(File $phpcsFile, $stackPtr): void
106109
}
107110
}
108111

109-
if (Common::isCamelCaps($varName, false, true, false) !== false) {
112+
if ($this->matchesRegex($varName)) {
110113
return;
111114
}
112115

@@ -158,7 +161,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr): void
158161
// Remove a potential underscore prefix for testing CamelCaps.
159162
$varName = ltrim($varName, '_');
160163

161-
if (Common::isCamelCaps($varName, false, true, false) !== false) {
164+
if ($this->matchesRegex($varName)) {
162165
return;
163166
}
164167

@@ -195,7 +198,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr): void
195198
continue;
196199
}
197200

198-
if (Common::isCamelCaps($varName, false, true, false) !== false) {
201+
if ($this->matchesRegex($varName)) {
199202
continue;
200203
}
201204

@@ -204,4 +207,9 @@ protected function processVariableInString(File $phpcsFile, $stackPtr): void
204207
$phpcsFile->addError($error, $stackPtr, 'StringNotCamelCaps', $data);
205208
}
206209
}
210+
211+
private function matchesRegex(string $varName): bool
212+
{
213+
return preg_match(sprintf('~%s~', $this->pattern), $varName) === 1;
214+
}
207215
}

tests/Sniffs/NamingConventions/ValidVariableNameSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testErrors(): void
5050
67 => self::CODE_NOT_CAMEL_CAPS,
5151
81 => self::CODE_STRING_NOT_CAMEL_CAPS,
5252
106 => self::CODE_PUBLIC_HAS_UNDERSCORE,
53-
107 => [self::CODE_PUBLIC_HAS_UNDERSCORE,self::CODE_MEMBER_NOT_CAMEL_CAPS],
53+
107 => self::CODE_PUBLIC_HAS_UNDERSCORE,
5454
108 => self::CODE_PUBLIC_HAS_UNDERSCORE,
5555
111 => self::CODE_PRIVATE_NO_UNDERSCORE,
5656
112 => self::CODE_PRIVATE_NO_UNDERSCORE,
@@ -78,6 +78,6 @@ public function testErrors(): void
7878
}
7979
}
8080

81-
self::assertSame(38, $file->getErrorCount());
81+
self::assertSame(37, $file->getErrorCount());
8282
}
8383
}

0 commit comments

Comments
 (0)