Skip to content

Commit d8a00fb

Browse files
authored
Allow non-enum tokens called 'enum' (#293)
* Allow non-enum tokens called 'enum' * Fix spacing issue * Do not stop scanning if we are not an enum
1 parent 241ddec commit d8a00fb

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

Tests/VariableAnalysisSniff/fixtures/EnumFixture.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ public function foobar(): string {
3737
};
3838
}
3939
}
40+
41+
final class Test
42+
{
43+
public function __construct(private SomeClass $someClass)
44+
{
45+
}
46+
47+
public function createFor()
48+
{
49+
$this->someClass->enum('test');
50+
}
51+
}

VariableAnalysis/Lib/Helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ public static function isTokenVariableVariable(File $phpcsFile, $stackPtr)
12981298
* @param File $phpcsFile
12991299
* @param int $stackPtr
13001300
*
1301-
* @return EnumInfo
1301+
* @return EnumInfo|null
13021302
*/
13031303
public static function makeEnumInfo(File $phpcsFile, $stackPtr)
13041304
{
@@ -1314,7 +1314,7 @@ public static function makeEnumInfo(File $phpcsFile, $stackPtr)
13141314

13151315
$blockStart = $phpcsFile->findNext([T_OPEN_CURLY_BRACKET], $stackPtr + 1);
13161316
if (! is_int($blockStart)) {
1317-
throw new \Exception("Cannot find enum start at position {$stackPtr}");
1317+
return null;
13181318
}
13191319
$blockEnd = $tokens[$blockStart]['bracket_closer'];
13201320
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ public function process(File $phpcsFile, $stackPtr)
278278

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

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

306-
/**
307-
* Record the boundaries of an enum.
308-
*
309-
* @param File $phpcsFile
310-
* @param int $stackPtr
311-
*
312-
* @return void
313-
*/
314-
private function recordEnum($phpcsFile, $stackPtr)
315-
{
316-
$this->enums[$stackPtr] = Helpers::makeEnumInfo($phpcsFile, $stackPtr);
317-
}
318-
319311
/**
320312
* Record the boundaries of a for loop.
321313
*

0 commit comments

Comments
 (0)