Browse Source

Merge pull request #16959 from cakephp/5.x-stan

Fix up Stan issues.
othercorey 3 years ago
parent
commit
e643a98741

+ 1 - 1
composer.json

@@ -115,7 +115,7 @@
         ],
         "stan-tests": "phpstan.phar analyze -c tests/phpstan.neon",
         "stan-baseline": "phpstan.phar --generate-baseline",
-        "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.9.0 psalm/phar:~5.1.0 && mv composer.backup composer.json",
+        "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.9.0 psalm/phar:~5.4.0 && mv composer.backup composer.json",
         "lowest": "validate-prefer-lowest",
         "lowest-setup": "composer update --prefer-lowest --prefer-stable --prefer-dist --no-interaction && cp composer.json composer.backup && composer require --dev dereuromark/composer-prefer-lowest && mv composer.backup composer.json",
         "test": "phpunit",

+ 1 - 0
src/Controller/Controller.php

@@ -295,6 +295,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
             return $this->components()->get($name);
         }
 
+        /** @var array<int, array<string, mixed>> $trace */
         $trace = debug_backtrace();
         $parts = explode('\\', static::class);
         trigger_error(

+ 4 - 2
src/Error/Debugger.php

@@ -107,14 +107,15 @@ class Debugger
     /**
      * Returns a reference to the Debugger singleton object instance.
      *
-     * @param string|null $class Class name.
+     * @param class-string<\Cake\Error\Debugger>|null $class Class name.
      * @return static
      */
     public static function getInstance(?string $class = null): static
     {
+        /** @var array<int, static> $instance */
         static $instance = [];
         if ($class) {
-            if (!$instance || strtolower($class) !== strtolower((string)get_class($instance[0]))) {
+            if (!$instance || strtolower($class) !== strtolower(get_class($instance[0]))) {
                 $instance[0] = new $class();
             }
         }
@@ -122,6 +123,7 @@ class Debugger
             $instance[0] = new Debugger();
         }
 
+        /** @var static */
         return $instance[0];
     }
 

+ 19 - 11
src/Http/Cookie/Cookie.php

@@ -331,9 +331,11 @@ class Cookie implements CookieInterface
     {
         $value = $this->value;
         if ($this->isExpanded) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_array($value), '$value is not an array');
+
             $value = $this->_flatten($value);
         }
+
         $headerValue = [];
         /** @var string $value */
         $headerValue[] = sprintf('%s=%s', $this->name, rawurlencode($value));
@@ -423,11 +425,13 @@ class Cookie implements CookieInterface
     public function getScalarValue(): string
     {
         if ($this->isExpanded) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_array($this->value), '$value is not an array');
+
             return $this->_flatten($this->value);
         }
 
-        /** @var string */
+        assert(is_string($this->value), '$value is not a string');
+
         return $this->value;
     }
 
@@ -654,11 +658,12 @@ class Cookie implements CookieInterface
     public function check(string $path): bool
     {
         if ($this->isExpanded === false) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_string($this->value), '$value is not a string');
             $this->value = $this->_expand($this->value);
         }
 
-        /** @psalm-suppress PossiblyInvalidArgument */
+        assert(is_array($this->value), '$value is not an array');
+
         return Hash::check($this->value, $path);
     }
 
@@ -673,11 +678,11 @@ class Cookie implements CookieInterface
     {
         $new = clone $this;
         if ($new->isExpanded === false) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_string($new->value), '$value is not a string');
             $new->value = $new->_expand($new->value);
         }
 
-        /** @psalm-suppress PossiblyInvalidArgument */
+        assert(is_array($new->value), '$value is not an array');
         $new->value = Hash::insert($new->value, $path, $value);
 
         return $new;
@@ -693,11 +698,12 @@ class Cookie implements CookieInterface
     {
         $new = clone $this;
         if ($new->isExpanded === false) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_string($new->value), '$value is not a string');
             $new->value = $new->_expand($new->value);
         }
 
-        /** @psalm-suppress PossiblyInvalidArgument */
+        assert(is_array($new->value), '$value is not an array');
+
         $new->value = Hash::remove($new->value, $path);
 
         return $new;
@@ -715,7 +721,8 @@ class Cookie implements CookieInterface
     public function read(?string $path = null): mixed
     {
         if ($this->isExpanded === false) {
-            /** @psalm-suppress PossiblyInvalidArgument */
+            assert(is_string($this->value), '$value is not a string');
+
             $this->value = $this->_expand($this->value);
         }
 
@@ -723,7 +730,8 @@ class Cookie implements CookieInterface
             return $this->value;
         }
 
-        /** @psalm-suppress PossiblyInvalidArgument */
+        assert(is_array($this->value), '$value is not an array');
+
         return Hash::get($this->value, $path);
     }
 

+ 4 - 7
src/I18n/Parser/PoFileParser.php

@@ -85,6 +85,7 @@ class PoFileParser
 
         $messages = [];
         $item = $defaults;
+        /** @var array<int, string> $stage */
         $stage = [];
 
         while ($line = fgets($stream)) {
@@ -109,20 +110,16 @@ class PoFileParser
             } elseif ($line[0] === '"') {
                 switch (count($stage)) {
                     case 2:
+                        assert(isset($stage[0]));
+                        assert(isset($stage[1]));
                         /**
-                         * @psalm-suppress PossiblyUndefinedArrayOffset
                          * @psalm-suppress InvalidArrayOffset
-                         * @psalm-suppress PossiblyNullArrayAccess
                          */
                         $item[$stage[0]][$stage[1]] .= substr($line, 1, -1);
                         break;
 
                     case 1:
-                        /**
-                         * @psalm-suppress PossiblyUndefinedArrayOffset
-                         * @psalm-suppress PossiblyInvalidOperand
-                         * @psalm-suppress PossiblyNullOperand
-                         */
+                        assert(isset($stage[0]));
                         $item[$stage[0]] .= substr($line, 1, -1);
                         break;
                 }