Skip to content

Commit

Permalink
Update to PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Dec 29, 2024
1 parent f123356 commit 447abec
Show file tree
Hide file tree
Showing 96 changed files with 4,013 additions and 77 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/all_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
fail-fast: false
matrix:
php-version:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
- "8.4"

steps:
- name: "Checkout"
Expand All @@ -44,4 +44,8 @@ jobs:
run: "composer install --no-interaction --no-progress"

- name: "Run tests"
run: "composer tests"
run: "composer tests-without-psalm"

- name: "Run Psalm"
if: matrix.php-version != '8.4' # Psalm does not fully support PHP 8.4 yet
run: "composer psalm"
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ To replace all the annotations that this package covers, use the set provided by

```php
use Rector\Config\RectorConfig;
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisSetList;
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAnnotationsToAttributesSetList;

return RectorConfig::configure()
->withSets([
PhpStaticAnalysisSetList::ANNOTATIONS_TO_ATTRIBUTES
PhpStaticAnalysisAnnotationsToAttributesSetList::ANNOTATIONS_TO_ATTRIBUTES
])
->withImportNames();
```
Expand Down Expand Up @@ -255,6 +255,32 @@ return RectorConfig::configure()
]
);
```
## Using these rules with Rector

Once you have converted your static analysis annotations to attributes, you may want to use them with Rector, so that Rector can
understand them and use them to apply its rules. To do this, when running rector we first need to temporarily convert these attributes
back to annotations, then run your Rector rules and finally convert the annotations back to attributes. To do this, use this in your
configuration:

```php
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAnnotationsToAttributesSetList;
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAttributesToAnnotationsSetList;
use Rector\Config\RectorConfig;
...

return RectorConfig::configure()
->withSets([
PhpStaticAnalysisAttributesToAnnotationsSetList::ATTRIBUTES_TO_ANNOTATIONS
])
...
//any other Rector rules or sets
...
->withSets([
PhpStaticAnalysisAnnotationsToAttributesSetList::ANNOTATIONS_TO_ATTRIBUTES
]);
```
If you use any special configuration for the Annotations to Attributes process, for example only converting some of the annotations
or setting some flags, use it in this last part of the block of code instead of the sample.

## Sponsor this project

Expand Down
28 changes: 19 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.3.1 || dev-main",
"php-static-analysis/node-visitor": "^0.3.1 || dev-main",
"rector/rector": "^0.19 || ^1.0"
"php": ">=8.1",
"php-static-analysis/attributes": "^0.4.0 || dev-main",
"php-static-analysis/node-visitor": "^0.4.0 || dev-main",
"rector/rector": "^2.0"
},
"require-dev": {
"php-static-analysis/phpstan-extension": "dev-main",
"php-static-analysis/psalm-plugin": "dev-main",
"composer/composer": "^2.8",
"php-static-analysis/phpstan-extension": "^0.4.0 || dev-main",
"php-static-analysis/psalm-plugin": "^0.4.0 || dev-main",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^9.0",
"symplify/easy-coding-standard": "^12.1",
"vimeo/psalm": "^5",
"vimeo/psalm": "dev-master",
"webmozart/assert": "^1.11"
},
"scripts": {
Expand All @@ -49,17 +50,26 @@
"@phpstan",
"@rector"
],
"tests-without-psalm": [
"@ecs",
"@phpunit",
"@phpstan",
"@rector"
],
"psalm": "psalm",
"ecs": "ecs",
"ecs-fix": "ecs --fix",
"phpunit": "phpunit",
"phpstan": "phpstan analyse",
"rector": "rector --dry-run",
"rector-fix": "rector",
"rector-debug": "rector --clear-cache --xdebug --dry-run"
"rector-debug": "rector --clear-cache --xdebug --dry-run",
"post-install-cmd": "PhpStaticAnalysis\\RectorRule\\Composer\\Plugin::onPostInstall",
"post-update-cmd": "PhpStaticAnalysis\\RectorRule\\Composer\\Plugin::onPostUpdate"
},
"config": {
"allow-plugins": {
"php-static-analysis/psalm-plugin": true,
"phpstan/extension-installer": true
},
"sort-packages": true
Expand Down
17 changes: 8 additions & 9 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
use PhpStaticAnalysis\Attributes\ImportType;
use PhpStaticAnalysis\Attributes\Impure;
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\IsReadOnly;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Mixin;
use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\Property;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\Pure;
use PhpStaticAnalysis\Attributes\RequireExtends;
use PhpStaticAnalysis\Attributes\RequireImplements;
use PhpStaticAnalysis\Attributes\Returns;
use PhpStaticAnalysis\Attributes\SelfOut;
use PhpStaticAnalysis\Attributes\Template;
use PhpStaticAnalysis\Attributes\TemplateContravariant;
use PhpStaticAnalysis\Attributes\TemplateCovariant;
use PhpStaticAnalysis\Attributes\TemplateExtends;
use PhpStaticAnalysis\Attributes\TemplateImplements;
use PhpStaticAnalysis\Attributes\TemplateUse;
use PhpStaticAnalysis\Attributes\Throws;
use Rector\Config\RectorConfig;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use PhpStaticAnalysis\Attributes\IsReadOnly;
use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\Property;
use PhpStaticAnalysis\Attributes\Returns;
use PhpStaticAnalysis\Attributes\Template;
use PhpStaticAnalysis\Attributes\Type;
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector;
use Rector\Config\RectorConfig;
use Rector\Php80\ValueObject\AnnotationToAttribute;

return RectorConfig::configure()
->withConfiguredRule(
Expand Down Expand Up @@ -81,5 +81,4 @@
'excludeAnnotations' => [],
'useTypeAttributeForTypeClassAnnotation' => false,
]
)
->withImportNames();
);
11 changes: 11 additions & 0 deletions config/sets/php-static-analysis-attributes-to-annotations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use PhpStaticAnalysis\RectorRule\AttributesToAnnotationsRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([
AttributesToAnnotationsRector::class
]);
29 changes: 29 additions & 0 deletions patches/rector-rector-src-application-fileprocessor-php.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--- /dev/null
+++ ../src/Application/FileProcessor.php
@@ -3,6 +3,7 @@
declare (strict_types=1);
namespace Rector\Application;

+use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use RectorPrefix202412\Nette\Utils\FileSystem;
use PHPStan\AnalysedCodeException;
use PHPStan\Parser\ParserErrorsException;
@@ -66,7 +67,9 @@
* @readonly
*/
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator;
- public function __construct(BetterStandardPrinter $betterStandardPrinter, RectorNodeTraverser $rectorNodeTraverser, SymfonyStyle $symfonyStyle, FileDiffFactory $fileDiffFactory, ChangedFilesDetector $changedFilesDetector, ErrorFactory $errorFactory, FilePathHelper $filePathHelper, PostFileProcessor $postFileProcessor, RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator)
+ public function __construct(BetterStandardPrinter $betterStandardPrinter, RectorNodeTraverser $rectorNodeTraverser, SymfonyStyle $symfonyStyle, FileDiffFactory $fileDiffFactory, ChangedFilesDetector $changedFilesDetector, ErrorFactory $errorFactory, FilePathHelper $filePathHelper, PostFileProcessor $postFileProcessor, RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
+ private PhpDocInfoFactory $phpDocInfoFactory
+ )
{
$this->betterStandardPrinter = $betterStandardPrinter;
$this->rectorNodeTraverser = $rectorNodeTraverser;
@@ -90,6 +93,7 @@
$fileHasChanged = \false;
$filePath = $file->getFilePath();
do {
+ $this->phpDocInfoFactory->clearCache();
$file->changeHasChanged(\false);
// 1. change nodes with Rector Rules
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts());
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- /dev/null
+++ ../src/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
@@ -55,6 +55,10 @@
$this->annotationNaming = $annotationNaming;
$this->phpDocNodeByTypeFinder = $phpDocNodeByTypeFinder;
}
+ public function clearCache(): void
+ {
+ $this->phpDocInfosByObjectId = [];
+ }
public function createFromNodeOrEmpty(Node $node) : \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo
{
// already added

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--- /dev/null
+++ ../src/PhpParser/NodeTraverser/RectorNodeTraverser.php
@@ -7,6 +7,8 @@
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
+use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector;
+use PhpStaticAnalysis\RectorRule\AttributesToAnnotationsRector;
use Rector\Configuration\ConfigurationRuleFilter;
use Rector\Contract\Rector\RectorInterface;
use Rector\VersionBonding\PhpVersionedFilter;
@@ -97,6 +99,23 @@
$this->visitors = $this->phpVersionedFilter->filter($this->rectors);
// filter by configuration
$this->visitors = $this->configurationRuleFilter->filter($this->visitors);
+
+ $first = [];
+ $middle = [];
+ $last = [];
+
+ foreach ($this->visitors as $visitor) {
+ if ($visitor instanceof AttributesToAnnotationsRector) {
+ $first[] = $visitor;
+ } elseif ($visitor instanceof AnnotationsToAttributesRector) {
+ $last[] = $visitor;
+ } else {
+ $middle[] = $visitor;
+ }
+ }
+
+ $this->visitors = array_merge($first, $middle, $last);
+
$this->areNodeVisitorsPrepared = \true;
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAnnotationsToAttributesSetList;
use Rector\Config\RectorConfig;
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisSetList;

return RectorConfig::configure()
->withPaths([
Expand All @@ -15,5 +15,5 @@
__DIR__ . '/tests/SpecialFixture',
])
->withSets([
PhpStaticAnalysisSetList::ANNOTATIONS_TO_ATTRIBUTES
PhpStaticAnalysisAnnotationsToAttributesSetList::ANNOTATIONS_TO_ATTRIBUTES
]);
13 changes: 11 additions & 2 deletions src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -53,6 +52,7 @@
use PhpStaticAnalysis\Attributes\Property;
use PhpStaticAnalysis\Attributes\Returns;
use PhpStaticAnalysis\Attributes\Type;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
Expand All @@ -62,6 +62,7 @@
use Rector\Php80\NodeManipulator\AttributeGroupNamedArgumentManipulator;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -207,6 +208,8 @@ public function getNodeTypes(): array
#[Param(node: 'Stmt\Class_|Stmt\ClassConst|Stmt\ClassMethod|Stmt\Function_|Stmt\Interface_|Stmt\Property|Stmt\Trait_')]
public function refactor(Node $node): ?Node
{
$nodeModified = false;

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);

$attributeGroups = [];
Expand All @@ -215,6 +218,8 @@ public function refactor(Node $node): ?Node
}

if ($attributeGroups !== []) {
$nodeModified = true;

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

$this->attributeGroupNamedArgumentManipulator->decorate($attributeGroups);
Expand Down Expand Up @@ -262,6 +267,8 @@ public function refactor(Node $node): ?Node
$useAttributeGroups = $this->processAnnotations($phpDocInfo);

if ($useAttributeGroups !== []) {
$nodeModified = true;

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);

$this->attributeGroupNamedArgumentManipulator->decorate($useAttributeGroups);
Expand All @@ -276,7 +283,7 @@ public function refactor(Node $node): ?Node
$node->attrGroups = array_merge($node->attrGroups, $attributeGroups);
}

return $node;
return $nodeModified ? $node : null;
}

#[Returns('AttributeGroup[]')]
Expand Down Expand Up @@ -350,6 +357,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
}
$attributeComment = $tagValueNode->description;
break;
case $tagValueNode instanceof DoctrineAnnotationTagValueNode:
case $tagValueNode instanceof DeprecatedTagValueNode:
case $tagValueNode instanceof GenericTagValueNode:
$args = [];
Expand Down Expand Up @@ -442,6 +450,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
return $attributeGroups;
}

#[Returns('PhpVersion::PHP_80')]
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
Expand Down
Loading

0 comments on commit 447abec

Please sign in to comment.