Skip to content

Commit

Permalink
fix: Process variable before the end of scope (#324)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
biinari authored Jun 26, 2024
1 parent bb070b8 commit bc8d7e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
31 changes: 31 additions & 0 deletions Tests/VariableAnalysisSniff/ShortPhpTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace VariableAnalysis\Tests\VariableAnalysisSniff;

use VariableAnalysis\Tests\BaseTestCase;

class ShortPhpTagsTest extends BaseTestCase
{
public function testVariableWarningsWhenShortEchoTagsAreUsed()
{
$fixtureFile = $this->getFixture('ShortPhpTagsFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
4,
7,
];
$this->assertSame($expectedWarnings, $lines);
}

public function testVariableWarningsHaveCorrectSniffCodesWhenShortEchoTagsAreUsed()
{
$fixtureFile = $this->getFixture('ShortPhpTagsFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();
$warnings = $phpcsFile->getWarnings();
$this->assertSame(self::UNUSED_ERROR_CODE, $warnings[4][1][0]['source']);
$this->assertSame(self::UNDEFINED_ERROR_CODE, $warnings[7][5][0]['source']);
}
}
10 changes: 10 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/ShortPhpTagsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$foo = 'hello';
$usedLast = 'hello';
$bar = 'bye'; // unused variable
?>
<html>
<?= $baz ?> undefined variable
<?= $foo ?>
<?= $usedLast ?> used as last php statement without semicolon
</html>
12 changes: 8 additions & 4 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public function process(File $phpcsFile, $stackPtr)
$this->scopeManager->recordScopeStartAndEnd($phpcsFile, 0);
}

// Find and process variables to perform two jobs: to record variable
// definition or use, and to report variables as undefined if they are used
// without having been first defined.
if ($token['code'] === T_VARIABLE) {
$this->processVariable($phpcsFile, $stackPtr);
}

// Report variables defined but not used in the current scope as unused
// variables if the current token closes scopes.
$this->searchForAndProcessClosingScopesAt($phpcsFile, $stackPtr);
Expand All @@ -253,13 +260,10 @@ public function process(File $phpcsFile, $stackPtr)
// expression of a for loop if the current token closes a loop.
$this->processClosingForLoopsAt($phpcsFile, $stackPtr);

// Find and process variables to perform two jobs: to record variable
// definition or use, and to report variables as undefined if they are used
// without having been first defined.
if ($token['code'] === T_VARIABLE) {
$this->processVariable($phpcsFile, $stackPtr);
return;
}

if (($token['code'] === T_DOUBLE_QUOTED_STRING) || ($token['code'] === T_HEREDOC)) {
$this->processVariableInString($phpcsFile, $stackPtr);
return;
Expand Down

0 comments on commit bc8d7e3

Please sign in to comment.