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

TokensAnalyzer - fix isConstantInvocation #3913

Merged
merged 1 commit into from Aug 19, 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
9 changes: 9 additions & 0 deletions src/Tokenizer/TokensAnalyzer.php
Expand Up @@ -355,6 +355,15 @@ public function isConstantInvocation($index)
}
}

// check for array in double quoted string: `"..$foo[bar].."`
if ($this->tokens[$prevIndex]->equals('[') && $this->tokens[$nextIndex]->equals(']')) {
$checkToken = $this->tokens[$this->tokens->getNextMeaningfulToken($nextIndex)];

if ($checkToken->equals('"') || $checkToken->isGivenKind([T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES, T_ENCAPSED_AND_WHITESPACE, T_VARIABLE])) {
return false;
}
}

// check for goto label
if ($this->tokens[$nextIndex]->equals(':') && $this->tokens[$prevIndex]->equalsAny([';', '}', [T_OPEN_TAG], [T_OPEN_TAG_WITH_ECHO]])) {
return false;
Expand Down
12 changes: 12 additions & 0 deletions tests/Tokenizer/TokensAnalyzerTest.php
Expand Up @@ -544,6 +544,10 @@ public function provideIsConstantInvocationCases()
'<?php echo FOO & $bar;',
[3 => true],
],
[
'<?php echo $foo[BAR];',
[5 => true],
],
[
'<?php echo FOO[BAR];',
[3 => true, 5 => true],
Expand Down Expand Up @@ -644,6 +648,14 @@ public function provideIsConstantInvocationCases()
'<?php try {} catch (FOO $e) {}',
[9 => false],
],
[
'<?php "$foo[BAR]";',
[4 => false],
],
[
'<?php "{$foo[BAR]}";',
[5 => true],
],
[
'<?php FOO: goto FOO;',
[1 => false, 6 => false],
Expand Down