Browse Source

Add native property type hints for Datasource

Corey Taylor 4 years ago
parent
commit
6d810ffd20

+ 5 - 5
src/Datasource/ConnectionManager.php

@@ -45,7 +45,7 @@ class ConnectionManager
      *
      * @var array<string>
      */
-    protected static $_aliasMap = [];
+    protected static array $_aliasMap = [];
 
     /**
      * An array mapping url schemes to fully qualified driver class names
@@ -53,7 +53,7 @@ class ConnectionManager
      * @var array<string>
      * @psalm-var array<string, class-string>
      */
-    protected static $_dsnClassMap = [
+    protected static array $_dsnClassMap = [
         'mysql' => Mysql::class,
         'postgres' => Postgres::class,
         'sqlite' => Sqlite::class,
@@ -63,9 +63,9 @@ class ConnectionManager
     /**
      * The ConnectionRegistry used by the manager.
      *
-     * @var \Cake\Datasource\ConnectionRegistry
+     * @var \Cake\Datasource\ConnectionRegistry|null
      */
-    protected static $_registry;
+    protected static ?ConnectionRegistry $_registry = null;
 
     /**
      * Configure a new connection object.
@@ -202,7 +202,7 @@ class ConnectionManager
         if (empty(static::$_config[$name])) {
             throw new MissingDatasourceConfigException(['name' => $name]);
         }
-        if (empty(static::$_registry)) {
+        if (static::$_registry === null) {
             static::$_registry = new ConnectionRegistry();
         }
 

+ 11 - 11
src/Datasource/EntityTrait.php

@@ -34,14 +34,14 @@ trait EntityTrait
      *
      * @var array
      */
-    protected $_fields = [];
+    protected array $_fields = [];
 
     /**
      * Holds all fields that have been changed and their original values for this entity.
      *
      * @var array
      */
-    protected $_original = [];
+    protected array $_original = [];
 
     /**
      * List of field names that should **not** be included in JSON or Array
@@ -49,7 +49,7 @@ trait EntityTrait
      *
      * @var array<string>
      */
-    protected $_hidden = [];
+    protected array $_hidden = [];
 
     /**
      * List of computed or virtual fields that **should** be included in JSON or array
@@ -58,7 +58,7 @@ trait EntityTrait
      *
      * @var array<string>
      */
-    protected $_virtual = [];
+    protected array $_virtual = [];
 
     /**
      * Holds a list of the fields that were modified or added after this object
@@ -66,14 +66,14 @@ trait EntityTrait
      *
      * @var array<bool>
      */
-    protected $_dirty = [];
+    protected array $_dirty = [];
 
     /**
      * Holds a cached list of getters/setters per class
      *
      * @var array
      */
-    protected static $_accessors = [];
+    protected static array $_accessors = [];
 
     /**
      * Indicates whether or not this entity is yet to be persisted.
@@ -82,21 +82,21 @@ trait EntityTrait
      *
      * @var bool
      */
-    protected $_new = true;
+    protected bool $_new = true;
 
     /**
      * List of errors per field as stored in this object.
      *
      * @var array
      */
-    protected $_errors = [];
+    protected array $_errors = [];
 
     /**
      * List of invalid fields and their data for errors upon validation/patching.
      *
      * @var array
      */
-    protected $_invalid = [];
+    protected array $_invalid = [];
 
     /**
      * Map of fields in this entity that can be safely assigned, each
@@ -109,14 +109,14 @@ trait EntityTrait
      *
      * @var array<bool>
      */
-    protected $_accessible = ['*' => true];
+    protected array $_accessible = ['*' => true];
 
     /**
      * The alias of the repository this entity came from
      *
      * @var string
      */
-    protected $_registryAlias = '';
+    protected string $_registryAlias = '';
 
     /**
      * Magic getter to access fields that have been set in this entity

+ 1 - 1
src/Datasource/FactoryLocator.php

@@ -30,7 +30,7 @@ class FactoryLocator
      *
      * @var array<callable|\Cake\Datasource\Locator\LocatorInterface>
      */
-    protected static $_modelFactories = [];
+    protected static array $_modelFactories = [];
 
     /**
      * Register a callable to generate repositories of a given type.

+ 2 - 2
src/Datasource/Locator/AbstractLocator.php

@@ -29,14 +29,14 @@ abstract class AbstractLocator implements LocatorInterface
      *
      * @var array<\Cake\Datasource\RepositoryInterface>
      */
-    protected $instances = [];
+    protected array $instances = [];
 
     /**
      * Contains a list of options that were passed to get() method.
      *
      * @var array
      */
-    protected $options = [];
+    protected array $options = [];
 
     /**
      * @inheritDoc

+ 3 - 3
src/Datasource/ModelAwareTrait.php

@@ -42,21 +42,21 @@ trait ModelAwareTrait
      *
      * @var string|null
      */
-    protected $modelClass;
+    protected ?string $modelClass = null;
 
     /**
      * A list of overridden model factory functions.
      *
      * @var array<callable|\Cake\Datasource\Locator\LocatorInterface>
      */
-    protected $_modelFactories = [];
+    protected array $_modelFactories = [];
 
     /**
      * The model type to use.
      *
      * @var string
      */
-    protected $_modelType = 'Table';
+    protected string $_modelType = 'Table';
 
     /**
      * Set the modelClass property based on conventions.

+ 2 - 2
src/Datasource/Paginator.php

@@ -42,7 +42,7 @@ class Paginator implements PaginatorInterface
      *
      * @var array
      */
-    protected $_defaultConfig = [
+    protected array $_defaultConfig = [
         'page' => 1,
         'limit' => 20,
         'maxLimit' => 100,
@@ -54,7 +54,7 @@ class Paginator implements PaginatorInterface
      *
      * @var array
      */
-    protected $_pagingParams = [];
+    protected array $_pagingParams = [];
 
     /**
      * Handles automatic pagination of model records.

+ 2 - 2
src/Datasource/QueryCacher.php

@@ -37,14 +37,14 @@ class QueryCacher
      *
      * @var \Closure|string
      */
-    protected $_key;
+    protected Closure|string $_key;
 
     /**
      * Config for cache engine.
      *
      * @var \Psr\SimpleCache\CacheInterface|string
      */
-    protected $_config;
+    protected CacheInterface|string $_config;
 
     /**
      * Constructor.

+ 7 - 7
src/Datasource/QueryTrait.php

@@ -34,7 +34,7 @@ trait QueryTrait
      *
      * @var \Cake\Datasource\RepositoryInterface
      */
-    protected $_repository;
+    protected RepositoryInterface $_repository;
 
     /**
      * A ResultSet.
@@ -44,7 +44,7 @@ trait QueryTrait
      * @var iterable|null
      * @see \Cake\Datasource\QueryTrait::setResult()
      */
-    protected $_results;
+    protected ?iterable $_results = null;
 
     /**
      * List of map-reduce routines that should be applied over the query
@@ -52,7 +52,7 @@ trait QueryTrait
      *
      * @var array
      */
-    protected $_mapReduce = [];
+    protected array $_mapReduce = [];
 
     /**
      * List of formatter classes or callbacks that will post-process the
@@ -60,14 +60,14 @@ trait QueryTrait
      *
      * @var array<callable>
      */
-    protected $_formatters = [];
+    protected array $_formatters = [];
 
     /**
      * A query cacher instance if this query has caching enabled.
      *
      * @var \Cake\Datasource\QueryCacher|null
      */
-    protected $_cache;
+    protected ?QueryCacher $_cache = null;
 
     /**
      * Holds any custom options passed using applyOptions that could not be processed
@@ -75,14 +75,14 @@ trait QueryTrait
      *
      * @var array
      */
-    protected $_options = [];
+    protected array $_options = [];
 
     /**
      * Whether the query is standalone or the product of an eager load operation.
      *
      * @var bool
      */
-    protected $_eagerLoaded = false;
+    protected bool $_eagerLoaded = false;
 
     /**
      * Set the default Table object that will be used by this query

+ 2 - 2
src/Datasource/RuleInvoker.php

@@ -32,14 +32,14 @@ class RuleInvoker
      *
      * @var string|null
      */
-    protected $name;
+    protected ?string $name = null;
 
     /**
      * Rule options
      *
      * @var array
      */
-    protected $options = [];
+    protected array $options = [];
 
     /**
      * Rule callable

+ 2 - 2
src/Datasource/RulesAwareTrait.php

@@ -34,9 +34,9 @@ trait RulesAwareTrait
     /**
      * The domain rules to be applied to entities saved by this table
      *
-     * @var \Cake\Datasource\RulesChecker
+     * @var \Cake\Datasource\RulesChecker|null
      */
-    protected $_rulesChecker;
+    protected ?RulesChecker $_rulesChecker = null;
 
     /**
      * Returns whether or not the passed entity complies with all the rules stored in

+ 6 - 6
src/Datasource/RulesChecker.php

@@ -67,42 +67,42 @@ class RulesChecker
      *
      * @var array<\Cake\Datasource\RuleInvoker>
      */
-    protected $_rules = [];
+    protected array $_rules = [];
 
     /**
      * The list of rules to check during create operations
      *
      * @var array<\Cake\Datasource\RuleInvoker>
      */
-    protected $_createRules = [];
+    protected array $_createRules = [];
 
     /**
      * The list of rules to check during update operations
      *
      * @var array<\Cake\Datasource\RuleInvoker>
      */
-    protected $_updateRules = [];
+    protected array $_updateRules = [];
 
     /**
      * The list of rules to check during delete operations
      *
      * @var array<\Cake\Datasource\RuleInvoker>
      */
-    protected $_deleteRules = [];
+    protected array $_deleteRules = [];
 
     /**
      * List of options to pass to every callable rule
      *
      * @var array
      */
-    protected $_options = [];
+    protected array $_options = [];
 
     /**
      * Whether or not to use I18n functions for translating default error messages
      *
      * @var bool
      */
-    protected $_useI18n = false;
+    protected bool $_useI18n = false;
 
     /**
      * Constructor. Takes the options to be passed to all rules.

+ 1 - 1
src/ORM/Locator/TableLocator.php

@@ -50,7 +50,7 @@ class TableLocator extends AbstractLocator implements LocatorInterface
      *
      * @var array<string, \Cake\ORM\Table>
      */
-    protected $instances = [];
+    protected array $instances = [];
 
     /**
      * Contains a list of Table objects that were created out of the

+ 1 - 4
src/ORM/Query.php

@@ -141,10 +141,7 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
     {
         parent::__construct($connection);
         $this->repository($table);
-
-        if ($this->_repository !== null) {
-            $this->addDefaultTypes($this->_repository);
-        }
+        $this->addDefaultTypes($table);
     }
 
     /**

+ 1 - 1
tests/test_app/TestApp/Command/AutoLoadModelCommand.php

@@ -7,5 +7,5 @@ use Cake\Command\Command;
 
 class AutoLoadModelCommand extends Command
 {
-    protected $modelClass = 'Posts';
+    protected ?string $modelClass = 'Posts';
 }

+ 1 - 1
tests/test_app/TestApp/Controller/ArticlesController.php

@@ -23,5 +23,5 @@ class ArticlesController extends Controller
     /**
      * @var string
      */
-    protected $modelClass = ArticlesTable::class;
+    protected ?string $modelClass = ArticlesTable::class;
 }

+ 1 - 1
tests/test_app/TestApp/Controller/CakesController.php

@@ -16,7 +16,7 @@ class CakesController extends Controller
      *
      * @var string
      */
-    protected $modelClass = 'Posts';
+    protected ?string $modelClass = 'Posts';
 
     /**
      * index method

+ 1 - 1
tests/test_app/TestApp/Controller/ControllerTestAppController.php

@@ -15,5 +15,5 @@ class ControllerTestAppController extends Controller
      *
      * @var string
      */
-    protected $modelClass = 'Posts';
+    protected ?string $modelClass = 'Posts';
 }

+ 2 - 2
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -27,9 +27,9 @@ class RequestActionController extends AppController
     /**
      * The default model to use.
      *
-     * @var string
+     * @var string|null
      */
-    protected $modelClass = 'Posts';
+    protected ?string $modelClass = 'Posts';
 
     /**
      * test_request_action method

+ 1 - 1
tests/test_app/TestApp/Controller/TestController.php

@@ -23,7 +23,7 @@ class TestController extends ControllerTestAppController
      *
      * @var string
      */
-    protected $modelClass = 'Comments';
+    protected ?string $modelClass = 'Comments';
 
     /**
      * beforeFilter handler

+ 1 - 1
tests/test_app/TestApp/Model/Entity/OpenArticleEntity.php

@@ -10,7 +10,7 @@ use Cake\ORM\Entity;
  */
 class OpenArticleEntity extends Entity
 {
-    protected $_accessible = [
+    protected array $_accessible = [
         '*' => true,
     ];
 }

+ 1 - 1
tests/test_app/TestApp/Model/Entity/OpenTag.php

@@ -10,7 +10,7 @@ use Cake\ORM\Entity;
  */
 class OpenTag extends Entity
 {
-    protected $_accessible = [
+    protected array $_accessible = [
         'tag' => true,
     ];
 }

+ 1 - 1
tests/test_app/TestApp/Model/Entity/ProtectedArticle.php

@@ -10,7 +10,7 @@ use Cake\ORM\Entity;
  */
 class ProtectedArticle extends Entity
 {
-    protected $_accessible = [
+    protected array $_accessible = [
         'title' => true,
         'body' => true,
     ];

+ 1 - 1
tests/test_app/TestApp/Model/Entity/ProtectedEntity.php

@@ -7,7 +7,7 @@ use Cake\ORM\Entity;
 
 class ProtectedEntity extends Entity
 {
-    protected $_accessible = [
+    protected array $_accessible = [
         'id' => true,
         'title' => false,
     ];

+ 1 - 1
tests/test_app/TestApp/Model/Entity/Session.php

@@ -10,7 +10,7 @@ use Cake\ORM\Entity;
  */
 class Session extends Entity
 {
-    protected $_accessible = [
+    protected array $_accessible = [
         'id' => false,
         '*' => true,
     ];

+ 1 - 1
tests/test_app/TestApp/Model/Entity/TranslateBakedArticle.php

@@ -10,7 +10,7 @@ class TranslateBakedArticle extends Entity
 {
     use TranslateTrait;
 
-    protected $_accessible = [
+    protected array $_accessible = [
         'title' => true,
         'body' => true,
     ];

+ 1 - 1
tests/test_app/TestApp/Model/Entity/VirtualUser.php

@@ -7,7 +7,7 @@ use Cake\ORM\Entity;
 
 class VirtualUser extends Entity
 {
-    protected $_virtual = [
+    protected array $_virtual = [
         'bonus',
     ];