Skip to content

fix: Process variable before the end of scope #324

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

Merged
merged 2 commits into from
Jun 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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