Browse Source

Fix errors reported by static analyzers.

ADmad 3 years ago
parent
commit
adcd1cd568

+ 0 - 20
phpstan-baseline.neon

@@ -71,26 +71,6 @@ parameters:
 			path: src/Routing/RouteBuilder.php
 
 		-
-			message: "#^Method Cake\\\\Utility\\\\Hash\\:\\:_matches\\(\\) has parameter \\$data with generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
-			count: 1
-			path: src/Utility/Hash.php
-
-		-
-			message: "#^Method Cake\\\\Utility\\\\Hash\\:\\:extract\\(\\) has parameter \\$data with generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
-			count: 1
-			path: src/Utility/Hash.php
-
-		-
-			message: "#^Method Cake\\\\Utility\\\\Hash\\:\\:extract\\(\\) return type with generic interface ArrayAccess does not specify its types\\: TKey, TValue$#"
-			count: 1
-			path: src/Utility/Hash.php
-
-		-
-			message: "#^Method Cake\\\\Utility\\\\Hash\\:\\:get\\(\\) has parameter \\$data with generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
-			count: 1
-			path: src/Utility/Hash.php
-
-		-
 			message: "#^Unsafe usage of new static\\(\\)\\.$#"
 			count: 1
 			path: src/View/Form/ContextFactory.php

+ 1 - 0
phpstan.neon.dist

@@ -4,6 +4,7 @@ includes:
 parameters:
 	level: 6
 	checkMissingIterableValueType: false
+	checkGenericClassInNonGenericObjectType: false
 	treatPhpDocTypesAsCertain: false
 	bootstrapFiles:
 		- tests/bootstrap.php

+ 4 - 8
src/Database/Connection.php

@@ -124,24 +124,20 @@ class Connection implements ConnectionInterface
     {
         $this->_config = $config;
         [self::ROLE_READ => $this->readDriver, self::ROLE_WRITE => $this->writeDriver] = $this->createDrivers($config);
-
-        if (!empty($config['log'])) {
-            $this->enableQueryLogging((bool)$config['log']);
-        }
     }
 
     /**
      * Creates read and write drivers.
      *
      * @param array $config Connection config
-     * @return array<string, \Cake\Database\DriverInterface>
-     * @psalm-return array{read: \Cake\Database\DriverInterface, write: \Cake\Database\DriverInterface}
+     * @return array<string, \Cake\Database\Driver>
+     * @psalm-return array{read: \Cake\Database\Driver, write: \Cake\Database\Driver}
      */
     protected function createDrivers(array $config): array
     {
         $driver = $config['driver'] ?? '';
         if (!is_string($driver)) {
-            /** @var \Cake\Database\DriverInterface $driver */
+            assert($driver instanceof Driver);
             if (!$driver->enabled()) {
                 throw new MissingExtensionException(['driver' => get_class($driver), 'name' => $this->configName()]);
             }
@@ -150,7 +146,7 @@ class Connection implements ConnectionInterface
             return [self::ROLE_READ => $driver, self::ROLE_WRITE => $driver];
         }
 
-        /** @var class-string<\Cake\Database\DriverInterface>|null $driverClass */
+        /** @var class-string<\Cake\Database\Driver>|null $driverClass */
         $driverClass = App::className($driver, 'Database/Driver');
         if ($driverClass === null) {
             throw new MissingDriverException(['driver' => $driver, 'connection' => $this->configName()]);

+ 3 - 0
src/Datasource/ResultSetDecorator.php

@@ -21,6 +21,9 @@ use Cake\Collection\Collection;
 /**
  * Generic ResultSet decorator. This will make any traversable object appear to
  * be a database result
+ *
+ * @template T
+ * @implements \Cake\Datasource\ResultSetInterface<T>
  */
 class ResultSetDecorator extends Collection implements ResultSetInterface
 {

+ 1 - 0
src/Error/ExceptionTrap.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace Cake\Error;
 
+use Cake\Cache\InvalidArgumentException;
 use Cake\Core\InstanceConfigTrait;
 use Cake\Error\Renderer\ConsoleExceptionRenderer;
 use Cake\Error\Renderer\WebExceptionRenderer;

+ 1 - 1
src/Event/Event.php

@@ -21,7 +21,7 @@ use Cake\Core\Exception\CakeException;
 /**
  * Class Event
  *
- * @template TSubject
+ * @template TSubject of object
  * @implements \Cake\Event\EventInterface<TSubject>
  */
 class Event implements EventInterface

+ 1 - 1
src/Event/EventInterface.php

@@ -21,7 +21,7 @@ namespace Cake\Event;
  * payload. The name can be any string that uniquely identifies the event across the application, while the subject
  * represents the object that the event applies to.
  *
- * @template TSubject
+ * @template TSubject of object
  */
 interface EventInterface
 {

+ 0 - 2
src/Http/BaseApplication.php

@@ -77,7 +77,6 @@ abstract class BaseApplication implements
      * Controller factory
      *
      * @var \Cake\Http\ControllerFactoryInterface|null
-     * @phpstan-ignore-next-line
      */
     protected ?ControllerFactoryInterface $controllerFactory = null;
 
@@ -94,7 +93,6 @@ abstract class BaseApplication implements
      * @param string $configDir The directory the bootstrap configuration is held in.
      * @param \Cake\Event\EventManagerInterface|null $eventManager Application event manager instance.
      * @param \Cake\Http\ControllerFactoryInterface|null $controllerFactory Controller factory.
-     * @phpstan-ignore-next-line
      */
     public function __construct(
         string $configDir,

+ 7 - 7
src/ORM/ResultSet.php

@@ -44,10 +44,10 @@ class ResultSet implements ResultSetInterface
     /**
      * Last record fetched from the statement
      *
-     * @var \Cake\Datasource\EntityInterface|array
-     * @psalm-var T
+     * @var \Cake\Datasource\EntityInterface|array|null
+     * @psalm-var T|null
      */
-    protected EntityInterface|array $_current = [];
+    protected EntityInterface|array|null $_current;
 
     /**
      * Holds the count of records in this result set
@@ -59,7 +59,7 @@ class ResultSet implements ResultSetInterface
     /**
      * Results that have been fetched or hydrated into the results.
      *
-     * @var \SplFixedArray<\Cake\Datasource\EntityInterface|array>
+     * @var \SplFixedArray<T>
      */
     protected SplFixedArray $_results;
 
@@ -78,10 +78,10 @@ class ResultSet implements ResultSetInterface
      *
      * Part of Iterator interface.
      *
-     * @return \Cake\Datasource\EntityInterface|array
-     * @psalm-return T
+     * @return \Cake\Datasource\EntityInterface|array|null
+     * @psalm-return T|null
      */
-    public function current(): EntityInterface|array
+    public function current(): EntityInterface|array|null
     {
         return $this->_current;
     }

+ 1 - 0
tests/TestCase/Error/ExceptionTrapTest.php

@@ -24,6 +24,7 @@ use Cake\Error\Renderer\TextExceptionRenderer;
 use Cake\Error\Renderer\WebExceptionRenderer;
 use Cake\Http\Exception\MissingControllerException;
 use Cake\Http\Exception\NotFoundException;
+use Cake\Http\ServerRequest;
 use Cake\Log\Log;
 use Cake\TestSuite\TestCase;
 use Cake\Utility\Text;