Browse Source

Fix errors reported by static analyzers

ADmad 1 year ago
parent
commit
77cadc1cf6

+ 1 - 1
.phive/phars.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <phive xmlns="https://phar.io/phive">
-  <phar name="psalm" version="5.20.0" installed="5.20.0" location="./tools/psalm" copy="false"/>
+  <phar name="psalm" version="5.24.0" installed="5.24.0" location="./tools/psalm" copy="false"/>
 </phive>

+ 1 - 1
composer.json

@@ -62,7 +62,7 @@
         "mikey179/vfsstream": "^1.6.10",
         "mockery/mockery": "^1.6",
         "paragonie/csp-builder": "^2.3 || ^3.0",
-        "phpstan/phpstan": "^1.10.30",
+        "phpstan/phpstan": "1.11.*",
         "phpstan/extension-installer": "^1.3",
         "symplify/phpstan-rules": "^12.4",
         "phpunit/phpunit": "^10.1.0 <=10.5.3"

+ 0 - 5
phpstan-baseline.neon

@@ -109,11 +109,6 @@ parameters:
 			path: src/TestSuite/TestCase.php
 
 		-
-			message: "#^Parameter \\#1 \\$methods of method PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\<Cake\\\\ORM\\\\Table\\>\\:\\:onlyMethods\\(\\) expects array\\<int, non\\-empty\\-string\\>, array\\<int, string\\> given\\.$#"
-			count: 1
-			path: src/TestSuite/TestCase.php
-
-		-
 			message: "#^Unreachable statement \\- code above always terminates\\.$#"
 			count: 1
 			path: src/TestSuite/TestCase.php

+ 9 - 0
src/Database/Type/EnumType.php

@@ -175,6 +175,15 @@ class EnumType extends BaseType
             return $value;
         }
 
+        if (!is_string($value) && !is_int($value)) {
+            throw new InvalidArgumentException(sprintf(
+                'Unable to marshal value `%s` of type `%s` to `%s`',
+                print_r($value, true),
+                get_debug_type($value),
+                $this->enumClassName,
+            ));
+        }
+
         if ($this->backingType === 'int' && is_numeric($value) && is_string($value)) {
             $value = (int)$value;
         }

+ 0 - 1
src/Http/ServerRequest.php

@@ -1562,7 +1562,6 @@ class ServerRequest implements ServerRequestInterface
      * @param string $name The attribute name.
      * @param mixed $default The default value if the attribute has not been set.
      * @return mixed
-     * @psalm-suppress MethodSignatureMismatch
      */
     public function getAttribute(string $name, mixed $default = null): mixed
     {

+ 2 - 2
src/Http/Session/CacheSession.php

@@ -117,10 +117,10 @@ class CacheSession implements SessionHandlerInterface
     /**
      * No-op method. Always returns 0 since cache engine don't have garbage collection.
      *
-     * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
+     * @param int $max_lifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
      * @return int|false
      */
-    public function gc(int $maxlifetime): int|false
+    public function gc(int $max_lifetime): int|false
     {
         return 0;
     }

+ 2 - 2
src/Http/Session/DatabaseSession.php

@@ -180,10 +180,10 @@ class DatabaseSession implements SessionHandlerInterface
     /**
      * Helper function called on gc for database sessions.
      *
-     * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
+     * @param int $max_lifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
      * @return int|false The number of deleted sessions on success, or false on failure.
      */
-    public function gc(int $maxlifetime): int|false
+    public function gc(int $max_lifetime): int|false
     {
         return $this->_table->deleteAll(['expires <' => time()]);
     }

+ 1 - 1
src/I18n/Middleware/LocaleSelectorMiddleware.php

@@ -63,7 +63,7 @@ class LocaleSelectorMiddleware implements MiddlewareInterface
         if ($this->locales !== ['*']) {
             $locale = Locale::lookup($this->locales, $locale, true);
         }
-        if ($locale || $this->locales === ['*']) {
+        if ($locale) {
             I18n::setLocale($locale);
         }
 

+ 1 - 1
src/I18n/Parser/MoFileParser.php

@@ -119,7 +119,7 @@ class MoFileParser
 
             fseek($stream, $offsetTranslated + $i * 8);
             $length = $this->_readLong($stream, $isBigEndian);
-            if ($length < 0) {
+            if ($length < 1) {
                 throw new CakeException('Length must be > 0');
             }
 

+ 6 - 0
src/I18n/PluralRules.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\I18n;
 
 use Cake\Core\Exception\CakeException;
+use InvalidArgumentException;
 use Locale;
 
 /**
@@ -145,6 +146,11 @@ class PluralRules
     {
         $locale = Locale::canonicalize($locale);
 
+        /** @psalm-suppress TypeDoesNotContainNull */
+        if ($locale === null) {
+            throw new InvalidArgumentException('Invalid locale provided');
+        }
+
         if (!isset(static::$_rulesMap[$locale])) {
             $locale = explode('_', $locale)[0];
         }

+ 1 - 1
src/Network/Socket.php

@@ -396,7 +396,7 @@ class Socket
      */
     public function read(int $length = 1024): ?string
     {
-        if ($length < 0) {
+        if ($length < 1) {
             throw new InvalidArgumentException('Length must be greater than `0`');
         }
 

+ 1 - 0
src/Utility/Hash.php

@@ -650,6 +650,7 @@ class Hash
             }
         }
 
+        /** @psalm-suppress InvalidArgument */
         return array_filter($data, $callback ?? [static::class, '_filter']);
     }
 

+ 4 - 0
src/Validation/Validation.php

@@ -827,6 +827,10 @@ class Validation
             );
         }
 
+        if (!is_string($check) && !is_int($check)) {
+            return false;
+        }
+
         if ($backingType === 'int') {
             if (!is_numeric($check)) {
                 return false;