Skip to content

Commit

Permalink
Allow non-enum tokens called 'enum' (#293)
Browse files Browse the repository at this point in the history
* Allow non-enum tokens called 'enum'

* Fix spacing issue

* Do not stop scanning if we are not an enum
  • Loading branch information
sirbrillig authored Mar 13, 2023
1 parent 241ddec commit d8a00fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
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

0 comments on commit d8a00fb

Please sign in to comment.