Browse Source

Add missing typehints to Core classes.

ADmad 7 years ago
parent
commit
5c3da82f0f

+ 6 - 4
src/Cache/CacheRegistry.php

@@ -53,7 +53,7 @@ class CacheRegistry extends ObjectRegistry
      * @return void
      * @throws \BadMethodCallException
      */
-    protected function _throwMissingClassError($class, $plugin)
+    protected function _throwMissingClassError(string $class, string $plugin): void
     {
         throw new BadMethodCallException(sprintf('Cache engine %s is not available.', $class));
     }
@@ -69,7 +69,7 @@ class CacheRegistry extends ObjectRegistry
      * @return \Cake\Cache\CacheEngine The constructed CacheEngine class.
      * @throws \RuntimeException when an object doesn't implement the correct interface.
      */
-    protected function _create($class, $alias, $config)
+    protected function _create($class, string $alias, array $config)
     {
         if (is_object($class)) {
             $instance = $class;
@@ -104,10 +104,12 @@ class CacheRegistry extends ObjectRegistry
      * Remove a single adapter from the registry.
      *
      * @param string $name The adapter name.
-     * @return void
+     * @return $this
      */
-    public function unload(string $name)
+    public function unload(string $name): ObjectRegistry
     {
         unset($this->_loaded[$name]);
+
+        return $this;
     }
 }

+ 1 - 1
src/Core/App.php

@@ -147,7 +147,7 @@ class App
      * @param string $namespace Namespace.
      * @return bool
      */
-    protected static function _classExistsInBase($name, $namespace)
+    protected static function _classExistsInBase(string $name, string $namespace): bool
     {
         return class_exists($namespace . $name);
     }

+ 11 - 6
src/Core/BasePlugin.php

@@ -14,6 +14,9 @@ declare(strict_types=1);
  */
 namespace Cake\Core;
 
+use Cake\Console\CommandCollection;
+use Cake\Http\MiddlewareQueue;
+use Cake\Routing\RouteBuilder;
 use InvalidArgumentException;
 use ReflectionClass;
 
@@ -103,9 +106,11 @@ class BasePlugin implements PluginInterface
     }
 
     /**
-     * {@inheritdoc}
+     * Initialization hook called from constructor.
+     *
+     * @return void
      */
-    public function initialize()
+    public function initialize(): void
     {
     }
 
@@ -221,7 +226,7 @@ class BasePlugin implements PluginInterface
     /**
      * {@inheritdoc}
      */
-    public function routes($routes)
+    public function routes(RouteBuilder $routes): void
     {
         $path = $this->getConfigPath() . 'routes.php';
         if (file_exists($path)) {
@@ -232,7 +237,7 @@ class BasePlugin implements PluginInterface
     /**
      * {@inheritdoc}
      */
-    public function bootstrap(PluginApplicationInterface $app)
+    public function bootstrap(PluginApplicationInterface $app): void
     {
         $bootstrap = $this->getConfigPath() . 'bootstrap.php';
         if (file_exists($bootstrap)) {
@@ -243,7 +248,7 @@ class BasePlugin implements PluginInterface
     /**
      * {@inheritdoc}
      */
-    public function console($commands)
+    public function console(CommandCollection $commands): CommandCollection
     {
         return $commands->addMany($commands->discoverPlugin($this->getName()));
     }
@@ -251,7 +256,7 @@ class BasePlugin implements PluginInterface
     /**
      * {@inheritdoc}
      */
-    public function middleware($middleware)
+    public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
     {
         return $middleware;
     }

+ 5 - 5
src/Core/ClassLoader.php

@@ -32,7 +32,7 @@ class ClassLoader
      *
      * @return void
      */
-    public function register()
+    public function register(): void
     {
         spl_autoload_register([$this, 'loadClass']);
     }
@@ -48,7 +48,7 @@ class ClassLoader
      * than last.
      * @return void
      */
-    public function addNamespace($prefix, $baseDir, $prepend = false)
+    public function addNamespace(string $prefix, string $baseDir, bool $prepend = false): void
     {
         $prefix = trim($prefix, '\\') . '\\';
 
@@ -73,7 +73,7 @@ class ClassLoader
      * @return string|false The mapped file name on success, or boolean false on
      * failure.
      */
-    public function loadClass($class)
+    public function loadClass(string $class)
     {
         $prefix = $class;
 
@@ -100,7 +100,7 @@ class ClassLoader
      * @return mixed Boolean false if no mapped file can be loaded, or the
      * name of the mapped file that was loaded.
      */
-    protected function _loadMappedFile($prefix, $relativeClass)
+    protected function _loadMappedFile(string $prefix, string $relativeClass)
     {
         if (!isset($this->_prefixes[$prefix])) {
             return false;
@@ -123,7 +123,7 @@ class ClassLoader
      * @param string $file The file to require.
      * @return bool True if the file exists, false if not.
      */
-    protected function _requireFile($file)
+    protected function _requireFile(string $file): bool
     {
         if (file_exists($file)) {
             require $file;

+ 2 - 2
src/Core/Configure.php

@@ -385,9 +385,9 @@ class Configure
      * Will create new PhpConfig for default if not configured yet.
      *
      * @param string $config The name of the configured adapter
-     * @return \Cake\Core\Configure\ConfigEngineInterface Engine instance or null
+     * @return \Cake\Core\Configure\ConfigEngineInterface|null Engine instance or null
      */
-    protected static function _getEngine(string $config)
+    protected static function _getEngine(string $config): ?ConfigEngineInterface
     {
         if (!isset(static::$_engines[$config])) {
             if ($config !== 'default') {

+ 1 - 1
src/Core/Configure/Engine/IniConfig.php

@@ -78,7 +78,7 @@ class IniConfig implements ConfigEngineInterface
      * @param string|null $section Only get one section, leave null to parse and fetch
      *     all sections in the ini file.
      */
-    public function __construct($path = null, $section = null)
+    public function __construct(?string $path = null, ?string $section = null)
     {
         if ($path === null) {
             $path = CONFIG;

+ 1 - 1
src/Core/Configure/Engine/JsonConfig.php

@@ -53,7 +53,7 @@ class JsonConfig implements ConfigEngineInterface
      *
      * @param string|null $path The path to read config files from. Defaults to CONFIG.
      */
-    public function __construct($path = null)
+    public function __construct(?string $path = null)
     {
         if ($path === null) {
             $path = CONFIG;

+ 1 - 1
src/Core/Configure/Engine/PhpConfig.php

@@ -59,7 +59,7 @@ class PhpConfig implements ConfigEngineInterface
      *
      * @param string|null $path The path to read config files from. Defaults to CONFIG.
      */
-    public function __construct($path = null)
+    public function __construct(?string $path = null)
     {
         if ($path === null) {
             $path = CONFIG;

+ 2 - 2
src/Core/Exception/Exception.php

@@ -78,7 +78,7 @@ class Exception extends RuntimeException
      *
      * @return array
      */
-    public function getAttributes()
+    public function getAttributes(): array
     {
         return $this->_attributes;
     }
@@ -93,7 +93,7 @@ class Exception extends RuntimeException
      * @param string|null $value The header value.
      * @return array
      */
-    public function responseHeader($header = null, $value = null)
+    public function responseHeader($header = null, $value = null): array
     {
         if ($header === null) {
             return $this->_responseHeaders;

+ 2 - 2
src/Core/InstanceConfigTrait.php

@@ -169,7 +169,7 @@ trait InstanceConfigTrait
      * @param string|null $key Key to read.
      * @return mixed
      */
-    protected function _configRead($key)
+    protected function _configRead(?string $key)
     {
         if ($key === null) {
             return $this->_config;
@@ -203,7 +203,7 @@ trait InstanceConfigTrait
      * @return void
      * @throws \Cake\Core\Exception\Exception if attempting to clobber existing config
      */
-    protected function _configWrite($key, $value, $merge = false)
+    protected function _configWrite($key, $value, $merge = false): void
     {
         if (is_string($key) && $value === null) {
             $this->_configDelete($key);

+ 12 - 12
src/Core/ObjectRegistry.php

@@ -72,7 +72,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @return mixed
      * @throws \Exception If the class cannot be found.
      */
-    public function load($objectName, array $config = [])
+    public function load(string $objectName, array $config = [])
     {
         if (is_array($config) && isset($config['className'])) {
             $name = $objectName;
@@ -166,7 +166,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @return void
      * @throws \Exception
      */
-    abstract protected function _throwMissingClassError($class, $plugin);
+    abstract protected function _throwMissingClassError(string $class, string $plugin): void;
 
     /**
      * Create an instance of a given classname.
@@ -179,7 +179,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param array $config The Configuration settings for construction
      * @return object
      */
-    abstract protected function _create($class, $alias, $config);
+    abstract protected function _create($class, string $alias, array $config);
 
     /**
      * Get the list of loaded objects.
@@ -223,7 +223,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param string $name Name of property to read
      * @return mixed
      */
-    public function __get(string $name)
+    public function __get($name)
     {
         return $this->get($name);
     }
@@ -234,7 +234,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param string $name Name of object being checked.
      * @return bool
      */
-    public function __isset(string $name)
+    public function __isset($name)
     {
         return isset($this->_loaded[$name]);
     }
@@ -246,7 +246,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param mixed $object Object to set.
      * @return void
      */
-    public function __set(string $name, $object)
+    public function __set($name, $object)
     {
         $this->set($name, $object);
     }
@@ -257,7 +257,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param string $name Name of a property to unset.
      * @return void
      */
-    public function __unset(string $name)
+    public function __unset($name)
     {
         $this->unload($name);
     }
@@ -296,7 +296,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      *
      * @return $this
      */
-    public function reset()
+    public function reset(): self
     {
         foreach (array_keys($this->_loaded) as $name) {
             $this->unload($name);
@@ -315,7 +315,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param object $object instance to store in the registry
      * @return $this
      */
-    public function set(string $objectName, $object)
+    public function set(string $objectName, $object): self
     {
         list(, $name) = pluginSplit($objectName);
 
@@ -339,7 +339,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * @param string $objectName The name of the object to remove from the registry.
      * @return $this
      */
-    public function unload(string $objectName)
+    public function unload(string $objectName): self
     {
         if (empty($this->_loaded[$objectName])) {
             list($plugin, $objectName) = pluginSplit($objectName);
@@ -360,7 +360,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      *
      * @return \ArrayIterator
      */
-    public function getIterator()
+    public function getIterator(): ArrayIterator
     {
         return new ArrayIterator($this->_loaded);
     }
@@ -380,7 +380,7 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      *
      * @return array
      */
-    public function __debugInfo(): array
+    public function __debugInfo()
     {
         $properties = get_object_vars($this);
         if (isset($properties['_loaded'])) {

+ 1 - 1
src/Core/Plugin.php

@@ -128,7 +128,7 @@ class Plugin
      * @internal
      * @return \Cake\Core\PluginCollection
      */
-    public static function getCollection()
+    public static function getCollection(): PluginCollection
     {
         if (!isset(static::$plugins)) {
             static::$plugins = new PluginCollection();

+ 4 - 3
src/Core/PluginCollection.php

@@ -16,6 +16,7 @@ namespace Cake\Core;
 
 use Cake\Core\Exception\MissingPluginException;
 use Countable;
+use Generator;
 use InvalidArgumentException;
 use Iterator;
 
@@ -191,7 +192,7 @@ class PluginCollection implements Iterator, Countable
      *
      * @return void
      */
-    public function next()
+    public function next(): void
     {
         $this->position++;
     }
@@ -211,7 +212,7 @@ class PluginCollection implements Iterator, Countable
      *
      * @return \Cake\Core\PluginInterface
      */
-    public function current()
+    public function current(): PluginInterface
     {
         $name = $this->names[$this->position];
 
@@ -257,7 +258,7 @@ class PluginCollection implements Iterator, Countable
      * @return \Generator A generator containing matching plugins.
      * @throws \InvalidArgumentException on invalid hooks
      */
-    public function with(string $hook)
+    public function with(string $hook): Generator
     {
         if (!in_array($hook, PluginInterface::VALID_HOOKS)) {
             throw new InvalidArgumentException("The `{$hook}` hook is not a known plugin hook.");

+ 10 - 6
src/Core/PluginInterface.php

@@ -13,6 +13,10 @@ declare(strict_types=1);
  */
 namespace Cake\Core;
 
+use Cake\Console\CommandCollection;
+use Cake\Http\MiddlewareQueue;
+use Cake\Routing\RouteBuilder;
+
 /**
  * Plugin Interface
  */
@@ -63,7 +67,7 @@ interface PluginInterface
      * @param \Cake\Core\PluginApplicationInterface $app The host application
      * @return void
      */
-    public function bootstrap(PluginApplicationInterface $app);
+    public function bootstrap(PluginApplicationInterface $app): void;
 
     /**
      * Add console commands for the plugin.
@@ -71,7 +75,7 @@ interface PluginInterface
      * @param \Cake\Console\CommandCollection $commands The command collection to update
      * @return \Cake\Console\CommandCollection
      */
-    public function console($commands);
+    public function console(CommandCollection $commands): CommandCollection;
 
     /**
      * Add middleware for the plugin.
@@ -79,7 +83,7 @@ interface PluginInterface
      * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update.
      * @return \Cake\Http\MiddlewareQueue
      */
-    public function middleware($middleware);
+    public function middleware(MiddlewareQueue $middleware): MiddlewareQueue;
 
     /**
      * Add routes for the plugin.
@@ -90,7 +94,7 @@ interface PluginInterface
      * @param \Cake\Routing\RouteBuilder $routes The route builder to update.
      * @return void
      */
-    public function routes($routes);
+    public function routes(RouteBuilder $routes): void;
 
     /**
      * Disables the named hook
@@ -98,7 +102,7 @@ interface PluginInterface
      * @param string $hook The hook to disable
      * @return $this
      */
-    public function disable(string $hook): self;
+    public function disable(string $hook): PluginInterface;
 
     /**
      * Enables the named hook
@@ -106,7 +110,7 @@ interface PluginInterface
      * @param string $hook The hook to disable
      * @return $this
      */
-    public function enable(string $hook): self;
+    public function enable(string $hook): PluginInterface;
 
     /**
      * Check if the named hook is enabled

+ 1 - 1
src/Core/Retry/CommandRetry.php

@@ -45,7 +45,7 @@ class CommandRetry
      * @param \Cake\Core\Retry\RetryStrategyInterface $strategy The strategy to follow should the action fail
      * @param int $retries The number of times the action has been already called
      */
-    public function __construct(RetryStrategyInterface $strategy, $retries = 1)
+    public function __construct(RetryStrategyInterface $strategy, int $retries = 1)
     {
         $this->strategy = $strategy;
         $this->retries = $retries;

+ 1 - 0
tests/TestCase/Core/Retry/CommandRetryTest.php

@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 /**
  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)

+ 2 - 1
tests/test_app/Plugin/TestPlugin/src/Plugin.php

@@ -15,6 +15,7 @@ namespace TestPlugin;
 
 use Cake\Core\BasePlugin;
 use Cake\Event\EventManagerInterface;
+use Cake\Http\MiddlewareQueue;
 
 class Plugin extends BasePlugin
 {
@@ -26,7 +27,7 @@ class Plugin extends BasePlugin
         return $events;
     }
 
-    public function middleware($middleware)
+    public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
     {
         $middleware->add(function ($req, $res, $next) {
             return $next($req, $res);

+ 1 - 1
tests/test_app/TestApp/Core/TestApp.php

@@ -8,7 +8,7 @@ class TestApp extends App
 {
     public static $existsInBaseCallback;
 
-    protected static function _classExistsInBase($name, $namespace)
+    protected static function _classExistsInBase(string $name, string $namespace): bool
     {
         $callback = static::$existsInBaseCallback;