Skip to content

Commit

Permalink
bug #3913 TokensAnalyzer - fix isConstantInvocation (gharlan, keradus)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.12 branch (closes #3913).

Discussion
----------

TokensAnalyzer - fix isConstantInvocation

```php
echo "... $foo[bar] .."
```

`bar` is not a constant in this case.

Commits
-------

53b196c TokensAnalyzer - fix isConstantInvocation
  • Loading branch information
keradus committed Aug 19, 2018
2 parents 1ab8faf + 53b196c commit 7774286
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit 7774286

Please sign in to comment.