Skip to content

Commit

Permalink
Fix scanning for static variables near start of file (#274)
Browse files Browse the repository at this point in the history
* Add test for variable at start of file

* Allow looking for previous static statements at start of file
  • Loading branch information
sirbrillig authored Aug 15, 2022
1 parent f0146d7 commit e0c3d7b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions Tests/VariableAnalysisSniff/GlobalScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public function testGlobalScopeWarnings()
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
4,
7,
10,
13,
3,
5,
8,
11,
14,
];
$this->assertSame($expectedErrors, $lines);
}
Expand All @@ -38,9 +39,10 @@ public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope()
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
4,
10,
13,
3,
5,
11,
14,
];
$this->assertSame($expectedErrors, $lines);
}
Expand All @@ -57,8 +59,9 @@ public function testGlobalScopeWarningsWithAllowUnusedVariablesInFileScope()
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
7,
10,
3,
8,
11,
];
$this->assertSame($expectedErrors, $lines);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

$post = $data['post']; // Undefined variable $data, unused variable $post
$name = 'friend';
$place = 'faerie'; // unused variable $place

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,7 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr
$startOfStatement = $phpcsFile->findPrevious([T_SEMICOLON, T_OPEN_CURLY_BRACKET], $stackPtr - 1, null, false, null, true);
$staticPtr = $phpcsFile->findPrevious([T_STATIC], $stackPtr - 1, null, false, null, true);
if (! is_int($startOfStatement)) {
$line = $tokens[$stackPtr]['line'];
throw new \Exception("Could not find start of statement on line {$line}");
$startOfStatement = 1;
}
if (! is_int($staticPtr)) {
return false;
Expand Down

0 comments on commit e0c3d7b

Please sign in to comment.