Skip to content

Commit ca39fe1

Browse files
Bump to phpstan-extension 12.0 (#49)
* Updates for PHPStan 2.0 * Changes after PR review * Bump to phpstan-extension 12.0 * fix donwgrade script * Run Rector and cs fix --------- Co-authored-by: Carlos Granados <[email protected]>
1 parent 955bc8c commit ca39fe1

26 files changed

+71
-68
lines changed

.github/workflows/downgraded_release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- run: mkdir rector-local
3333
- run: composer require rector/rector:^1.1 --working-dir rector-local --ansi
3434

35-
# downgrade to PHP 7.2
35+
# downgrade to PHP 7.4
3636
- run: rector-local/vendor/bin/rector process src --config build/rector-downgrade-php-72.php --ansi
3737

3838
# clear the dev files
@@ -56,7 +56,7 @@ jobs:
5656
run: |
5757
# separate a "git add" to add untracked (new) files too
5858
git add --all
59-
git commit -m "release PHP 7.2 downgraded"
59+
git commit -m "release PHP 7.4 downgraded"
6060
6161
# force push tag, so there is only 1 version
6262
git tag "${GITHUB_REF#refs/tags/}" --force
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Rector\Set\ValueObject\DowngradeLevelSetList;
77

88
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
9+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_74]);
1010

1111
$rectorConfig->skip(['*/tests/*']);
1212
};

build/target-repository/.github/workflows/standalone_install.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
php_version: ['7.2', '7.3', '7.4', '8.0', '8.1']
12+
php_version: ['7.4', '8.0', '8.1']
1313

1414
steps:
1515
# prepare empty composer.json that allows the phpstan extension plugin

build/target-repository/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Next level type declaration checks",
55
"license": "MIT",
66
"require": {
7-
"php": "^7.2|^8.0",
8-
"phpstan/phpstan": "^1.11",
7+
"php": "^7.4|^8.0",
8+
"phpstan/phpstan": "^2.0",
99
"webmozart/assert": "^1.11"
1010
},
1111
"autoload": {

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
"license": "MIT",
66
"require": {
77
"php": "^8.2",
8-
"phpstan/phpstan": "^1.11",
8+
"phpstan/phpstan": "^2.0",
99
"webmozart/assert": "^1.11"
1010
},
1111
"require-dev": {
12-
"nikic/php-parser": "^4.19",
13-
"symplify/phpstan-extensions": "^11.4",
12+
"nikic/php-parser": "^5.0",
13+
"symplify/phpstan-extensions": "^12.0",
1414
"symplify/rule-doc-generator": "^12.1",
1515
"phpunit/phpunit": "^10.5",
16-
"rector/rector": "^1.1",
16+
"rector/rector": "^2.0",
1717
"symplify/easy-coding-standard": "^12.1",
1818
"phpstan/extension-installer": "^1.3",
1919
"tomasvotruba/class-leak": "^0.2",

phpstan.neon

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ parameters:
1919

2020
# overly detailed generics
2121
- '#Rector\\TypePerfect\\Tests\\Rules\\(.*?) generic (class|interface)#'
22-
- '#Method Rector\\TypePerfect\\Tests\\Rules\\(.*?)testRule\(\) has parameter \$expectedErrorsWithLines with no value type specified in iterable type array#'
22+
23+
-
24+
identifier: phpstanApi.instanceofType
25+
paths:
26+
- src/Printer/CollectorMetadataPrinter.php
27+
- src/Rules/NarrowPrivateClassMethodParamTypeRule.php
28+
- src/Rules/NoArrayAccessOnObjectRule.php
29+
- src/Rules/ReturnNullOverFalseRule.php
30+
31+
-
32+
identifier: phpstanApi.instanceofAssumption
33+
paths:
34+
- src/Rules/NoParamTypeRemovalRule.php

src/Matcher/ClassMethodCallReferenceResolver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Node\MethodCallableNode;
1111
use PHPStan\Type\ThisType;
1212
use PHPStan\Type\TypeCombinator;
13-
use PHPStan\Type\TypeWithClassName;
1413
use Rector\TypePerfect\ValueObject\MethodCallReference;
1514

1615
final class ClassMethodCallReferenceResolver
@@ -40,12 +39,12 @@ public function resolve(MethodCall|MethodCallableNode $methodCallOrMethodCallabl
4039
return null;
4140
}
4241

43-
if (! $callerType instanceof TypeWithClassName) {
42+
if (count($callerType->getObjectClassNames()) !== 1) {
4443
return null;
4544
}
4645

4746
// move to the class where method is defined, e.g. parent class defines the method, so it should be checked there
48-
$className = $callerType->getClassName();
47+
$className = $callerType->getObjectClassNames()[0];
4948
$methodNameString = $methodName->toString();
5049

5150
return new MethodCallReference($className, $methodNameString);

src/Printer/CollectorMetadataPrinter.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
use PHPStan\Analyser\Scope;
1818
use PHPStan\Reflection\ExtendedMethodReflection;
1919
use PHPStan\Reflection\ParametersAcceptorSelector;
20-
use PHPStan\Type\ArrayType;
21-
use PHPStan\Type\BooleanType;
22-
use PHPStan\Type\ClassStringType;
2320
use PHPStan\Type\ClosureType;
24-
use PHPStan\Type\Enum\EnumCaseObjectType;
2521
use PHPStan\Type\IntegerRangeType;
2622
use PHPStan\Type\IntersectionType;
2723
use PHPStan\Type\MixedType;
@@ -35,11 +31,11 @@
3531

3632
final readonly class CollectorMetadataPrinter
3733
{
38-
private Standard $printerStandard;
34+
private Standard $standard;
3935

40-
public function __construct()
41-
{
42-
$this->printerStandard = new Standard();
36+
public function __construct(
37+
) {
38+
$this->standard = new Standard();
4339
}
4440

4541
public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodReflection $extendedMethodReflection, Scope $scope): string
@@ -100,7 +96,7 @@ public function printParamTypesToString(ClassMethod $classMethod, ?string $class
10096
$paramType = $this->resolveSortedTypes($paramType, $className);
10197
}
10298

103-
$printedParamType = $this->printerStandard->prettyPrint([$paramType]);
99+
$printedParamType = $this->standard->prettyPrint([$paramType]);
104100
$printedParamType = str_replace('\Closure', 'callable', $printedParamType);
105101
$printedParamType = ltrim($printedParamType, '\\');
106102
$printedParamType = str_replace('|\\', '|', $printedParamType);
@@ -160,15 +156,15 @@ private function resolveSortedTypes(UnionType|NodeIntersectionType $paramType, ?
160156

161157
private function printTypeToString(Type $type): string
162158
{
163-
if ($type instanceof ClassStringType) {
159+
if ($type->isClassString()->yes()) {
164160
return 'string';
165161
}
166162

167-
if ($type instanceof ArrayType) {
163+
if ($type->isArray()->yes()) {
168164
return 'array';
169165
}
170166

171-
if ($type instanceof BooleanType) {
167+
if ($type->isBoolean()->yes()) {
172168
return 'bool';
173169
}
174170

@@ -180,8 +176,8 @@ private function printTypeToString(Type $type): string
180176
return 'callable';
181177
}
182178

183-
if ($type instanceof EnumCaseObjectType) {
184-
return $type->getClassName();
179+
if (count($type->getEnumCases()) === 1) {
180+
return $type->getEnumCases()[0]->getClassName();
185181
}
186182

187183
return $type->describe(VerbosityLevel::typeOnly());

src/Printer/NodeComparator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace Rector\TypePerfect\Printer;
66

77
use PhpParser\Node;
8-
use PhpParser\PrettyPrinter\Standard;
8+
use PHPStan\Node\Printer\Printer;
99

1010
final readonly class NodeComparator
1111
{
1212
public function __construct(
13-
private Standard $standard
13+
private Printer $printer
1414
) {
1515
}
1616

@@ -20,6 +20,6 @@ public function areNodesEqual(Node $firstNode, Node $secondNode): bool
2020
$firstNode->setAttribute('comments', null);
2121
$secondNode->setAttribute('comments', null);
2222

23-
return $this->standard->prettyPrint([$firstNode]) === $this->standard->prettyPrint([$secondNode]);
23+
return $this->printer->prettyPrint([$firstNode]) === $this->printer->prettyPrint([$secondNode]);
2424
}
2525
}

src/Reflection/ReflectionParser.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class ReflectionParser
2626
public function __construct()
2727
{
2828
$parserFactory = new ParserFactory();
29-
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7);
29+
$this->parser = $parserFactory->createForNewestSupportedVersion();
3030
}
3131

3232
public function parseClassReflection(ClassReflection $classReflection): ?ClassLike
@@ -79,12 +79,6 @@ private function parseFilenameToClass(string $fileName): ClassLike|null
7979
private function findFirstClassLike(array $nodes): ?ClassLike
8080
{
8181
$nodeFinder = new NodeFinder();
82-
83-
$foundClassLike = $nodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
84-
if ($foundClassLike instanceof ClassLike) {
85-
return $foundClassLike;
86-
}
87-
88-
return null;
82+
return $nodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
8983
}
9084
}

0 commit comments

Comments
 (0)