Skip to content

Commit bc8d7e3

Browse files
authored
fix: Process variable before the end of scope (#324)
* test: add test for variable use in short open echo When a variable is used in a short php open echo tag, it should be marked as used. * fix: process variable before the end of scope fixes #323 When the last php code is a short php open echo tag using a variable, process the variable before processing the end of scope.
1 parent bb070b8 commit bc8d7e3

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace VariableAnalysis\Tests\VariableAnalysisSniff;
4+
5+
use VariableAnalysis\Tests\BaseTestCase;
6+
7+
class ShortPhpTagsTest extends BaseTestCase
8+
{
9+
public function testVariableWarningsWhenShortEchoTagsAreUsed()
10+
{
11+
$fixtureFile = $this->getFixture('ShortPhpTagsFixture.php');
12+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
13+
$phpcsFile->process();
14+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
15+
$expectedWarnings = [
16+
4,
17+
7,
18+
];
19+
$this->assertSame($expectedWarnings, $lines);
20+
}
21+
22+
public function testVariableWarningsHaveCorrectSniffCodesWhenShortEchoTagsAreUsed()
23+
{
24+
$fixtureFile = $this->getFixture('ShortPhpTagsFixture.php');
25+
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
26+
$phpcsFile->process();
27+
$warnings = $phpcsFile->getWarnings();
28+
$this->assertSame(self::UNUSED_ERROR_CODE, $warnings[4][1][0]['source']);
29+
$this->assertSame(self::UNDEFINED_ERROR_CODE, $warnings[7][5][0]['source']);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
$foo = 'hello';
3+
$usedLast = 'hello';
4+
$bar = 'bye'; // unused variable
5+
?>
6+
<html>
7+
<?= $baz ?> undefined variable
8+
<?= $foo ?>
9+
<?= $usedLast ?> used as last php statement without semicolon
10+
</html>

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ public function process(File $phpcsFile, $stackPtr)
245245
$this->scopeManager->recordScopeStartAndEnd($phpcsFile, 0);
246246
}
247247

248+
// Find and process variables to perform two jobs: to record variable
249+
// definition or use, and to report variables as undefined if they are used
250+
// without having been first defined.
251+
if ($token['code'] === T_VARIABLE) {
252+
$this->processVariable($phpcsFile, $stackPtr);
253+
}
254+
248255
// Report variables defined but not used in the current scope as unused
249256
// variables if the current token closes scopes.
250257
$this->searchForAndProcessClosingScopesAt($phpcsFile, $stackPtr);
@@ -253,13 +260,10 @@ public function process(File $phpcsFile, $stackPtr)
253260
// expression of a for loop if the current token closes a loop.
254261
$this->processClosingForLoopsAt($phpcsFile, $stackPtr);
255262

256-
// Find and process variables to perform two jobs: to record variable
257-
// definition or use, and to report variables as undefined if they are used
258-
// without having been first defined.
259263
if ($token['code'] === T_VARIABLE) {
260-
$this->processVariable($phpcsFile, $stackPtr);
261264
return;
262265
}
266+
263267
if (($token['code'] === T_DOUBLE_QUOTED_STRING) || ($token['code'] === T_HEREDOC)) {
264268
$this->processVariableInString($phpcsFile, $stackPtr);
265269
return;

0 commit comments

Comments
 (0)