Skip to content
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

feat: add support for typed constants #141

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
Loading