Browse Source

Use psalm specific annotations.

This prevents errors in IDEs/tools which don't yet support generic types
and object-like array types.
ADmad 6 years ago
parent
commit
9de1be1efc

+ 0 - 4
phpcs.xml.dist

@@ -3,8 +3,4 @@
     <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>

+ 0 - 1
phpstan.neon

@@ -35,7 +35,6 @@ 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'

+ 1 - 1
src/Controller/Controller.php

@@ -296,7 +296,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
         if (!empty($this->modelClass)) {
             [$plugin, $class] = pluginSplit($this->modelClass, true);
             if ($class === $name) {
-                return $this->loadModel($plugin . $class);
+                return $this->loadModel((string)$plugin . $class);
             }
         }
 

+ 16 - 8
src/Core/ObjectRegistry.php

@@ -45,7 +45,8 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
     /**
      * Map of loaded objects.
      *
-     * @var TObject[]
+     * @var object[]
+     * @psalm-var TObject[]
      */
     protected $_loaded = [];
 
@@ -71,7 +72,8 @@ 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 TObject
+     * @return mixed
+     * @psalm-return TObject
      * @throws \Exception If the class cannot be found.
      */
     public function load(string $objectName, array $config = [])
@@ -181,10 +183,12 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * This method should construct and do any other initialization logic
      * required.
      *
-     * @param string|TObject $class The class to build.
+     * @param string|object $class The class to build.
      * @param string $alias The alias of the object.
      * @param array $config The Configuration settings for construction
-     * @return TObject
+     * @return object
+     * @psalm-param string|TObject $class
+     * @psalm-return TObject
      */
     abstract protected function _create($class, string $alias, array $config);
 
@@ -213,8 +217,9 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Get loaded object instance.
      *
      * @param string $name Name of object.
-     * @return TObject Object instance.
+     * @return object Object instance.
      * @throws \RuntimeException If not loaded or found.
+     * @psalm-return TObject
      */
     public function get(string $name)
     {
@@ -229,7 +234,8 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return TObject|null
+     * @return object|null
+     * @psalm-return TObject|null
      */
     public function __get(string $name)
     {
@@ -251,7 +257,8 @@ abstract class ObjectRegistry implements Countable, IteratorAggregate
      * Sets an object.
      *
      * @param string $name Name of a property to set.
-     * @param TObject $object Object to set.
+     * @param object $object Object to set.
+     * @psalm-param TObject $object
      * @return void
      */
     public function __set(string $name, $object): void
@@ -320,7 +327,8 @@ 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 TObject $object instance to store in the registry
+     * @param object $object instance to store in the registry
+     * @psalm-param TObject $object
      * @return $this
      */
     public function set(string $objectName, object $object)

+ 2 - 0
src/Core/functions.php

@@ -77,6 +77,7 @@ if (!function_exists('pluginSplit')) {
      * @param string|null $plugin Optional default plugin to use if no plugin is found. Defaults to null.
      * @return array Array with 2 indexes. 0 => plugin name, 1 => class name.
      * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#pluginSplit
+     * @psalm-return array{string|null, string}
      */
     function pluginSplit(string $name, bool $dotAppend = false, ?string $plugin = null): array
     {
@@ -86,6 +87,7 @@ if (!function_exists('pluginSplit')) {
                 $parts[0] .= '.';
             }
 
+            /** @psalm-var array{string, string}*/
             return $parts;
         }
 

+ 2 - 1
src/Mailer/AbstractTransport.php

@@ -37,7 +37,8 @@ abstract class AbstractTransport
      * Send mail
      *
      * @param \Cake\Mailer\Message $message Email mesage.
-     * @return array{headers: string, message: string}
+     * @return array
+     * @psalm-return array{headers: string, message: string}
      */
     abstract public function send(Message $message): array;
 

+ 2 - 1
src/Mailer/Email.php

@@ -366,8 +366,9 @@ class Email implements JsonSerializable, Serializable
      * Send an email using the specified content, template and layout
      *
      * @param string|array|null $content String with message or array with messages
-     * @return array{headers: string, message: string}
+     * @return array
      * @throws \BadMethodCallException
+     * @psalm-return array{headers: string, message: string}
      */
     public function send($content = null): array
     {

+ 2 - 1
src/Mailer/Renderer.php

@@ -51,7 +51,8 @@ class Renderer
      *
      * @param string $content The content.
      * @param array $types Content types to render.
-     * @return array{html?: string, text?: string} The rendered content with "html" and/or "text" keys.
+     * @return array The rendered content with "html" and/or "text" keys.
+     * @psalm-return array{html?: string, text?: string}
      */
     public function render(string $content, array $types = []): array
     {

+ 2 - 1
src/Mailer/Transport/SmtpTransport.php

@@ -168,8 +168,9 @@ class SmtpTransport extends AbstractTransport
      * Send mail
      *
      * @param \Cake\Mailer\Message $message Message instance
-     * @return array{headers: string, message: string}
+     * @return array
      * @throws \Cake\Network\Exception\SocketException
+     * @psalm-return array{headers: string, message: string}
      */
     public function send(Message $message): array
     {

+ 1 - 1
src/ORM/Association.php

@@ -380,7 +380,7 @@ abstract class Association
         if ($this->_targetTable === null) {
             if (strpos($this->_className, '.')) {
                 [$plugin] = pluginSplit($this->_className, true);
-                $registryAlias = $plugin . $this->_name;
+                $registryAlias = (string)$plugin . $this->_name;
             } else {
                 $registryAlias = $this->_name;
             }

+ 2 - 1
src/TestSuite/TestEmailTransport.php

@@ -38,7 +38,8 @@ class TestEmailTransport extends DebugTransport
      * Stores email for later assertions
      *
      * @param \Cake\Mailer\Message $message Message
-     * @return array{headers: string, message: string}
+     * @return array
+     * @psalm-return array{headers: string, message: string}
      */
     public function send(Message $message): array
     {

+ 2 - 1
src/View/Helper/PaginatorHelper.php

@@ -825,7 +825,8 @@ class PaginatorHelper extends Helper
      *
      * @param array $params Params from the numbers() method.
      * @param array $options Options from the numbers() method.
-     * @return array{0: int, 1: int} An array with the start and end numbers.
+     * @return array An array with the start and end numbers.
+     * @psalm-return array{0: int, 1: int}
      */
     protected function _getNumbersStartAndEnd(array $params, array $options): array
     {

+ 2 - 1
src/View/Helper/SecureFieldTokenTrait.php

@@ -32,7 +32,8 @@ trait SecureFieldTokenTrait
      *    generating the hash.
      * @param string[] $unlockedFields The list of fields that are excluded from
      *    field validation.
-     * @return array{fields: string, unlocked: string} The token data.
+     * @return array The token data.
+     * @psalm-return array{fields: string, unlocked: string}
      */
     protected function _buildFieldToken(string $url, array $fields, array $unlockedFields = []): array
     {

+ 2 - 1
src/View/HelperRegistry.php

@@ -88,7 +88,8 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
      * Provide public read access to the loaded objects
      *
      * @param string $name Name of property to read
-     * @return \Cake\View\Helper|null
+     * @return object|null
+     * @psalm-return \Cake\View\Helper|null
      */
     public function __get(string $name)
     {

+ 2 - 1
src/View/JsonView.php

@@ -94,7 +94,8 @@ class JsonView extends SerializedView
      *   - Setting it to a string value, uses the provided query string parameter
      *     for finding the JSONP callback name.
      *
-     * @var array{serialize:string|bool|null, jsonOptions: int|null, jsonp: bool|string|null}
+     * @var array
+     * @pslam-var array{serialize:string|bool|null, jsonOptions: int|null, jsonp: bool|string|null}
      */
     protected $_defaultConfig = [
         'serialize' => null,

+ 2 - 1
src/View/SerializedView.php

@@ -45,7 +45,8 @@ abstract class SerializedView extends View
      *   names. If true all view variables will be serialized. If null or false
      *   normal view template will be rendered.
      *
-     * @var array{serialize:string|bool|null}
+     * @var array
+     * @psalm-var array{serialize:string|bool|null}
      */
     protected $_defaultConfig = [
         'serialize' => null,

+ 2 - 1
src/View/View.php

@@ -1375,7 +1375,8 @@ class View implements EventDispatcherInterface
      *
      * @param string $name The name you want to plugin split.
      * @param bool $fallback If true uses the plugin set in the current Request when parsed plugin is not loaded
-     * @return array{0: null|string|mixed, 1: string|mixed} Array with 2 indexes. 0 => plugin name, 1 => filename.
+     * @return array Array with 2 indexes. 0 => plugin name, 1 => filename.
+     * @psalm-return array{string|null, string}
      */
     public function pluginSplit(string $name, bool $fallback = true): array
     {

+ 2 - 1
src/View/XmlView.php

@@ -103,7 +103,8 @@ class XmlView extends SerializedView
      *   For e.g. 'format' as 'attributes' instead of 'tags'.
      * - `rootNode`: Root node name. Defaults to "response".
      *
-     * @var array{serialize:string|bool|null, xmlOptions: int|null, rootNode: string|null}
+     * @var array
+     * @psalm-var array{serialize:string|bool|null, xmlOptions: int|null, rootNode: string|null}
      */
     protected $_defaultConfig = [
         'serialize' => null,