Skip to content

Commit

Permalink
feat: add support for typed constants
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Feb 18, 2024
1 parent 501b2dc commit 7c35a01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Cdn77/Sniffs/NamingConventions/ValidConstantNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use const T_CONST;
use const T_CONSTANT_ENCAPSED_STRING;
use const T_DOUBLE_COLON;
use const T_EQUAL;
use const T_NULLSAFE_OBJECT_OPERATOR;
use const T_OBJECT_OPERATOR;
use const T_SEMICOLON;
use const T_STRING;
use const T_WHITESPACE;

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

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

$constant = $phpcsFile->findPrevious(Tokens::$emptyTokens, $assignmentOperator - 1, $stackPtr + 1, true);
if ($constant === false) {
return;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Sniffs/NamingConventions/ValidConstantNameSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function json_encode;

use const JSON_THROW_ON_ERROR;
use const PHP_VERSION_ID;

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

self::assertSame(count($errorTypesPerLine), $file->getErrorCount());
}

public function testErrorsConstantType(): void
{
if (PHP_VERSION_ID < 80300) {
self::markTestSkipped('Test requires PHP 8.3');
}

$file = self::checkFile(__DIR__ . '/data/ValidConstantNameWithTypeTest.inc');

$errorTypesPerLine = [
6 => ValidConstantNameSniff::CodeClassConstantNotMatchPattern,
];
$possibleLines = array_keys($errorTypesPerLine);

$errors = $file->getErrors();
foreach ($errors as $line => $error) {
self::assertContains($line, $possibleLines, json_encode($error, JSON_THROW_ON_ERROR));

$errorType = $errorTypesPerLine[$line];

self::assertSniffError($file, $line, $errorType);
}

self::assertSame(count($errorTypesPerLine), $file->getErrorCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

class TestClass
{
const string PascalCase = 'hello';
const string camelCase = 'hello';
}

0 comments on commit 7c35a01

Please sign in to comment.