Skip to content

Commit e0c3d7b

Browse files
authored
Fix scanning for static variables near start of file (#274)
* Add test for variable at start of file * Allow looking for previous static statements at start of file
1 parent f0146d7 commit e0c3d7b

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Tests/VariableAnalysisSniff/GlobalScopeTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ public function testGlobalScopeWarnings()
1818
$phpcsFile->process();
1919
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
2020
$expectedErrors = [
21-
4,
22-
7,
23-
10,
24-
13,
21+
3,
22+
5,
23+
8,
24+
11,
25+
14,
2526
];
2627
$this->assertSame($expectedErrors, $lines);
2728
}
@@ -38,9 +39,10 @@ public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope()
3839
$phpcsFile->process();
3940
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
4041
$expectedErrors = [
41-
4,
42-
10,
43-
13,
42+
3,
43+
5,
44+
11,
45+
14,
4446
];
4547
$this->assertSame($expectedErrors, $lines);
4648
}
@@ -57,8 +59,9 @@ public function testGlobalScopeWarningsWithAllowUnusedVariablesInFileScope()
5759
$phpcsFile->process();
5860
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
5961
$expectedErrors = [
60-
7,
61-
10,
62+
3,
63+
8,
64+
11,
6265
];
6366
$this->assertSame($expectedErrors, $lines);
6467
}

Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
$post = $data['post']; // Undefined variable $data, unused variable $post
34
$name = 'friend';
45
$place = 'faerie'; // unused variable $place
56

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,7 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr
13161316
$startOfStatement = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $stackPtr - 1, null, false, null, true);
13171317
$staticPtr = $phpcsFile->findPrevious([T_STATIC], $stackPtr - 1, null, false, null, true);
13181318
if (! is_int($startOfStatement)) {
1319-
$line = $tokens[$stackPtr]['line'];
1320-
throw new \Exception("Could not find start of statement on line {$line}");
1319+
$startOfStatement = 1;
13211320
}
13221321
if (! is_int($staticPtr)) {
13231322
return false;

0 commit comments

Comments
 (0)