Skip to content

Allow non-enum tokens called 'enum' #293

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 3 commits into from
Mar 13, 2023
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
12 changes: 12 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/EnumFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ public function foobar(): string {
};
}
}

final class Test
{
public function __construct(private SomeClass $someClass)
{
}

public function createFor()
{
$this->someClass->enum('test');
}
}
4 changes: 2 additions & 2 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ public static function isTokenVariableVariable(File $phpcsFile, $stackPtr)
* @param File $phpcsFile
* @param int $stackPtr
*
* @return EnumInfo
* @return EnumInfo|null
*/
public static function makeEnumInfo(File $phpcsFile, $stackPtr)
{
Expand All @@ -1314,7 +1314,7 @@ public static function makeEnumInfo(File $phpcsFile, $stackPtr)

$blockStart = $phpcsFile->findNext([T_OPEN_CURLY_BRACKET], $stackPtr + 1);
if (! is_int($blockStart)) {
throw new \Exception("Cannot find enum start at position {$stackPtr}");
return null;
}
$blockEnd = $tokens[$blockStart]['bracket_closer'];
}
Expand Down
22 changes: 7 additions & 15 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ public function process(File $phpcsFile, $stackPtr)

// Record enums so we can detect them even before phpcs was able to.
if ($token['content'] === 'enum') {
$this->recordEnum($phpcsFile, $stackPtr);
return;
$enumInfo = Helpers::makeEnumInfo($phpcsFile, $stackPtr);
// The token might not actually be an enum so let's avoid returning if
// it's not.
if ($enumInfo) {
$this->enums[$stackPtr] = $enumInfo;
return;
}
}

// If the current token is a call to `get_defined_vars()`, consider that a
Expand All @@ -303,19 +308,6 @@ public function process(File $phpcsFile, $stackPtr)
}
}

/**
* Record the boundaries of an enum.
*
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
private function recordEnum($phpcsFile, $stackPtr)
{
$this->enums[$stackPtr] = Helpers::makeEnumInfo($phpcsFile, $stackPtr);
}

/**
* Record the boundaries of a for loop.
*
Expand Down