Skip to content

Commit

Permalink
Bump to phpstan-extension 12.0 (#49)
Browse files Browse the repository at this point in the history
* 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 <barel.barelon@gmail.com>
  • Loading branch information
samsonasik and carlos-granados authored Dec 12, 2024
1 parent 955bc8c commit ca39fe1
Show file tree
Hide file tree
Showing 26 changed files with 71 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- run: mkdir rector-local
- run: composer require rector/rector:^1.1 --working-dir rector-local --ansi

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

# clear the dev files
Expand All @@ -56,7 +56,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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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]);

$rectorConfig->skip(['*/tests/*']);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php_version: ['7.2', '7.3', '7.4', '8.0', '8.1']
php_version: ['7.4', '8.0', '8.1']

steps:
# prepare empty composer.json that allows the phpstan extension plugin
Expand Down
4 changes: 2 additions & 2 deletions build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Next level type declaration checks",
"license": "MIT",
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.11",
"php": "^7.4|^8.0",
"phpstan/phpstan": "^2.0",
"webmozart/assert": "^1.11"
},
"autoload": {
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"license": "MIT",
"require": {
"php": "^8.2",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan": "^2.0",
"webmozart/assert": "^1.11"
},
"require-dev": {
"nikic/php-parser": "^4.19",
"symplify/phpstan-extensions": "^11.4",
"nikic/php-parser": "^5.0",
"symplify/phpstan-extensions": "^12.0",
"symplify/rule-doc-generator": "^12.1",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.1",
"rector/rector": "^2.0",
"symplify/easy-coding-standard": "^12.1",
"phpstan/extension-installer": "^1.3",
"tomasvotruba/class-leak": "^0.2",
Expand Down
14 changes: 13 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ parameters:

# overly detailed generics
- '#Rector\\TypePerfect\\Tests\\Rules\\(.*?) generic (class|interface)#'
- '#Method Rector\\TypePerfect\\Tests\\Rules\\(.*?)testRule\(\) has parameter \$expectedErrorsWithLines with no value type specified in iterable type array#'

-
identifier: phpstanApi.instanceofType
paths:
- src/Printer/CollectorMetadataPrinter.php
- src/Rules/NarrowPrivateClassMethodParamTypeRule.php
- src/Rules/NoArrayAccessOnObjectRule.php
- src/Rules/ReturnNullOverFalseRule.php

-
identifier: phpstanApi.instanceofAssumption
paths:
- src/Rules/NoParamTypeRemovalRule.php
5 changes: 2 additions & 3 deletions src/Matcher/ClassMethodCallReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Node\MethodCallableNode;
use PHPStan\Type\ThisType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use Rector\TypePerfect\ValueObject\MethodCallReference;

final class ClassMethodCallReferenceResolver
Expand Down Expand Up @@ -40,12 +39,12 @@ public function resolve(MethodCall|MethodCallableNode $methodCallOrMethodCallabl
return null;
}

if (! $callerType instanceof TypeWithClassName) {
if (count($callerType->getObjectClassNames()) !== 1) {
return null;
}

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

return new MethodCallReference($className, $methodNameString);
Expand Down
24 changes: 10 additions & 14 deletions src/Printer/CollectorMetadataPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
Expand All @@ -35,11 +31,11 @@

final readonly class CollectorMetadataPrinter
{
private Standard $printerStandard;
private Standard $standard;

public function __construct()
{
$this->printerStandard = new Standard();
public function __construct(
) {
$this->standard = new Standard();
}

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

$printedParamType = $this->printerStandard->prettyPrint([$paramType]);
$printedParamType = $this->standard->prettyPrint([$paramType]);
$printedParamType = str_replace('\Closure', 'callable', $printedParamType);
$printedParamType = ltrim($printedParamType, '\\');
$printedParamType = str_replace('|\\', '|', $printedParamType);
Expand Down Expand Up @@ -160,15 +156,15 @@ private function resolveSortedTypes(UnionType|NodeIntersectionType $paramType, ?

private function printTypeToString(Type $type): string
{
if ($type instanceof ClassStringType) {
if ($type->isClassString()->yes()) {
return 'string';
}

if ($type instanceof ArrayType) {
if ($type->isArray()->yes()) {
return 'array';
}

if ($type instanceof BooleanType) {
if ($type->isBoolean()->yes()) {
return 'bool';
}

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

if ($type instanceof EnumCaseObjectType) {
return $type->getClassName();
if (count($type->getEnumCases()) === 1) {
return $type->getEnumCases()[0]->getClassName();
}

return $type->describe(VerbosityLevel::typeOnly());
Expand Down
6 changes: 3 additions & 3 deletions src/Printer/NodeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Rector\TypePerfect\Printer;

use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Node\Printer\Printer;

final readonly class NodeComparator
{
public function __construct(
private Standard $standard
private Printer $printer
) {
}

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

return $this->standard->prettyPrint([$firstNode]) === $this->standard->prettyPrint([$secondNode]);
return $this->printer->prettyPrint([$firstNode]) === $this->printer->prettyPrint([$secondNode]);
}
}
10 changes: 2 additions & 8 deletions src/Reflection/ReflectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ReflectionParser
public function __construct()
{
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7);
$this->parser = $parserFactory->createForNewestSupportedVersion();
}

public function parseClassReflection(ClassReflection $classReflection): ?ClassLike
Expand Down Expand Up @@ -79,12 +79,6 @@ private function parseFilenameToClass(string $fileName): ClassLike|null
private function findFirstClassLike(array $nodes): ?ClassLike
{
$nodeFinder = new NodeFinder();

$foundClassLike = $nodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
if ($foundClassLike instanceof ClassLike) {
return $foundClassLike;
}

return null;
return $nodeFinder->findFirstInstanceOf($nodes, ClassLike::class);
}
}
4 changes: 0 additions & 4 deletions src/Rules/NarrowPrivateClassMethodParamTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ private function validateArgVsParamTypes(array $args, MethodCall $methodCall, Sc
continue;
}

if (! $arg instanceof Arg) {
continue;
}

$paramRuleError = $this->validateParam($param, $position, $arg->value, $scope);
if (! $paramRuleError instanceof RuleError) {
continue;
Expand Down
15 changes: 8 additions & 7 deletions src/Rules/NarrowReturnObjectTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\TypePerfect\Configuration;
use Rector\TypePerfect\NodeFinder\ReturnNodeFinder;
use Rector\TypePerfect\Reflection\MethodNodeAnalyser;
Expand Down Expand Up @@ -94,8 +93,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

/** @var TypeWithClassName $returnExprType */
$errorMessage = sprintf(self::ERROR_MESSAGE, $returnExprType->getClassName());
if (count($returnExprType->getObjectClassNames()) !== 1) {
return [];
}

$errorMessage = sprintf(self::ERROR_MESSAGE, $returnExprType->getObjectClassNames()[0]);

return [
RuleErrorBuilder::message($errorMessage)
Expand Down Expand Up @@ -127,15 +129,14 @@ private function shouldSkipScope(Scope $scope): bool

private function shouldSkipReturnExprType(Type $type): bool
{
if (! $type instanceof TypeWithClassName) {
if (count($type->getObjectClassNames()) !== 1) {
return true;
}

$classReflection = $type->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
if (count($type->getObjectClassReflections()) !== 1) {
return true;
}

return $classReflection->isAnonymous();
return $type->getObjectClassReflections()[0]->isAnonymous();
}
}
6 changes: 3 additions & 3 deletions src/Rules/NoMixedMethodCallerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -27,7 +27,7 @@
public const ERROR_MESSAGE = 'Mixed variable in a `%s->...()` can skip important errors. Make sure the type is known';

public function __construct(
private Standard $printerStandard,
private Printer $printer,
private Configuration $configuration,
) {
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$printedMethodCall = $this->printerStandard->prettyPrintExpr($node->var);
$printedMethodCall = $this->printer->prettyPrintExpr($node->var);

return [
RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $printedMethodCall))
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/NoMixedPropertyFetcherRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -26,7 +26,7 @@
public const ERROR_MESSAGE = 'Mixed property fetch in a "%s->..." can skip important errors. Make sure the type is known';

public function __construct(
private Standard $standard,
private Printer $printer,
private Configuration $configuration,
) {
}
Expand Down Expand Up @@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$printedVar = $this->standard->prettyPrintExpr($node->var);
$printedVar = $this->printer->prettyPrintExpr($node->var);

return [
RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $printedVar))
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/NoParamTypeRemovalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function processNode(Node $node, Scope $scope): array

private function resolveParentParamType(PhpMethodReflection $phpMethodReflection, int $paramPosition): Type
{
foreach ($phpMethodReflection->getVariants() as $parametersAcceptorWithPhpDoc) {
foreach ($parametersAcceptorWithPhpDoc->getParameters() as $parentParamPosition => $parameterReflectionWithPhpDoc) {
foreach ($phpMethodReflection->getVariants() as $variant) {
foreach ($variant->getParameters() as $parentParamPosition => $parameterReflectionWithPhpDoc) {
if ($paramPosition !== $parentParamPosition) {
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/ReturnNullOverFalseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\TypePerfect\Configuration;

Expand Down Expand Up @@ -75,7 +74,7 @@ public function processNode(Node $node, Scope $scope): array

$exprType = $scope->getType($return->expr);
if (! $exprType instanceof ConstantBooleanType) {
if ($exprType instanceof BooleanType) {
if ($exprType->isBoolean()->yes()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class NarrowPrivateClassMethodParamTypeRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorsWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorsWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorsWithLines): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class NarrowPublicClassMethodParamTypeRuleTest extends RuleTestCase
{
/**
* @param string[] $filePaths
* @param mixed[] $expectedErrorsWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorsWithLines
*/
#[DataProvider('provideData')]
public function testRule(array $filePaths, array $expectedErrorsWithLines): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
final class NarrowReturnObjectTypeRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(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 NoArrayAccessOnObjectRuleTest extends RuleTestCase
{
/**
* @param mixed[] $expectedErrorMessagesWithLines
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorMessagesWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
Expand Down
Loading

0 comments on commit ca39fe1

Please sign in to comment.