archtechx/laravel-seo

Fixing all PHPStan problems

szepeviktor opened this issue · 1 comments

We need dev-master from PHPStan and nunomaduro/larastan#946

diff --git a/composer.json b/composer.json
index 6c0e997..3e62416 100644
--- a/composer.json
+++ b/composer.json
@@ -29,9 +29,10 @@
     },
     "require-dev": {
         "orchestra/testbench": "^6.9",
-        "nunomaduro/larastan": "^0.6.10",
+        "nunomaduro/larastan": "dev-master",
         "pestphp/pest": "^1.2",
-        "pestphp/pest-plugin-laravel": "^1.0"
+        "pestphp/pest-plugin-laravel": "^1.0",
+        "phpstan/phpstan": "dev-master as 0.12.90"
     },
     "extra": {
         "laravel": {
diff --git a/phpstan.neon b/phpstan.neon
index f586106..a04c137 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -10,11 +10,9 @@ parameters:
     universalObjectCratesClasses:
         - Illuminate\Routing\Route
         - ArchTech\SEO\SEOManager
+    checkMissingIterableValueType: false

     ignoreErrors:
-        - '#SEOManager\|array\|string\|null#'
-        - '#string\|false given#'
-        - '#flipp\(\) should return#'
-        - '#\_\_set\(\) has no return typehint specified#'
-
-    checkMissingIterableValueType: false
+        # Waiting for https://github.com/phpstan/phpstan/issues/5706
+        - '#^Cannot call method (flipp|get|set)\(\) on ArchTech\\SEO\\SEOManager\|array\|string\|null\.$#'
+        - '#^Method ArchTech\\SEO\\SEOManager::flipp\(\) should return static\(ArchTech\\SEO\\SEOManager\)\|string but returns array\|string\|null\.$#'
diff --git a/src/SEOManager.php b/src/SEOManager.php
index d036034..d68a98f 100644
--- a/src/SEOManager.php
+++ b/src/SEOManager.php
@@ -82,11 +82,14 @@ class SEOManager
             : value($this->values[$key]);
     }

-    /** Set one or more values. */
+    /**
+     * Set one or more values.
+     *
+     * @param string|array<string, string> $key
+     */
     public function set(string|array $key, string|Closure|null $value = null): string|array|null
     {
         if (is_array($key)) {
-            /** @var array<string, string> $key */
             foreach ($key as $k => $v) {
                 $this->set($k, $v);
             }
@@ -166,7 +169,7 @@ class SEOManager
             ];
         }

-        $query = base64_encode(json_encode($data));
+        $query = base64_encode((string)json_encode($data));

         /** @var string $template */
         $template = $this->meta("flipp.templates.$alias");
@@ -273,7 +276,11 @@ class SEOManager
         return $this->get(Str::snake($key, '.'));
     }

-    /** Handle magic set. */
+    /**
+     * Handle magic set.
+     *
+     * @phpstan-return string|array|null
+     */
     public function __set(string $key, string $value)
     {
         return $this->set(Str::snake($key, '.'), $value);
diff --git a/src/helpers.php b/src/helpers.php
index 658df88..9a8e8f1 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -5,14 +5,21 @@ declare(strict_types=1);
 use ArchTech\SEO\SEOManager;

 if (! function_exists('seo')) {
+    /**
+     * @template T of string|array
+     * @param T|null $key
+     * @return SEOManager|string|array|null
+     */
     function seo(string|array $key = null): SEOManager|string|array|null
     {
-        if (! $key) {
+        if ($key === null) {
             return app('seo');
-        } elseif (is_array($key)) {
+        }
+
+        if (is_array($key)) {
             return app('seo')->set($key);
-        } else {
-            return app('seo')->get($key);
         }
+
+        return app('seo')->get($key);
     }
 }

It is 100% up to you! This is an inspiration, not code.

You can submit a PR after this is released in a stable version (I don't want to use dev-master here).