Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FunctionsAnalyzer - false positive for constant and function definition #3895

Merged
merged 1 commit into from Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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