Skip to content

Commit c406baa

Browse files
Update to PHPStan 2.0
1 parent b62b26c commit c406baa

35 files changed

+119
-47
lines changed

.github/workflows/all_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
php-version:
17-
- "8.0"
1817
- "8.1"
1918
- "8.2"
2019
- "8.3"
20+
- "8.4"
2121

2222
steps:
2323
- name: "Checkout"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ composer.lock
33
.phpunit.result.cache
44
.idea
55
cache
6+
.codebase_patch_applied
7+
.config_patch_applied

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ However, static analysis tools like Psalm have not made this transition to attri
1010

1111
This is a Psalm plugin that allows Psalm to understand a new set of attributes that replace the PHPDoc annotations. These attributes are defined in [this repository](https://github.com/php-static-analysis/attributes)
1212

13+
NOTE: Version 0.4.0 of this plugin requires Php Parser v5. The current available version of Psalm (v5) does not support this
14+
version of the parser, so currently this library only supports the `dev-master` version of Psalm. If you need to
15+
use Psalm 5, you will need to use version 0.3 of this plugin.
16+
1317
## Example
1418

1519
In order to show how code would look with these attributes, we can look at the following example. This is how a class looks like with the current annotations:
@@ -74,6 +78,12 @@ To use this plugin, require it in Composer:
7478
composer require --dev php-static-analysis/psalm-plugin
7579
```
7680

81+
NOTE: When adding this dependency, composer will ask you
82+
if you want to allow this dependency as a composer plugin.
83+
This is needed so that this plugin can patch Psalm in order
84+
to enable its functionality. This will add an entry in your
85+
`allow-plugins` composer config entry.
86+
7787
Then run this command to enable the plugin:
7888

7989
```

composer.json

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "php-static-analysis/psalm-plugin",
33
"description": "Psalm plugin to read static analysis attributes",
4-
"type": "psalm-plugin",
4+
"type": "composer-plugin",
55
"keywords": ["dev", "static analysis"],
66
"license": "MIT",
77
"autoload": {
@@ -24,17 +24,19 @@
2424
"minimum-stability": "dev",
2525
"prefer-stable": true,
2626
"require": {
27-
"php": ">=8.0",
27+
"php": ">=8.1",
28+
"composer-plugin-api": "^2.0",
2829
"ext-simplexml": "*",
29-
"php-static-analysis/attributes": "^0.3.1 || dev-main",
30-
"php-static-analysis/node-visitor": "^0.3.1 || dev-main",
31-
"vimeo/psalm": "^5",
30+
"php-static-analysis/attributes": "^0.3.2 || dev-main",
31+
"php-static-analysis/node-visitor": "^0.3.2 || dev-main",
32+
"vimeo/psalm": "dev-master",
3233
"webmozart/assert": "^1.11"
3334
},
3435
"require-dev": {
35-
"php-static-analysis/phpstan-extension": "dev-main",
36+
"composer/composer": "^2.0",
37+
"php-static-analysis/phpstan-extension": "^0.3.2 || dev-main",
3638
"phpstan/extension-installer": "^1.3",
37-
"phpstan/phpstan": "^1.10",
39+
"phpstan/phpstan": "^1.8 | ^2.0",
3840
"phpunit/phpunit": "^9.0",
3941
"symplify/easy-coding-standard": "^12.1"
4042
},
@@ -50,7 +52,8 @@
5052
"extra": {
5153
"psalm": {
5254
"pluginClass": "PhpStaticAnalysis\\PsalmPlugin\\Plugin"
53-
}
55+
},
56+
"class": "PhpStaticAnalysis\\PsalmPlugin\\Composer\\Plugin"
5457
},
5558
"scripts": {
5659
"tests": [
@@ -64,6 +67,8 @@
6467
"ecs": "ecs",
6568
"ecs-fix": "ecs --fix",
6669
"phpunit": "phpunit",
67-
"phpstan": "phpstan analyse"
70+
"phpstan": "phpstan analyse",
71+
"post-install-cmd": "PhpStaticAnalysis\\PsalmPlugin\\Composer\\Plugin::onPostInstall",
72+
"post-update-cmd": "PhpStaticAnalysis\\PsalmPlugin\\Composer\\Plugin::onPostUpdate"
6873
}
6974
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- /dev/null
2+
+++ ../src/Psalm/Codebase.php
3+
@@ -131,7 +131,10 @@
4+
5+
public FileReferenceProvider $file_reference_provider;
6+
7+
- public StatementsProvider $statements_provider;
8+
+ /**
9+
+ * @var StatementsProvider
10+
+ */
11+
+ public $statements_provider;
12+
13+
private readonly Progress $progress;
14+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- /dev/null
2+
+++ ../src/Psalm/Config.php
3+
@@ -130,7 +130,7 @@
4+
* @psalm-suppress PropertyNotSetInConstructor
5+
* @psalm-consistent-constructor
6+
*/
7+
-final class Config
8+
+class Config
9+
{
10+
final public const DEFAULT_BASELINE_NAME = 'psalm-baseline.xml';
11+
private const DEFAULT_FILE_NAMES = [

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ parameters:
44
- src
55
- tests
66
excludePaths:
7-
- tests/data/*
7+
- tests/data/*

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<directory name="src" />
1313
<directory name="tests" />
1414
<ignoreFiles>
15-
<directory name="vendor" />
1615
<directory name="tests/data" />
1716
</ignoreFiles>
1817
</projectFiles>

src/Composer/Plugin.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace PhpStaticAnalysis\PsalmPlugin\Composer;
4+
5+
use Composer\Composer;
6+
use Composer\EventDispatcher\EventSubscriberInterface;
7+
use Composer\IO\IOInterface;
8+
use Composer\Plugin\PluginInterface;
9+
use Composer\Script\Event;
10+
use PhpStaticAnalysis\Attributes\Returns;
11+
12+
class Plugin implements PluginInterface, EventSubscriberInterface
13+
{
14+
public function activate(Composer $composer, IOInterface $io)
15+
{
16+
}
17+
18+
public function deactivate(Composer $composer, IOInterface $io)
19+
{
20+
}
21+
22+
public function uninstall(Composer $composer, IOInterface $io)
23+
{
24+
}
25+
26+
#[Returns('array<string, string>')]
27+
public static function getSubscribedEvents(): array
28+
{
29+
return [
30+
'post-install-cmd' => 'onPostInstall',
31+
'post-update-cmd' => 'onPostUpdate'
32+
];
33+
}
34+
35+
public static function onPostInstall(Event $event): void
36+
{
37+
self::applyPatches($event);
38+
}
39+
40+
public static function onPostUpdate(Event $event): void
41+
{
42+
self::applyPatches($event);
43+
}
44+
45+
private static function applyPatches(Event $event): void
46+
{
47+
/**
48+
* @var string $vendorDir
49+
*/
50+
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
51+
52+
$dependencyPath = $vendorDir . '/vimeo/psalm';
53+
$patchFile = __DIR__ . '/../../patches/vimeo-psalm-src-psalm-config-php.patch';
54+
55+
exec("patch -p1 -d $dependencyPath --forward < $patchFile");
56+
57+
$patchFile = __DIR__ . '/../../patches/vimeo-psalm-src-psalm-codebase-php.patch';
58+
59+
exec("patch -p1 -d $dependencyPath --forward < $patchFile");
60+
}
61+
}

src/Provider/AttributeStatementProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getStatementsForFile(
3838
return $this->traverseAst($ast);
3939
}
4040

41-
#[Param(args: 'mixed[]')]
41+
#[Param(args: 'array<int|string, mixed>')]
4242
public function __call(string $method, array $args): mixed
4343
{
4444
$callable = [$this->statementsProvider, $method];

0 commit comments

Comments
 (0)