Skip to content

Commit

Permalink
FunctionsAnalyzer - false positive for constant and function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and SpacePossum committed Jul 10, 2018
1 parent fb4f582 commit 673de4a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Fixer/LanguageConstruct/ErrorSuppressionFixer.php
Expand Up @@ -114,7 +114,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

if (!$functionsAnalyzer->isGlobalFunctionIndex($tokens, $index)) {
if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
continue;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Tokenizer/Analyzer/FunctionsAnalyzer.php
Expand Up @@ -27,7 +27,7 @@ final class FunctionsAnalyzer
*
* @return bool
*/
public function isGlobalFunctionIndex(Tokens $tokens, $index)
public function isGlobalFunctionCall(Tokens $tokens, $index)
{
if (!$tokens[$index]->isGivenKind(T_STRING)) {
return false;
Expand All @@ -40,8 +40,9 @@ public function isGlobalFunctionIndex(Tokens $tokens, $index)

$nextIndex = $tokens->getNextMeaningfulToken($index);

return !$tokens[$prevIndex]->isGivenKind([T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_STRING])
&& !$tokens[$nextIndex]->isGivenKind([T_DOUBLE_COLON, T_NS_SEPARATOR]);
return !$tokens[$prevIndex]->isGivenKind([T_DOUBLE_COLON, T_NEW, T_OBJECT_OPERATOR, T_STRING, T_FUNCTION])
&& !$tokens[$nextIndex]->isGivenKind([T_DOUBLE_COLON, T_NS_SEPARATOR])
&& $tokens[$nextIndex]->equals('(');
}

/**
Expand Down
18 changes: 14 additions & 4 deletions tests/Tokenizer/Analyzer/FunctionsAnalyzerTest.php
Expand Up @@ -32,19 +32,24 @@ final class FunctionsAnalyzerTest extends TestCase
* @param string $code
* @param int $index
*
* @dataProvider provideIsGlobalFunctionIndexCases
* @dataProvider provideIsGlobalFunctionCallCases
*/
public function testIsGlobalFunctionIndex($isFunctionIndex, $code, $index)
public function testIsGlobalFunctionCall($isFunctionIndex, $code, $index)
{
$tokens = Tokens::fromCode($code);
$analyzer = new FunctionsAnalyzer();

$this->assertSame($isFunctionIndex, $analyzer->isGlobalFunctionIndex($tokens, $index));
$this->assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
}

public function provideIsGlobalFunctionIndexCases()
public function provideIsGlobalFunctionCallCases()
{
return [
[
false,
'<?php CONSTANT;',
1,
],
[
true,
'<?php foo("bar");',
Expand Down Expand Up @@ -90,6 +95,11 @@ public function provideIsGlobalFunctionIndexCases()
'<?php new bar("baz");',
3,
],
[
false,
'<?php function foo() {};',
3,
],
];
}

Expand Down

0 comments on commit 673de4a

Please sign in to comment.