Skip to content

Commit

Permalink
Add test for compact being used with variable strings (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig authored Jan 8, 2021
1 parent 6a0b28f commit 0775e0c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
23 changes: 23 additions & 0 deletions Tests/VariableAnalysisSniff/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,33 @@ public function testCompactWarnings() {
19,
23,
26,
36,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testCompactWarningsHaveCorrectSniffCodes() {
$fixtureFile = $this->getFixture('CompactFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'false'
);
$phpcsFile->process();

$warnings = $phpcsFile->getWarnings();
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable', $warnings[2][49][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[7][23][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[10][54][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable', $warnings[14][52][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable', $warnings[19][5][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[23][23][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[26][66][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable', $warnings[36][5][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[36][23][0]['source']);
}

public function testTraitAllowsThis() {
$fixtureFile = $this->getFixture('TraitFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
Expand Down
19 changes: 12 additions & 7 deletions Tests/VariableAnalysisSniff/fixtures/CompactFixture.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
<?php
function function_with_literal_compact($param1, $param2, $param3, $param4) {
function function_with_literal_compact($param1, $param2, $param3, $param4) { // unused variable param2
$var1 = 'value1';
$var2 = 'value2';
$var4 = 'value4';
$squish = compact('var1');
$squish = compact('var3');
$squish = compact('var3'); // undefined variable var3
$squish = compact('param1');
$squish = compact('var2', /*comment*/ 'param3');
$squish = compact(array('var4'), array('param4', 'var5'));
$squish = compact(array('var4'), array('param4', 'var5')); // undefined variable var5
echo $squish;
}

function function_with_expression_compact($param1, $param2, $param3, $param4) {
function function_with_expression_compact($param1, $param2, $param3, $param4) { // unused variable param2
$var1 = "value1";
$var2 = "value2";
$var4 = "value4";
$var6 = "value6";
$var7 = "value7";
$var7 = "value7"; // unused variale var7 (not actually unused but it's hard to detect that line 28 uses it)
$var8 = "value8";
$var9 = "value9";
$squish = compact("var1");
$squish = compact("var3"/*comment*/ );
$squish = compact("var3"/*comment*/ ); // undefined variable var3
$squish = compact("param1");
$squish = compact("var2", "param3");
$squish = compact(array("var4"), array("param4", /*comment*/ "var5"));
$squish = compact(array("var4"), array("param4", /*comment*/ "var5")); // undefined variable var5
$squish = compact($var6);
$squish = compact("var" . "7");
$squish = compact("blah $var8");
$squish = compact("$var9");
echo $squish;
}

function foo() {
$a = 'Hello';
$c = compact( $a, $b ); // Unused variable c and undefined variable b
}

0 comments on commit 0775e0c

Please # to comment.