From 5ad40327fa1c2e607af90cfe56f033a449578475 Mon Sep 17 00:00:00 2001
From: Odinn Adalsteinsson <odinn@odinns.dk>
Date: Fri, 29 Nov 2024 16:13:13 +0100
Subject: [PATCH] feat: Upgrade to PHPStan 2.x

Upgraded to PHPStan 2.x for a 2.0 release.
---
 .github/workflows/static.yml      |  6 ++++--
 .github/workflows/tests.yml       | 27 ++++++++-------------------
 composer.json                     | 14 ++++++++++----
 phpstan-baseline.neon             |  7 +++++++
 src/RequireRequestFactoryRule.php |  9 +++++++--
 5 files changed, 36 insertions(+), 27 deletions(-)

diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml
index 4937a98..a438bfa 100644
--- a/.github/workflows/static.yml
+++ b/.github/workflows/static.yml
@@ -16,11 +16,13 @@ jobs:
       - name: Setup PHP
         uses: shivammathur/setup-php@v2
         with:
-          php-version: '8.2'
+          php-version: '8.3'
           coverage: none
+        env:
+          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
       - name: Install composer dependencies
-        uses: ramsey/composer-install@v2
+        uses: ramsey/composer-install@v3
 
       - name: Run PHPStan
         run: composer test:types
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 5c94ea2..112ae09 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -8,23 +8,12 @@ on:
 
 jobs:
   test:
-    runs-on: ${{ matrix.os }}
+    name: PHPStan
+    runs-on: ubuntu-latest
     env:
       PREVENT_OUTPUT: true
     strategy:
-      fail-fast: true
-      matrix:
-        os: [ubuntu-latest]
-        php: [8.2, 8.3]
-        laravel: [10.*, 11.*]
-        stability: [prefer-lowest, prefer-stable]
-        include:
-          - laravel: 10.*
-            testbench: 8.*
-          - laravel: 11.*
-            testbench: 9.*
-
-    name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
+      fail-fast: false
 
     steps:
       - name: Checkout code
@@ -33,18 +22,18 @@ jobs:
       - name: Setup PHP
         uses: shivammathur/setup-php@v2
         with:
-          php-version: ${{ matrix.php }}
+          php-version: 8.3
           coverage: none
+        env:
+          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
       - name: Setup problem matchers
         run: |
           echo "::add-matcher::${{ runner.tool_cache }}/php.json"
           echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
 
-      - name: Install dependencies
-        run: |
-          composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update --ansi
-          composer update --${{ matrix.stability }} --prefer-dist --no-interaction --ansi
+      - name: Install composer dependencies
+        uses: ramsey/composer-install@v3
 
       - name: Execute tests
         run: composer test:unit
diff --git a/composer.json b/composer.json
index c8f765a..490616b 100644
--- a/composer.json
+++ b/composer.json
@@ -19,15 +19,15 @@
         "php": "^8.2",
         "illuminate/contracts": "^10.0 || ^11.0",
         "illuminate/http": "^10.0 || ^11.0",
-        "phpstan/phpstan": "^1.10.57",
+        "larastan/larastan": "^3.0",
+        "phpstan/phpstan": "^2.0",
+        "worksome/coding-style": "dev-feature/phpstan-2.x",
         "worksome/request-factories": "^3.2"
     },
     "require-dev": {
         "nunomaduro/collision": "^7.10 || ^8.1",
-        "larastan/larastan": "^2.6",
         "orchestra/testbench": "^8.5.8 || ^9.0",
-        "pestphp/pest": "^2.33",
-        "worksome/coding-style": "^2.8"
+        "pestphp/pest": "^2.33"
     },
     "autoload": {
         "psr-4": {
@@ -52,6 +52,12 @@
             "@test:unit"
         ]
     },
+    "repositories": [
+        {
+            "type": "vcs",
+            "url": "https://github.com/odinns/larastan-strict-rules"
+        }
+    ],
     "config": {
         "sort-packages": true,
         "allow-plugins": {
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index e69de29..137d945 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -0,0 +1,7 @@
+parameters:
+	ignoreErrors:
+		-
+			message: '#^Function class_parents\(\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\. Use objects retrieved from ReflectionProvider instead\.$#'
+			identifier: phpstanApi.runtimeReflection
+			count: 2
+			path: src/RequireRequestFactoryRule.php
diff --git a/src/RequireRequestFactoryRule.php b/src/RequireRequestFactoryRule.php
index acc5e29..fe7b130 100755
--- a/src/RequireRequestFactoryRule.php
+++ b/src/RequireRequestFactoryRule.php
@@ -33,7 +33,9 @@ public function getNodeType(): string
 
     /**
      * @param Class_ $node
-     */
+     *
+     * @return array<int, \PHPStan\Rules\RuleError>
+    */
     public function processNode(Node $node, Scope $scope): array
     {
         if ($node->namespacedName === null) {
@@ -57,6 +59,7 @@ public function processNode(Node $node, Scope $scope): array
         return $this->checkForCorrespondingFactory($className);
     }
 
+    /** @return array<int, \PHPStan\Rules\RuleError> */
     public function checkForCorrespondingFactory(string $className): array
     {
         $subName = Str::after($className, "{$this->requestsNamespace}\\");
@@ -69,7 +72,9 @@ public function checkForCorrespondingFactory(string $className): array
         return [
             RuleErrorBuilder::message(
                 "Request \"{$className}\" does not have a corresponding Request Factory at \"{$factoryName}\"."
-            )->build()
+            )
+                ->identifier('worksome.declareStrictTypes')
+                ->build(),
         ];
     }
 }