Skip to content

Commit

Permalink
Bump to Rector 2 and PHPStan 2 (#141)
Browse files Browse the repository at this point in the history
* Update to PHPStan 2.0

* Use Rector release candidate

* Allow multiple class names in call user func collector

* bump to rector 2

* bump downgrade to 7.4

---------

Co-authored-by: Carlos Granados <[email protected]>
  • Loading branch information
TomasVotruba and carlos-granados authored Dec 12, 2024
1 parent f061193 commit 160ae3f
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 49 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,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 composer-dependency-analyser.php
Expand All @@ -42,7 +42,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
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 @@ -5,8 +5,8 @@
"license": "MIT",
"keywords": ["static analysis", "phpstan-extension"],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10.19",
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2.0.3",
"webmozart/assert": "^1.11"
},
"autoload": {
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]);
};
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
"keywords": ["static analysis", "phpstan-extension"],
"require": {
"php": "^8.2",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan": "^2.0.3",
"webmozart/assert": "^1.11",
"nikic/php-parser": "^4.19"
"nikic/php-parser": "^5.3"
},
"require-dev": {
"phpstan/extension-installer": "^1.4",
"tracy/tracy": "^2.10",
"symplify/easy-coding-standard": "^12.3",
"rector/rector": "^1.2.5",
"phpunit/phpunit": "^10.5",
"tomasvotruba/class-leak": "^0.2.11",
"tomasvotruba/type-coverage": "^0.3",
"symplify/easy-coding-standard": "^12.4",
"rector/rector": "^2.0.0",
"phpunit/phpunit": "^11.5",
"tomasvotruba/class-leak": "^1.2",
"symplify/easy-ci": "^12.1",
"shipmonk/composer-dependency-analyser": "^1.7",
"symplify/phpstan-rules": "^13.0"
"symplify/phpstan-rules": "^14.0",
"shipmonk/composer-dependency-analyser": "^1.7"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 14 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ includes:
- config/extension.neon

parameters:
type_coverage:
return_type: 99
param_type: 99
property_type: 99
# type_coverage:
# return_type: 99
# param_type: 99
# property_type: 99

level: 8

Expand All @@ -21,7 +21,14 @@ parameters:
-
identifier: missingType.generics

# overly detailed
# removes important type knowledge
-
message: '#testRule\(\) has parameter (.*?) with no value type specified in iterable type array#'
path: tests/Rules
identifier: phpstanApi.instanceofType

# not useful - we need to check reflection instance
-
path: src/MethodTypeDetector.php
identifier: phpstanApi.instanceofAssumption
-
path: src/MethodTypeDetector.php
identifier: phpstanApi.class
8 changes: 6 additions & 2 deletions src/ClassMethodCallReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ public function resolve(MethodCall $methodCall, Scope $scope): array
}

$methodCallReferences = [];
foreach ($callerType->getReferencedClasses() as $className) {
$methodCallReferences[] = new MethodCallReference($className, $methodCall->name->toString(), $isLocal);
foreach ($callerType->getReferencedClasses() as $referencedClass) {
$methodCallReferences[] = new MethodCallReference(
$referencedClass,
$methodCall->name->toString(),
$isLocal
);
}

return $methodCallReferences;
Expand Down
9 changes: 0 additions & 9 deletions src/Collectors/Callable_/AttributeCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
Expand Down Expand Up @@ -84,14 +83,6 @@ private function matchClassAndMethodExprs(Attribute $attribute): ?ClassAndMethod
return null;
}

if (! $array->items[0] instanceof ArrayItem) {
return null;
}

if (! $array->items[1] instanceof ArrayItem) {
return null;
}

$classArrayItem = $array->items[0]->value;
$methodArrayItem = $array->items[1]->value;

Expand Down
27 changes: 15 additions & 12 deletions src/Collectors/Callable_/CallableTypeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -26,7 +27,7 @@ public function __construct(

public function getNodeType(): string
{
return Expr\Array_::class;
return Array_::class;
}

/**
Expand All @@ -51,21 +52,23 @@ public function processNode(Node $node, Scope $scope): ?array
return null;
}

$typeAndMethodNames = $callableType->findTypeAndMethodNames();
if ($typeAndMethodNames === []) {
return null;
}

$classMethodReferences = [];
foreach ($typeAndMethodNames as $typeAndMethodName) {
if ($typeAndMethodName->isUnknown()) {
foreach ($callableType->getConstantArrays() as $constantArray) {
$typeAndMethodNames = $constantArray->findTypeAndMethodNames();
if ($typeAndMethodNames === []) {
continue;
}

$objectClassNames = $typeAndMethodName->getType()
->getObjectClassNames();
foreach ($objectClassNames as $objectClassName) {
$classMethodReferences[] = $objectClassName . '::' . $typeAndMethodName->getMethod();
foreach ($typeAndMethodNames as $typeAndMethodName) {
if ($typeAndMethodName->isUnknown()) {
continue;
}

$objectClassNames = $typeAndMethodName->getType()
->getObjectClassNames();
foreach ($objectClassNames as $objectClassName) {
$classMethodReferences[] = $objectClassName . '::' . $typeAndMethodName->getMethod();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collectors/FormTypeClassCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace TomasVotruba\UnusedPublic\Collectors;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ private function run()

function doFoo()
{
call_user_func([SkipPublicCallbackMethod::class, 'runHere']);
$object = new SkipPublicCallbackMethod();
call_user_func([$object, 'runHere']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

final class LocalOnlyPublicClassMethodRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

final class RelativeUnusedPublicClassMethodRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

final class UnusedPublicClassConstRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
#[DataProvider('provideDataFromBladeTemplates')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/UnusedPublicClassMethodRule/TemplatePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

final class TemplatePathsTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideDataWithTwigTemplates')]
#[DataProvider('provideDataWithBladeTemplates')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

final class UnusedPublicClassMethodRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
#[DataProvider('provideDataTests')]
#[DataProvider('provideDataSymfony')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

final class UnusedPublicPropertyRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
{
Expand Down

0 comments on commit 160ae3f

Please sign in to comment.