-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReferenceUsedNamesOnlySniff: functions and constants support is hopef…
…ully complete
- Loading branch information
Showing
21 changed files
with
411 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Helpers; | ||
|
||
class ConstantHelper | ||
{ | ||
|
||
public static function getName(\PHP_CodeSniffer\Files\File $codeSnifferFile, int $constantPointer): string | ||
{ | ||
$tokens = $codeSnifferFile->getTokens(); | ||
return $tokens[TokenHelper::findNext($codeSnifferFile, T_STRING, $constantPointer + 1)]['content']; | ||
} | ||
|
||
public static function getFullyQualifiedName(\PHP_CodeSniffer\Files\File $codeSnifferFile, int $constantPointer): string | ||
{ | ||
$name = self::getName($codeSnifferFile, $constantPointer); | ||
$namespace = NamespaceHelper::findCurrentNamespaceName($codeSnifferFile, $constantPointer); | ||
|
||
return $namespace !== null ? sprintf('%s%s%s%s', NamespaceHelper::NAMESPACE_SEPARATOR, $namespace, NamespaceHelper::NAMESPACE_SEPARATOR, $name) : $name; | ||
} | ||
|
||
/** | ||
* @param \PHP_CodeSniffer\Files\File $codeSnifferFile | ||
* @return string[] | ||
*/ | ||
public static function getAllNames(\PHP_CodeSniffer\Files\File $codeSnifferFile): array | ||
{ | ||
$previousConstantPointer = 0; | ||
|
||
return array_map( | ||
function (int $constantPointer) use ($codeSnifferFile): string { | ||
return self::getName($codeSnifferFile, $constantPointer); | ||
}, | ||
array_filter( | ||
iterator_to_array(self::getAllConstantPointers($codeSnifferFile, $previousConstantPointer)), | ||
function (int $constantPointer) use ($codeSnifferFile): bool { | ||
foreach (array_reverse($codeSnifferFile->getTokens()[$constantPointer]['conditions']) as $conditionTokenCode) { | ||
if (in_array($conditionTokenCode, [T_CLASS, T_INTERFACE, T_TRAIT, T_ANON_CLASS], true)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
) | ||
); | ||
} | ||
|
||
private static function getAllConstantPointers(\PHP_CodeSniffer\Files\File $codeSnifferFile, int &$previousConstantPointer): \Generator | ||
{ | ||
do { | ||
$nextConstantPointer = TokenHelper::findNext($codeSnifferFile, T_CONST, $previousConstantPointer + 1); | ||
if ($nextConstantPointer !== null) { | ||
$previousConstantPointer = $nextConstantPointer; | ||
yield $nextConstantPointer; | ||
} | ||
} while ($nextConstantPointer !== null); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Helpers; | ||
|
||
class ConstantHelperTest extends \SlevomatCodingStandard\Helpers\TestCase | ||
{ | ||
|
||
public function testNameWithNamespace(): void | ||
{ | ||
$codeSnifferFile = $this->getCodeSnifferFile(__DIR__ . '/data/constantWithNamespace.php'); | ||
|
||
$constantPointer = $this->findConstantPointerByName($codeSnifferFile, 'FOO'); | ||
$this->assertSame('\FooNamespace\FOO', ConstantHelper::getFullyQualifiedName($codeSnifferFile, $constantPointer)); | ||
$this->assertSame('FOO', ConstantHelper::getName($codeSnifferFile, $constantPointer)); | ||
} | ||
|
||
public function testNameWithoutNamespace(): void | ||
{ | ||
$codeSnifferFile = $this->getCodeSnifferFile(__DIR__ . '/data/constantWithoutNamespace.php'); | ||
|
||
$constantPointer = $this->findConstantPointerByName($codeSnifferFile, 'FOO'); | ||
$this->assertSame('FOO', ConstantHelper::getFullyQualifiedName($codeSnifferFile, $constantPointer)); | ||
$this->assertSame('FOO', ConstantHelper::getName($codeSnifferFile, $constantPointer)); | ||
} | ||
|
||
public function testGetAllNames(): void | ||
{ | ||
$codeSnifferFile = $this->getCodeSnifferFile(__DIR__ . '/data/constantNames.php'); | ||
$this->assertSame(['FOO', 'BOO'], ConstantHelper::getAllNames($codeSnifferFile)); | ||
} | ||
|
||
} |
Oops, something went wrong.
b742ecb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allowFullyQualifiedGlobalFunctions=true
doesn't seem to play well withallowFallbackGlobalFunctions=false
+allowFullyQualifiedNameForCollidingFunctions=true
- doesn't report any errors for fallback references (no\
anduse
). WithallowFullyQualifiedGlobalFunctions=false
it works.b742ecb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also found one bug: it added a method name into uses: Majkl578/doctrine-orm@2b69bd9#diff-6e8c1c1e78b054ba05e20ea09d877865R45, this method returns reference if that could be the source.
b742ecb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5309879
It's possible there will be some more mistakes like this.
b742ecb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed bug via 52f3c79 but it was caused by
allowFullyQualifiedNameForCollidingFunctions=true
notallowFullyQualifiedGlobalFunctions=true
. So if there is still a bug caused byallowFullyQualifiedGlobalFunctions=true
could you please send me the file?b742ecb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ok now. 👍