Browse Source

Use template / generic type for annotating registries object types.

ADmad 6 years ago
parent
commit
b86e02bcfc

+ 4 - 0
phpcs.xml.dist

@@ -3,4 +3,8 @@
     <config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
 
     <rule ref="CakePHP" />
+
+    <rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation.NonFullyQualifiedClassName">
+        <exclude-pattern>Core/ObjectRegistry.php</exclude-pattern>
+    </rule>
 </ruleset>

+ 1 - 0
phpstan.neon

@@ -35,6 +35,7 @@ parameters:
         - '#Property Cake\\Controller\\Controller::\$response \(Cake\\Http\\Response\) does not accept Psr\\Http\\Message\\ResponseInterface#'
         - "#Parameter \\#2 \\$callback of function array_filter expects callable\\(mixed, mixed\\): bool, 'strlen' given.#"
         - '#Parameter \#1 \$autoload_function of function spl_autoload_register expects#'
+        - '#TObject#'
         -
             message: '#Call to function method_exists\(\) with string and [^ ]+ will always evaluate to false#'
             path: 'src/Controller/Component/AuthComponent.php'

+ 0 - 10
psalm-baseline.xml

@@ -9,11 +9,6 @@
       <code>$config</code>
     </PossiblyInvalidOperand>
   </file>
-  <file src="src/Cache/CacheRegistry.php">
-    <MoreSpecificImplementedParamType occurrences="1">
-      <code>$class</code>
-    </MoreSpecificImplementedParamType>
-  </file>
   <file src="src/Cache/Engine/FileEngine.php">
     <PossiblyInvalidOperand occurrences="1">
       <code>$this-&gt;_File-&gt;current()</code>
@@ -901,11 +896,6 @@
       <code>$level</code>
     </MoreSpecificImplementedParamType>
   </file>
-  <file src="src/Log/LogEngineRegistry.php">
-    <MoreSpecificImplementedParamType occurrences="1">
-      <code>$class</code>
-    </MoreSpecificImplementedParamType>
-  </file>
   <file src="src/Mailer/Message.php">
     <PossiblyInvalidArgument occurrences="1">
       <code>env('HTTP_HOST')</code>

+ 2 - 0
src/Cache/CacheRegistry.php

@@ -25,6 +25,8 @@ use RuntimeException;
  * An object registry for cache engines.
  *
  * Used by Cake\Cache\Cache to load and manage cache engines.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Cache\CacheEngine>
  */
 class CacheRegistry extends ObjectRegistry
 {

+ 2 - 0
src/Console/HelperRegistry.php

@@ -23,6 +23,8 @@ use Cake\Core\ObjectRegistry;
 /**
  * Registry for Helpers. Provides features
  * for lazily loading helpers.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Console\Helper>
  */
 class HelperRegistry extends ObjectRegistry
 {

+ 2 - 0
src/Console/TaskRegistry.php

@@ -23,6 +23,8 @@ use Cake\Core\ObjectRegistry;
 /**
  * Registry for Tasks. Provides features
  * for lazily loading tasks.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Console\Shell>
  */
 class TaskRegistry extends ObjectRegistry
 {

+ 2 - 0
src/Controller/ComponentRegistry.php

@@ -27,6 +27,8 @@ use Cake\Event\EventDispatcherTrait;
  * ComponentRegistry is a registry for loaded components
  *
  * Handles loading, constructing and binding events for component class objects.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Controller\Component>
  */
 class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterface
 {

+ 10 - 9
src/Core/ObjectRegistry.php

@@ -38,13 +38,14 @@ use RuntimeException;
  * @see \Cake\Controller\ComponentRegistry
  * @see \Cake\View\HelperRegistry
  * @see \Cake\Console\TaskRegistry
+ * @template TObject
  */
 abstract class ObjectRegistry implements Countable, IteratorAggregate
 {
     /**
      * Map of loaded objects.
      *
-     * @var object[]
+     * @var TObject[]
      */
     protected $_loaded = [];
 
@@ -70,7 +71,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      *
      * @param string $objectName The name/class of the object to load.
      * @param array $config Additional settings to use when loading the object.
-     * @return mixed
+     * @return TObject
      * @throws \Exception If the class cannot be found.
      */
     public function load(string $objectName, array $config = [])
@@ -180,10 +181,10 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * This method should construct and do any other initialization logic
      * required.
      *
-     * @param string|object $class The class to build.
+     * @param string|TObject $class The class to build.
      * @param string $alias The alias of the object.
      * @param array $config The Configuration settings for construction
-     * @return object
+     * @return TObject
      */
     abstract protected function _create($class, string $alias, array $config);
 
@@ -212,10 +213,10 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Get loaded object instance.
      *
      * @param string $name Name of object.
-     * @return object Object instance.
+     * @return TObject Object instance.
      * @throws \RuntimeException If not loaded or found.
      */
-    public function get(string $name): object
+    public function get(string $name)
     {
         if (!isset($this->_loaded[$name])) {
             throw new RuntimeException(sprintf('Unknown object "%s"', $name));
@@ -228,7 +229,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return object|null
+     * @return TObject|null
      */
     public function __get(string $name)
     {
@@ -250,7 +251,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Sets an object.
      *
      * @param string $name Name of a property to set.
-     * @param mixed $object Object to set.
+     * @param TObject $object Object to set.
      * @return void
      */
     public function __set(string $name, $object): void
@@ -319,7 +320,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * be attached into the event manager
      *
      * @param string $objectName The name of the object to set in the registry.
-     * @param object $object instance to store in the registry
+     * @param TObject $object instance to store in the registry
      * @return $this
      */
     public function set(string $objectName, object $object)

+ 6 - 3
src/Datasource/ConnectionRegistry.php

@@ -24,6 +24,8 @@ use Cake\Datasource\Exception\MissingDatasourceException;
  * A registry object for connection instances.
  *
  * @see \Cake\Datasource\ConnectionManager
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Datasource\ConnectionInterface>
  */
 class ConnectionRegistry extends ObjectRegistry
 {
@@ -66,12 +68,12 @@ class ConnectionRegistry extends ObjectRegistry
      * If a callable is passed as first argument, The returned value of this
      * function will be the result of the callable.
      *
-     * @param string|object|callable $class The classname or object to make.
+     * @param string|\Cake\Datasource\ConnectionInterface|callable $class The classname or object to make.
      * @param string $alias The alias of the object.
      * @param array $settings An array of settings to use for the datasource.
-     * @return object A connection with the correct settings.
+     * @return \Cake\Datasource\ConnectionInterface A connection with the correct settings.
      */
-    protected function _create($class, string $alias, array $settings): object
+    protected function _create($class, string $alias, array $settings)
     {
         if (is_callable($class)) {
             return $class($alias);
@@ -83,6 +85,7 @@ class ConnectionRegistry extends ObjectRegistry
 
         unset($settings['className']);
 
+        /** @var \Cake\Datasource\ConnectionInterface */
         return new $class($settings);
     }
 

+ 8 - 8
src/Log/LogEngineRegistry.php

@@ -18,11 +18,13 @@ namespace Cake\Log;
 
 use Cake\Core\App;
 use Cake\Core\ObjectRegistry;
-use Psr\Log\LoggerInterface;
+use Cake\Log\Engine\BaseLog;
 use RuntimeException;
 
 /**
  * Registry of loaded log engines
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Log\Engine\BaseLog>
  */
 class LogEngineRegistry extends ObjectRegistry
 {
@@ -59,13 +61,13 @@ class LogEngineRegistry extends ObjectRegistry
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      *
-     * @param string|\Psr\Log\LoggerInterface $class The classname or object to make.
+     * @param string|\Cake\Log\Engine\BaseLog $class The classname or object to make.
      * @param string $alias The alias of the object.
      * @param array $settings An array of settings to use for the logger.
-     * @return \Psr\Log\LoggerInterface The constructed logger class.
+     * @return \Cake\Log\Engine\BaseLog The constructed logger class.
      * @throws \RuntimeException when an object doesn't implement the correct interface.
      */
-    protected function _create($class, string $alias, array $settings): LoggerInterface
+    protected function _create($class, string $alias, array $settings): BaseLog
     {
         if (is_callable($class)) {
             $class = $class($alias);
@@ -80,13 +82,11 @@ class LogEngineRegistry extends ObjectRegistry
             $instance = new $class($settings);
         }
 
-        if ($instance instanceof LoggerInterface) {
+        if ($instance instanceof BaseLog) {
             return $instance;
         }
 
-        throw new RuntimeException(
-            'Loggers must implement Psr\Log\LoggerInterface.'
-        );
+        throw new RuntimeException('Loggers must instanceof ' . BaseLog::class);
     }
 
     /**

+ 3 - 1
src/Mailer/TransportRegistry.php

@@ -23,6 +23,8 @@ use RuntimeException;
 
 /**
  * An object registry for mailer transports.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\Mailer\AbstractTransport>
  */
 class TransportRegistry extends ObjectRegistry
 {
@@ -59,7 +61,7 @@ class TransportRegistry extends ObjectRegistry
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      *
-     * @param string|object $class The classname or object to make.
+     * @param string|\Cake\Mailer\AbstractTransport $class The classname or object to make.
      * @param string $alias The alias of the object.
      * @param array $config An array of settings to use for the cache engine.
      * @return \Cake\Mailer\AbstractTransport The constructed transport class.

+ 2 - 0
src/ORM/BehaviorRegistry.php

@@ -29,6 +29,8 @@ use LogicException;
  * and constructing behavior objects.
  *
  * This class also provides method for checking and dispatching behavior methods.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\ORM\Behavior>
  */
 class BehaviorRegistry extends ObjectRegistry implements EventDispatcherInterface
 {

+ 3 - 1
src/View/HelperRegistry.php

@@ -25,6 +25,8 @@ use Cake\View\Exception\MissingHelperException;
 /**
  * HelperRegistry is used as a registry for loaded helpers and handles loading
  * and constructing helper class objects.
+ *
+ * @extends \Cake\Core\ObjectRegistry<\Cake\View\Helper>
  */
 class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
 {
@@ -86,7 +88,7 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return object|null
+     * @return \Cake\View\Helper|null
      */
     public function __get(string $name)
     {