-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add allowUnusedVariablesBeforeRequire option (#196)
* Add tests for unused before require * Move areFollowingArgumentsUsed to Helpers * Add test cases for all forms of require/include * Add isRequireInScopeAfter and use allowUnusedVariablesBeforeRequire * Add test to be sure option does not break other behavior * Add allowUnusedVariablesBeforeRequire to README
- Loading branch information
1 parent
26bf3d3
commit 3b19ddf
Showing
5 changed files
with
171 additions
and
27 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
74 changes: 74 additions & 0 deletions
74
Tests/VariableAnalysisSniff/UnusedFollowedByRequireTest.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,74 @@ | ||
<?php | ||
namespace VariableAnalysis\Tests\VariableAnalysisSniff; | ||
|
||
use VariableAnalysis\Tests\BaseTestCase; | ||
|
||
class UnusedFollowedByRequire extends BaseTestCase { | ||
public function testUnusedFollowedByRequireWarnsByDefault() { | ||
$fixtureFile = $this->getFixture('UnusedFollowedByRequireFixture.php'); | ||
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); | ||
$phpcsFile->process(); | ||
$lines = $this->getWarningLineNumbersFromFile($phpcsFile); | ||
$expectedWarnings = [ | ||
2, | ||
3, | ||
4, | ||
8, | ||
9, | ||
10, | ||
14, | ||
15, | ||
16, | ||
20, | ||
21, | ||
22, | ||
]; | ||
$this->assertEquals($expectedWarnings, $lines); | ||
} | ||
|
||
public function testUnusedFollowedByRequireDoesNotWarnWhenSet() { | ||
$fixtureFile = $this->getFixture('UnusedFollowedByRequireFixture.php'); | ||
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); | ||
$phpcsFile->ruleset->setSniffProperty( | ||
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff', | ||
'allowUnusedVariablesBeforeRequire', | ||
'true' | ||
); | ||
$phpcsFile->process(); | ||
$lines = $this->getWarningLineNumbersFromFile($phpcsFile); | ||
$expectedWarnings = [ | ||
4, | ||
10, | ||
16, | ||
22, | ||
]; | ||
$this->assertEquals($expectedWarnings, $lines); | ||
} | ||
|
||
public function testUnusedFollowedByRequireDoesNotBreakOtherThingsWhenSet() { | ||
$fixtureFile = $this->getFixture('FunctionWithoutParamFixture.php'); | ||
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); | ||
$phpcsFile->ruleset->setSniffProperty( | ||
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff', | ||
'allowUnusedVariablesBeforeRequire', | ||
'true' | ||
); | ||
$phpcsFile->process(); | ||
$lines = $this->getWarningLineNumbersFromFile($phpcsFile); | ||
$expectedWarnings = [ | ||
4, | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
10, | ||
11, | ||
12, | ||
13, | ||
18, | ||
19, | ||
]; | ||
$this->assertEquals($expectedWarnings, $lines); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Tests/VariableAnalysisSniff/fixtures/UnusedFollowedByRequireFixture.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,24 @@ | ||
<?php | ||
function require_file_function($param) { // unused variable $param | ||
$var = 'something'; // unused variable $var | ||
activate_code($data); // undefined variable $data | ||
require __DIR__ . '/views/my-view.php'; | ||
} | ||
|
||
function require_once_file_function($param) { // unused variable $param | ||
$var = 'something'; // unused variable $var | ||
activate_code($data); // undefined variable $data | ||
require_once __DIR__ . '/views/my-view.php'; | ||
} | ||
|
||
function include_file_function($param) { // unused variable $param | ||
$var = 'something'; // unused variable $var | ||
activate_code($data); // undefined variable $data | ||
include __DIR__ . '/views/my-view.php'; | ||
} | ||
|
||
function include_once_file_function($param) { // unused variable $param | ||
$var = 'something'; // unused variable $var | ||
activate_code($data); // undefined variable $data | ||
include_once __DIR__ . '/views/my-view.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
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