8000 feat: add support for typed constants (#141) · cdn77/coding-standard@be52c85 · GitHub
[go: up one dir, main page]

Skip to content

Commit be52c85

Browse files
authored
feat: add support for typed constants (#141)
1 parent 501b2dc commit be52c85

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/Cdn77/Sniffs/NamingConventions/ValidConstantNameSniff.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use const T_CONST;
1818
use const T_CONSTANT_ENCAPSED_STRING;
1919
use const T_DOUBLE_COLON;
20+
use const T_EQUAL;
2021
use const T_NULLSAFE_OBJECT_OPERATOR;
2122
use const T_OBJECT_OPERATOR;
23+
use const T_SEMICOLON;
2224
use const T_STRING;
2325
use const T_WHITESPACE;
2426

@@ -56,8 +58,16 @@ public function process(File $phpcsFile, $stackPtr): void
5658
$tokens = $phpcsFile->getTokens();
5759

5860
if ($tokens[$stackPtr]['code'] === T_CONST) {
59-
// This is a class constant.
60-
$constant = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
61+
// This is a constant declared with the "const" keyword.
62+
// This may be an OO constant, in which case it could be typed, so we need to
63+
// jump over a potential type to get to the name.
64+
$assignmentOperator = $phpcsFile->findNext([T_EQUAL, T_SEMICOLON], $stackPtr + 1);
65+
if ($assignmentOperator === false || $tokens[$assignmentOperator]['code'] !== T_EQUAL) {
66+
// Parse error/live coding. Nothing to do. Rest of loop is moot.
67+
return;
68+
}
69+
70+
$constant = $phpcsFile->findPrevious(Tokens::$emptyTokens, $assignmentOperator - 1, $stackPtr + 1, true);
6171
if ($constant === false) {
6272
return;
6373
}

tests/Sniffs/NamingConventions/ValidConstantNameSniffTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function json_encode;
1313

1414
use const JSON_THROW_ON_ERROR;
15+
use const PHP_VERSION_ID;
1516

1617
#[CoversClass(ValidConstantNameSniff::class)]
1718
class ValidConstantNameSniffTest extends TestCase
@@ -41,4 +42,29 @@ public function testErrors(): void
4142

4243
self::assertSame(count($errorTypesPerLine), $file->getErrorCount());
4344
}
45+
46+
public function testErrorsConstantType(): void
47+
{
48+
if (PHP_VERSION_ID < 80300) {
49+
self::markTestSkipped('Test requires PHP 8.3');
50+
}
51+
52+
$file = self::checkFile(__DIR__ . '/data/ValidConstantNameWithTypeTest.inc');
53+
54+
$errorTypesPerLine = [
55+
6 => ValidConstantNameSniff::CodeClassConstantNotMatchPattern,
56+
];
57+
$possibleLines = array_keys($errorTypesPerLine);
58+
59+
$errors = $file->getErrors();
60+
foreach ($errors as $line => $error) {
61+
self::assertContains($line, $possibleLines, json_encode($error, JSON_THROW_ON_ERROR));
62+
63+
$errorType = $errorTypesPerLine[$line];
64+
65+
self::assertSniffError($file, $line, $errorType);
66+
}
67+
68+
self::assertSame(count($errorTypesPerLine), $file->getErrorCount());
69+
}
4470
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
class TestClass
4+
{
5+
const string PascalCase = 'hello';
6+
const string camelCase = 'hello';
7+
}

0 commit comments

Comments
 (0)
0