Skip to content

Commit e1b89d1

Browse files
authored
Add psalm static analysis (#264)
* Add psalm * Fix some minor type errors and improve code comments * Add more function comments * If no scope closer is found, use the end of the file * Rename argument list to parameter * Clarify scopeType param type * Since we are not using real types, allow scopeType to be null * Allow typeHint to be null since we are not using real types * Disable cache for psalm because it causes failures * Adjust phpcs config to only lint source and tests * Add fix composer script * Run static-analysis in GH workflow * Fix linting issues * Add PHP 5.6 compatible version of psalm * Add psalm compatible with php 5.5 * Add support for psalm 5 to support php 8.1 * Support 5.0 beta * Allow psalm 0.2 * Allow beta stability of psalm * Allow php-coveralls install to change other deps
1 parent e9c99cd commit e1b89d1

File tree

9 files changed

+267
-60
lines changed

9 files changed

+267
-60
lines changed

.github/workflows/csqa.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ jobs:
7070
- name: Show PHPCS results in PR
7171
run: cs2pr ./phpcs-report.xml
7272

73-
phpstan:
74-
name: "PHP: 7.4 | PHPStan"
73+
static-analysis:
74+
name: "PHP: 7.4 | Static Analysis"
7575
runs-on: ubuntu-latest
7676

7777
steps:
@@ -90,5 +90,5 @@ jobs:
9090
- name: Install Composer dependencies
9191
uses: "ramsey/composer-install@v2"
9292

93-
- name: Run PHPStan
94-
run: composer phpstan
93+
- name: Run Static Analysis
94+
run: composer static-analysis

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ jobs:
187187

188188
- name: Install Coveralls
189189
if: ${{ success() }}
190-
run: composer require php-coveralls/php-coveralls:"^2.5.2" --no-interaction
190+
run: composer require php-coveralls/php-coveralls:"^2.5.2" --no-interaction --with-all-dependencies
191191

192192
- name: Upload coverage results to Coveralls
193193
if: ${{ success() }}

VariableAnalysis/Lib/Helpers.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public static function getClosestIfPositionIfBeforeOtherConditions(array $condit
136136
*
137137
* @return bool
138138
*/
139-
public static function isTokenInsideFunctionDefinitionArgumentList(File $phpcsFile, $stackPtr)
139+
public static function isTokenFunctionParameter(File $phpcsFile, $stackPtr)
140140
{
141-
return is_int(self::getFunctionIndexForFunctionArgument($phpcsFile, $stackPtr));
141+
return is_int(self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr));
142142
}
143143

144144
/**
@@ -167,7 +167,7 @@ public static function isTokenInsideFunctionCall(File $phpcsFile, $stackPtr)
167167
*
168168
* @return ?int
169169
*/
170-
public static function getFunctionIndexForFunctionArgument(File $phpcsFile, $stackPtr)
170+
public static function getFunctionIndexForFunctionParameter(File $phpcsFile, $stackPtr)
171171
{
172172
$tokens = $phpcsFile->getTokens();
173173
$token = $tokens[$stackPtr];
@@ -437,8 +437,8 @@ public static function findVariableScopeExceptArrowFunctions(File $phpcsFile, $s
437437
}
438438

439439
// If there is no "conditions" array, this is a function definition argument.
440-
if (self::isTokenInsideFunctionDefinitionArgumentList($phpcsFile, $stackPtr)) {
441-
$functionPtr = self::getFunctionIndexForFunctionArgument($phpcsFile, $stackPtr);
440+
if (self::isTokenFunctionParameter($phpcsFile, $stackPtr)) {
441+
$functionPtr = self::getFunctionIndexForFunctionParameter($phpcsFile, $stackPtr);
442442
if (! is_int($functionPtr)) {
443443
throw new \Exception("Function index not found for function argument index {$stackPtr}");
444444
}
@@ -894,7 +894,7 @@ public static function isIndexInsideScope($needle, $scopeStart, $scopeEnd)
894894
public static function getScopeCloseForScopeOpen(File $phpcsFile, $scopeStartIndex)
895895
{
896896
$tokens = $phpcsFile->getTokens();
897-
$scopeCloserIndex = isset($tokens[$scopeStartIndex]['scope_closer']) ? $tokens[$scopeStartIndex]['scope_closer'] : null;
897+
$scopeCloserIndex = isset($tokens[$scopeStartIndex]['scope_closer']) ? $tokens[$scopeStartIndex]['scope_closer'] : 0;
898898

899899
if (self::isArrowFunction($phpcsFile, $scopeStartIndex)) {
900900
$arrowFunctionInfo = self::getArrowFunctionOpenClose($phpcsFile, $scopeStartIndex);

VariableAnalysis/Lib/VariableInfo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class VariableInfo
1717
/**
1818
* What scope the variable has: local, param, static, global, bound
1919
*
20-
* @var string
20+
* @var ScopeType::PARAM|ScopeType::BOUND|ScopeType::LOCAL|ScopeType::GLOBALSCOPE|ScopeType::STATICSCOPE|null
2121
*/
2222
public $scopeType;
2323

2424
/**
25-
* @var string
25+
* @var string|null
2626
*/
2727
public $typeHint;
2828

@@ -89,7 +89,7 @@ class VariableInfo
8989
public $isForeachLoopAssociativeValue = false;
9090

9191
/**
92-
* @var string[]
92+
* @var array<ScopeType::PARAM|ScopeType::BOUND|ScopeType::LOCAL|ScopeType::GLOBALSCOPE|ScopeType::STATICSCOPE, string>
9393
*/
9494
public static $scopeTypeDescriptions = [
9595
ScopeType::LOCAL => 'variable',

0 commit comments

Comments
 (0)