From f3069fd72f660a86a5c1929a9ce584fdde593f28 Mon Sep 17 00:00:00 2001 From: Tyrone Tudehope Date: Fri, 14 Mar 2025 08:32:13 +0200 Subject: [PATCH] Upgrade to phpstan v2 --- .github/workflows/install_in_craftcms.yaml | 2 +- composer.json | 6 +++--- phpstan.neon | 4 ++-- scripts/signature-builder.php | 20 ++++++++++++++------ scripts/signature-diff.php | 17 +++++++++++++---- sets/craft-cms-50.php | 2 +- src/SetList.php | 4 +--- src/SignatureConfigurator.php | 2 +- 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.github/workflows/install_in_craftcms.yaml b/.github/workflows/install_in_craftcms.yaml index 95b1b5d..173c4f8 100644 --- a/.github/workflows/install_in_craftcms.yaml +++ b/.github/workflows/install_in_craftcms.yaml @@ -16,7 +16,7 @@ jobs: # see https://github.com/shivammathur/setup-php - uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.2 coverage: none - run: git clone https://github.com/craftcms/cms.git --depth 1 diff --git a/composer.json b/composer.json index 9d51433..33fcc69 100644 --- a/composer.json +++ b/composer.json @@ -2,13 +2,13 @@ "name": "craftcms/rector", "description": "Rector sets to automate Craft CMS upgrades", "require": { - "rector/rector": "^1.0.0" + "rector/rector": "^2.0" }, "require-dev": { "phpunit/phpunit": "^9.0", "craftcms/cms": "^4.0.0|^5.0.0", "craftcms/ecs": "dev-main", - "phpstan/phpstan": "^1.10", + "phpstan/phpstan": "^2.0", "symfony/var-exporter": "^6.0" }, "autoload": { @@ -34,4 +34,4 @@ "craftcms/plugin-installer": true } } -} +} \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon index a3c7b0d..e8e4d4d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,6 @@ parameters: level: 8 - checkGenericClassInNonGenericObjectType: false - paths: - ecs.php - scripts @@ -11,6 +9,8 @@ parameters: - tests ignoreErrors: + - identifier: missingType.generics + - '#expects string, ((.*?)string|string(.*?)) given#' # false positives on static calls diff --git a/scripts/signature-builder.php b/scripts/signature-builder.php index d142dac..9418573 100644 --- a/scripts/signature-builder.php +++ b/scripts/signature-builder.php @@ -47,7 +47,7 @@ function normalizeNamespace(string $namespace): string $excludeNamespaces = array_map(fn($namespace) => normalizeNamespace($namespace), explode(',', $excludeNamespaces)); } -$filterNamespace = function($class) use ($namespaces, $excludeNamespaces): bool { +$filterNamespace = function ($class) use ($namespaces, $excludeNamespaces): bool { if ($namespaces) { $include = false; foreach ($namespaces as $namespace) { @@ -174,16 +174,24 @@ public function build(array $classes): array return $this->signatures; } + private function serializeTypeCallback(string $className): callable + { + return function (ReflectionNamedType|ReflectionIntersectionType $type) use ($className): string { + if ($type instanceof ReflectionIntersectionType) { + return implode('|', array_map($this->serializeTypeCallback($className), $type->getTypes())); + } + $name = $type->getName(); + return $name === 'self' ? $className : $name; + }; + } + private function serializeType(?ReflectionType $type, string $className): ?string { if ($type === null) { return null; } if ($type instanceof ReflectionUnionType) { - return implode('|', array_map(function(ReflectionNamedType $type) use ($className) { - $name = $type->getName(); - return $name === 'self' ? $className : $name; - }, $type->getTypes())); + return implode('|', array_map($this->serializeTypeCallback($className), $type->getTypes())); } // todo: // if ($type instanceof ReflectionIntersectionType) { @@ -213,7 +221,7 @@ private function analyzeClass(ReflectionClass $class): void $parentClass = $class->getParentClass() ?: null; $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); - usort($properties, fn(ReflectionProperty $a, ReflectionProperty$b) => $a->getName() <=> $b->getName()); + usort($properties, fn(ReflectionProperty $a, ReflectionProperty $b) => $a->getName() <=> $b->getName()); foreach ($properties as $property) { $declaringClass = $property->getDeclaringClass(); diff --git a/scripts/signature-diff.php b/scripts/signature-diff.php index 8b0b7c0..f50948a 100644 --- a/scripts/signature-diff.php +++ b/scripts/signature-diff.php @@ -113,16 +113,25 @@ private function includeMethodParamType(string $className, string $method, int $ return !isset($oldParams[$position]) || $paramType !== $this->serializeType($oldParams[$position]->getType(), $className); } + private function serializeTypeCallback(string $className): callable + { + return function (ReflectionNamedType|ReflectionIntersectionType $type) use ($className): string { + if ($type instanceof ReflectionIntersectionType) { + return implode('|', array_map($this->serializeTypeCallback($className), $type->getTypes())); + } + $name = $type->getName(); + return $name === 'self' ? $className : $name; + }; + } + private function serializeType(?ReflectionType $type, string $className): ?string { if ($type === null) { return null; } + if ($type instanceof ReflectionUnionType) { - return implode('|', array_map(function(ReflectionNamedType $type) use ($className) { - $name = $type->getName(); - return $name === 'self' ? $className : $name; - }, $type->getTypes())); + return implode('|', array_map($this->serializeTypeCallback($className), $type->getTypes())); } // todo: // if ($type instanceof ReflectionIntersectionType) { diff --git a/sets/craft-cms-50.php b/sets/craft-cms-50.php index b5fe28b..e2677f4 100644 --- a/sets/craft-cms-50.php +++ b/sets/craft-cms-50.php @@ -9,7 +9,6 @@ use Rector\Arguments\ValueObject\ArgumentAdder; use Rector\Arguments\ValueObject\RemoveMethodCallParam; use Rector\Config\RectorConfig; -use Rector\ValueObject\Visibility; use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector; use Rector\Renaming\Rector\MethodCall\RenameMethodRector; use Rector\Renaming\Rector\Name\RenameClassRector; @@ -20,6 +19,7 @@ use Rector\Renaming\ValueObject\RenameClassConstFetch; use Rector\Renaming\ValueObject\RenameProperty; use Rector\Renaming\ValueObject\RenameStaticMethod; +use Rector\ValueObject\Visibility; use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector; use Rector\Visibility\ValueObject\ChangeMethodVisibility; diff --git a/src/SetList.php b/src/SetList.php index 4cf0ecd..8930192 100644 --- a/src/SetList.php +++ b/src/SetList.php @@ -4,9 +4,7 @@ namespace craft\rector; -use Rector\Set\Contract\SetListInterface; - -final class SetList implements SetListInterface +final class SetList { /** * @var string diff --git a/src/SignatureConfigurator.php b/src/SignatureConfigurator.php index 1411faf..c1c686e 100644 --- a/src/SignatureConfigurator.php +++ b/src/SignatureConfigurator.php @@ -76,7 +76,7 @@ public static function configure(RectorConfig $rectorConfig, string $name): void private static function type(string $type): Type { - if (! isset(self::$types[$type])) { + if (!isset(self::$types[$type])) { self::$types[$type] = self::createType($type); }