Skip to content

Commit

Permalink
Update to PHPStan 2.x and Rector 2.x (#9)
Browse files Browse the repository at this point in the history
* Raise min PHP version to 7.4

* Update to PHPStan 2.x and Rector 2.x

* PHPStan upgrade-guide: checkGenericClassInNonGenericObjectType

* Fix alwaysTrue errors

* Fix RuleTestCase parameter types

* Remove use of `ParametersAcceptorSelector::selectSingle()`

* Fix remaining PHPStan errors

* Use `ParserFactory->createForHostVersion()`

* Fix PHP 7.2 leftovers
  • Loading branch information
staabm authored Dec 13, 2024
1 parent 1c9472c commit d73a34a
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 26 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ jobs:

- uses: "ramsey/composer-install@v2"

# downgrade /src to PHP 7.2
- run: vendor/bin/rector process src --config build/rector-downgrade-php-72.php --ansi
# downgrade /src to PHP 7.4
- run: vendor/bin/rector process src --config build/rector-downgrade-php-74.php --ansi
- run: vendor/bin/ecs check src --fix --ansi

# copy PHP 7.2 composer
- run: cp build/composer-php-72.json composer.json
# copy PHP 7.4 composer
- run: cp build/composer-php-74.json composer.json

# clear the dev files
- run: rm -rf build .github tests stubs ecs.php phpstan.neon phpunit.xml
Expand All @@ -43,7 +43,7 @@ jobs:
run: |
# separate a "git add" to add untracked (new) files too
git add --all
git commit -m "release PHP 7.2 downgraded"
git commit -m "release PHP 7.4 downgraded"
# force push tag, so there is only 1 version
git tag "${GITHUB_REF#refs/tags/}" --force
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/various_php_install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
fail-fast: false
matrix:
php:
- 7.2
- 7.4
- 8.0
- 8.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ How to keep **cognitive complexity on 1**? Read [Keep Cognitive Complexity Low w
composer require tomasvotruba/cognitive-complexity --dev
```

The package is available on PHP 7.2-8.1 versions in tagged releases.
The package is available on PHP 7.4-8.1 versions in tagged releases.

<br>

Expand Down
4 changes: 2 additions & 2 deletions build/composer-php-72.json → build/composer-php-74.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "PHPStan rules to measure cognitive complexity of your classes and methods",
"license": "MIT",
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10"
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
use Rector\Set\ValueObject\DowngradeLevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_74]);
};
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"license": "MIT",
"require": {
"php": "^8.2",
"phpstan/phpstan": "^1.10.50",
"nikic/php-parser": "^4"
"phpstan/phpstan": "^2.0",
"nikic/php-parser": "^5"
},
"require-dev": {
"phpstan/extension-installer": "^1.3",
"phpunit/phpunit": "^10.3",
"symplify/easy-coding-standard": "^12.0",
"rector/rector": "^0.18",
"rector/rector": "^2",
"tracy/tracy": "^2.9",
"php-parallel-lint/php-parallel-lint": "^1.3"
},
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ parameters:
- src
- tests

checkGenericClassInNonGenericObjectType: false

excludePaths:
- "*/Fixture/*"
- "*/Source/*"

ignoreErrors:
- identifier: missingType.generics

# skip as always string
- '#Parameter \#1 \$currentWorkingDirectory of class PHPStan\\DependencyInjection\\ContainerFactory constructor expects string, string\|false given#'
2 changes: 1 addition & 1 deletion src/ClassReflectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public function __construct()
{
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7);
$this->phpParser = $parserFactory->createForHostVersion();

$this->nodeFinder = new NodeFinder();
}
Expand Down
9 changes: 5 additions & 4 deletions src/NodeAnalyzer/ComplexityAffectingNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Catch_;
Expand Down Expand Up @@ -71,12 +70,14 @@ public function isBreakingNode(Node $node): bool
// B1. goto LABEL, break LABEL, continue LABEL
if ($this->isInstanceOf($node, self::BREAKING_NODE_TYPES)) {
// skip empty breaks
/** @var Goto_|Break_|Continue_ $node */
if ($node instanceof Goto_ && $node->name instanceof Identifier) {
if ($node instanceof Goto_) {
return true;
}

if (($node instanceof Break_ || $node instanceof Continue_) && $node->num instanceof Expr) {
if (
($node instanceof Break_ || $node instanceof Continue_)
&& $node->num instanceof Expr
) {
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/ClassDependencyTreeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeWithClassName;
use TomasVotruba\CognitiveComplexity\AstCognitiveComplexityAnalyzer;
use TomasVotruba\CognitiveComplexity\ClassReflectionParser;
use TomasVotruba\CognitiveComplexity\Configuration;
Expand Down Expand Up @@ -70,7 +69,9 @@ public function processNode(Node $node, Scope $scope): array

$extendedMethodReflection = $classReflection->getConstructor();

$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle(
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectFromArgs(
$scope,
[],
$extendedMethodReflection->getVariants()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function provideTokensAndExpectedCognitiveComplexity(): Iterator
private function parseFileToFirstFunctionLike(string $fileContent): ClassMethod | Function_
{
$parserFactory = new ParserFactory();
$parser = $parserFactory->create(ParserFactory::ONLY_PHP7);
$parser = $parserFactory->createForHostVersion();
$nodes = $parser->parse($fileContent);

$nodeFinder = new NodeFinder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class ClassDependencyTreeRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataForTest')]
public function test(string $filePath, array $expectedErrorMessagesWithLines): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class ClassLikeCognitiveComplexityRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataForTest')]
public function test(string $filePath, array $expectedErrorMessagesWithLines): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class FunctionLikeCognitiveComplexityRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataForTest')]
public function test(string $filePath, array $expectedErrorMessagesWithLines): void
Expand Down

0 comments on commit d73a34a

Please sign in to comment.