Skip to content

Commit 87168cf

Browse files
jrfnlsirbrillig
andauthored
PHPStan: update to run against current version (sirbrillig#246)
* PHPStan: update to run against current version The PHPStan version being used is wildly out of date, which makes it not very effective. This update the PHPStan version used to the latest release and updates the PHPStan configuration to match. Refs: * https://github.com/phpstan/phpstan/releases * https://github.com/phpstan/phpstan/releases/tag/1.0.0 * https://phpstan.org/blog/phpstan-1-0-released * Docs: improve type declarations to help PHPStan * Fix remaining issues * Fix missing quote in circleci config * Use empty for isTokenInsideArrowFunctionDefinition guard Co-authored-by: jrfnl <jrfnl@users.noreply.github.com> Co-authored-by: Payton Swick <payton@foolord.com>
1 parent ecac58f commit 87168cf

File tree

8 files changed

+37
-21
lines changed

8 files changed

+37
-21
lines changed

VariableAnalysis/Lib/Constants.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Constants {
1212
* ones which can be passed an undefined variable (eg: `$matches` in
1313
* `preg_match`) and will define that variable.
1414
*
15-
* @return array[]
15+
* @return array<string, array<int|string>>
1616
*/
1717
public static function getPassByReferenceFunctions() {
1818
return [
@@ -236,7 +236,7 @@ public static function getPassByReferenceFunctions() {
236236
}
237237

238238
/**
239-
* @return array[]
239+
* @return array<string, array<int>>
240240
*/
241241
public static function getWordPressPassByReferenceFunctions() {
242242
return [

VariableAnalysis/Lib/Helpers.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Helpers {
1212
/**
13-
* return int[]
13+
* @return array<int|string>
1414
*/
1515
public static function getPossibleEndOfFileTokens() {
1616
return array_merge(
@@ -259,7 +259,7 @@ public static function findFunctionCall(File $phpcsFile, $stackPtr) {
259259
* @param File $phpcsFile
260260
* @param int $stackPtr
261261
*
262-
* @return array[]
262+
* @return array<int, array<int>>
263263
*/
264264
public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
265265
$tokens = $phpcsFile->getTokens();
@@ -291,13 +291,21 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
291291
while (is_int($nextPtr)) {
292292
if (Helpers::findContainingOpeningBracket($phpcsFile, $nextPtr) == $openPtr) {
293293
// Comma is at our level of brackets, it's an argument delimiter.
294-
array_push($argPtrs, range($lastArgComma + 1, $nextPtr - 1));
294+
$range = range($lastArgComma + 1, $nextPtr - 1);
295+
$range = array_filter($range, function($element) {
296+
return is_int($element);
297+
});
298+
array_push($argPtrs, $range);
295299
$lastArgComma = $nextPtr;
296300
}
297301
$lastPtr = $nextPtr;
298302
$nextPtr = $phpcsFile->findNext([T_COMMA], $lastPtr + 1, $closePtr);
299303
}
300-
array_push($argPtrs, range($lastArgComma + 1, $closePtr - 1));
304+
$range = range($lastArgComma + 1, $closePtr - 1);
305+
$range = array_filter($range, function($element) {
306+
return is_int($element);
307+
});
308+
array_push($argPtrs, $range);
301309

302310
return $argPtrs;
303311
}
@@ -477,7 +485,7 @@ public static function isTokenInsideArrowFunctionDefinition(File $phpcsFile, $st
477485
$tokens = $phpcsFile->getTokens();
478486
$token = $tokens[$stackPtr];
479487
$openParenIndices = isset($token['nested_parenthesis']) ? $token['nested_parenthesis'] : [];
480-
if ($openParenIndices) {
488+
if (empty($openParenIndices)) {
481489
return false;
482490
}
483491
$openParenPtr = $openParenIndices[0];
@@ -561,7 +569,7 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr) {
561569
* @param File $phpcsFile
562570
* @param int $stackPtr
563571
*
564-
* @return ?array
572+
* @return ?array<string, int>
565573
*/
566574
public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
567575
$tokens = $phpcsFile->getTokens();
@@ -613,7 +621,7 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
613621
* @param File $phpcsFile
614622
* @param int $listOpenerIndex
615623
*
616-
* @return ?array
624+
* @return ?array<int>
617625
*/
618626
public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
619627
$tokens = $phpcsFile->getTokens();

VariableAnalysis/Lib/ScopeInfo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class ScopeInfo {
2727
*/
2828
public $variables = [];
2929

30+
/**
31+
* @param int $scopeStartIndex
32+
* @param int|null $scopeEndIndex
33+
*/
3034
public function __construct($scopeStartIndex, $scopeEndIndex = null) {
3135
$this->scopeStartIndex = $scopeStartIndex;
3236
$this->scopeEndIndex = $scopeEndIndex;

VariableAnalysis/Lib/VariableInfo.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class VariableInfo {
2626
public $typeHint;
2727

2828
/**
29-
* @var int | null
29+
* @var int|null
3030
*/
3131
public $referencedVariableScope;
3232

@@ -44,21 +44,21 @@ class VariableInfo {
4444
*
4545
* Assignment by reference is also a declaration and not an initialization.
4646
*
47-
* @var int
47+
* @var int|null
4848
*/
4949
public $firstDeclared;
5050

5151
/**
5252
* Stack pointer of first initialization
5353
*
54-
* @var int
54+
* @var int|null
5555
*/
5656
public $firstInitialized;
5757

5858
/**
5959
* Stack pointer of first read
6060
*
61-
* @var int
61+
* @var int|null
6262
*/
6363
public $firstRead;
6464

@@ -98,6 +98,9 @@ class VariableInfo {
9898
ScopeType::BOUND => 'bound variable',
9999
);
100100

101+
/**
102+
* @param string $varName
103+
*/
101104
public function __construct($varName) {
102105
$this->name = $varName;
103106
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function register() {
182182
/**
183183
* @param string $functionName
184184
*
185-
* @return string[]
185+
* @return array<int|string>
186186
*/
187187
private function getPassByReferenceFunction($functionName) {
188188
$passByRefFunctions = Constants::getPassByReferenceFunctions();
@@ -448,7 +448,7 @@ protected function markVariableAssignmentWithoutInitialization($varName, $stackP
448448
}
449449
}
450450

451-
if (!isset($varInfo->scopeType)) {
451+
if (empty($varInfo->scopeType)) {
452452
$varInfo->scopeType = ScopeType::LOCAL;
453453
}
454454
$varInfo->allAssignments[] = $stackPtr;
@@ -475,7 +475,7 @@ protected function markVariableDeclaration(
475475
Helpers::debug("marking variable '{$varName}' declared in scope starting at token", $currScope);
476476
$varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
477477

478-
if (isset($varInfo->scopeType)) {
478+
if (! empty($varInfo->scopeType)) {
479479
if (($permitMatchingRedeclaration === false) || ($varInfo->scopeType !== $scopeType)) {
480480
// Issue redeclaration/reuse warning
481481
// Note: we check off scopeType not firstDeclared, this is so that
@@ -620,6 +620,7 @@ protected function markAllVariablesRead(File $phpcsFile, $stackPtr) {
620620
* @param File $phpcsFile
621621
* @param int $stackPtr
622622
* @param string $varName
623+
* @param int $outerScope
623624
*
624625
* @return void
625626
*/
@@ -1578,7 +1579,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
15781579
/**
15791580
* @param File $phpcsFile
15801581
* @param int $stackPtr
1581-
* @param array[] $arguments The stack pointers of each argument
1582+
* @param array<int, array<int>> $arguments The stack pointers of each argument
15821583
* @param int $currScope
15831584
*
15841585
* @return void

composer.circleci.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"minimum-stability": "dev",
3535
"prefer-stable": true,
3636
"scripts": {
37-
"test": "./vendor/bin/phpunit
37+
"test": "./vendor/bin/phpunit"
3838
},
3939
"require" : {
4040
"php" : ">=5.4.0",

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"require-dev": {
4949
"phpunit/phpunit": "^5.0 || ^6.5 || ^7.0 || ^8.0",
5050
"sirbrillig/phpcs-import-detection": "^1.1",
51-
"phpstan/phpstan": "^0.11.8",
51+
"phpstan/phpstan": "^1.7",
5252
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0"
5353
}
5454
}

phpstan.neon.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
parameters:
22
level: 7
33
reportUnmatchedIgnoredErrors: false
4-
autoload_files:
4+
bootstrapFiles:
55
- %currentWorkingDirectory%/vendor/squizlabs/php_codesniffer/autoload.php
66
paths:
77
- %currentWorkingDirectory%/VariableAnalysis/
8-
excludes_analyse:
8+
excludePaths:
99
- %currentWorkingDirectory%/Tests/
1010
ignoreErrors:
1111
- '~^Constant T_\w+ not found.$~'

0 commit comments

Comments
 (0)