-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
Tests/VariableAnalysisSniff/fixtures/ShortPhpTagsFixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters