Skip to content

Commit

Permalink
Merge ed2703a into 241ddec
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Mar 13, 2023
2 parents 241ddec + ed2703a commit 1b883aa
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
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
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
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 1b883aa

Please sign in to comment.