Browse Source

Merge pull request #11841 from cakephp/phpstan-l3

Phpstan level 3
ADmad 8 years ago
parent
commit
c969cc4668

+ 1 - 1
src/Auth/Storage/SessionStorage.php

@@ -34,7 +34,7 @@ class SessionStorage implements StorageInterface
      * Stores user record array if fetched from session or false if session
      * does not have user record.
      *
-     * @var \ArrayAccess|array|bool
+     * @var \ArrayAccess|array|false
      */
     protected $_user;
 

+ 1 - 1
src/Auth/Storage/StorageInterface.php

@@ -23,7 +23,7 @@ interface StorageInterface
     /**
      * Read user record.
      *
-     * @return array|null
+     * @return \ArrayAccess|array|null
      */
     public function read();
 

+ 3 - 1
src/Console/CommandRunner.php

@@ -257,7 +257,9 @@ class CommandRunner implements EventDispatcherInterface
     public function setEventManager(EventManager $events)
     {
         if ($this->app instanceof PluginApplicationInterface) {
-            return $this->app->setEventManager($events);
+            $this->app->setEventManager($events);
+
+            return $this;
         }
 
         throw new InvalidArgumentException('Cannot set the event manager, the application does not support events.');

+ 1 - 1
src/Core/Plugin.php

@@ -30,7 +30,7 @@ class Plugin
     /**
      * Holds a list of all loaded plugins and their configuration
      *
-     * @var \Cake\Core\PluginCollection
+     * @var \Cake\Core\PluginCollection|null
      */
     protected static $plugins;
 

+ 3 - 1
src/Core/PluginCollection.php

@@ -48,7 +48,9 @@ class PluginCollection implements Iterator, Countable
     protected $names = [];
 
     /**
-     * @var null|string
+     * Iterator position.
+     *
+     * @var int
      */
     protected $position = 0;
 

+ 1 - 1
src/Database/Query.php

@@ -105,7 +105,7 @@ class Query implements ExpressionInterface, IteratorAggregate
      * The object responsible for generating query placeholders and temporarily store values
      * associated to each of those.
      *
-     * @var \Cake\Database\ValueBinder|false|null
+     * @var \Cake\Database\ValueBinder|null
      */
     protected $_valueBinder;
 

+ 1 - 1
src/Database/Statement/StatementDecorator.php

@@ -300,7 +300,7 @@ class StatementDecorator implements StatementInterface, Countable, IteratorAggre
      *
      * @param string|null $table table name or sequence to get last insert value from
      * @param string|null $column the name of the column representing the primary key
-     * @return string
+     * @return string|int
      */
     public function lastInsertId($table = null, $column = null)
     {

+ 1 - 1
src/Database/Type/DateTimeType.php

@@ -190,7 +190,7 @@ class DateTimeType extends Type implements TypeInterface, BatchCastingInterface
      * Convert request data into a datetime object.
      *
      * @param mixed $value Request data
-     * @return \DateTimeInterface
+     * @return \DateTimeInterface|null
      */
     public function marshal($value)
     {

+ 2 - 2
src/Filesystem/Folder.php

@@ -172,7 +172,7 @@ class Folder
     public function cd($path)
     {
         $path = $this->realpath($path);
-        if (is_dir($path)) {
+        if ($path !== false && is_dir($path)) {
             return $this->path = $path;
         }
 
@@ -923,7 +923,7 @@ class Folder
      * Get the real path (taking ".." and such into account)
      *
      * @param string $path Path to resolve
-     * @return string|bool The resolved path
+     * @return string|false The resolved path
      */
     public function realpath($path)
     {

+ 1 - 1
src/Http/Client.php

@@ -122,7 +122,7 @@ class Client
      * Cookies are indexed by the cookie's domain or
      * request host name.
      *
-     * @var \Cake\Http\Client\CookieCollection
+     * @var \Cake\Http\Cookie\CookieCollection
      */
     protected $_cookies;
 

+ 1 - 1
src/Http/Client/Response.php

@@ -440,7 +440,7 @@ class Response extends Message implements ResponseInterface
      * Get the value of a single cookie.
      *
      * @param string $name The name of the cookie value.
-     * @return string|null Either the cookie's value or null when the cookie is undefined.
+     * @return string|array|null Either the cookie's value or null when the cookie is undefined.
      */
     public function getCookie($name)
     {

+ 3 - 1
src/Http/Server.php

@@ -210,7 +210,9 @@ class Server implements EventDispatcherInterface
     public function setEventManager(EventManager $events)
     {
         if ($this->app instanceof PluginApplicationInterface) {
-            return $this->app->setEventManager($events);
+            $this->app->setEventManager($events);
+
+            return $this;
         }
 
         throw new InvalidArgumentException('Cannot set the event manager, the application does not support events.');

+ 2 - 3
src/Http/ServerRequest.php

@@ -56,7 +56,7 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
      * In PUT/PATCH/DELETE requests this property will contain the form-urlencoded
      * data.
      *
-     * @var array
+     * @var null|array|object
      * @deprecated 3.4.0 This public property will be removed in 4.0.0. Use getData() instead.
      */
     protected $data = [];
@@ -777,8 +777,7 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
     /**
      * Worker for the public is() function
      *
-     * @param string|array $type The type of request you want to check. If an array
-     *   this method will return true if the request matches any type.
+     * @param string $type The type of request you want to check.
      * @param array $args Array of custom detector arguments.
      * @return bool Whether or not the request is the type you are checking.
      */

+ 1 - 1
src/Log/Engine/ConsoleLog.php

@@ -91,6 +91,6 @@ class ConsoleLog extends BaseLog
         $message = $this->_format($message, $context);
         $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
 
-        return $this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
+        return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
     }
 }

+ 1 - 1
src/ORM/Association.php

@@ -502,7 +502,7 @@ abstract class Association
      * @deprecated 3.4.0 Use setConditions()/getConditions() instead.
      * @param array|null $conditions list of conditions to be used
      * @see \Cake\Database\Query::where() for examples on the format of the array
-     * @return array
+     * @return array|callable
      */
     public function conditions($conditions = null)
     {

+ 3 - 2
src/ORM/Association/Loader/SelectLoader.php

@@ -116,7 +116,7 @@ class SelectLoader
      * iterator. The options accepted by this method are the same as `Association::eagerLoader()`
      *
      * @param array $options Same options as `Association::eagerLoader()`
-     * @return callable
+     * @return \Closure
      */
     public function buildEagerLoader(array $options)
     {
@@ -294,13 +294,14 @@ class SelectLoader
         }
         $subquery->select($filter, true);
 
+        $conditions = null;
         if (is_array($key)) {
             $conditions = $this->_createTupleCondition($query, $key, $filter, '=');
         } else {
             $filter = current($filter);
         }
 
-        $conditions = isset($conditions) ? $conditions : $query->newExpr([$key => $filter]);
+        $conditions = $conditions ?: $query->newExpr([$key => $filter]);
 
         return $query->innerJoin(
             [$aliasedTable => $subquery],

+ 1 - 0
src/ORM/LazyEagerLoader.php

@@ -76,6 +76,7 @@ class LazyEagerLoader
             return $entity->{$method}($primaryKey);
         });
 
+        /** @var \Cake\ORM\Query $query */
         $query = $source
             ->find()
             ->select((array)$primaryKey)

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

@@ -236,7 +236,7 @@ class TableLocator implements LocatorInterface
      *
      * @param string $alias The alias name you want to get.
      * @param array $options Table options array.
-     * @return string
+     * @return string|false
      */
     protected function _getClassName($alias, array $options = [])
     {

+ 20 - 5
src/ORM/Table.php

@@ -749,7 +749,9 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             'Use setDisplayField()/getDisplayField() instead.'
         );
         if ($key !== null) {
-            return $this->setDisplayField($key);
+            $this->setDisplayField($key);
+
+            return $key;
         }
 
         return $this->getDisplayField();
@@ -931,6 +933,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
      */
     public function getBehavior($name)
     {
+        /** @var \Cake\ORM\Behavior $behavior */
         $behavior = $this->_behaviors->get($name);
         if ($behavior === null) {
             throw new InvalidArgumentException(sprintf(
@@ -1126,7 +1129,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        return $this->_associations->load(BelongsTo::class, $associated, $options);
+        /** @var \Cake\ORM\Association\BelongsTo $association */
+        $association = $this->_associations->load(BelongsTo::class, $associated, $options);
+
+        return $association;
     }
 
     /**
@@ -1169,7 +1175,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        return $this->_associations->load(HasOne::class, $associated, $options);
+        /** @var \Cake\ORM\Association\HasOne $association */
+        $association = $this->_associations->load(HasOne::class, $associated, $options);
+
+        return $association;
     }
 
     /**
@@ -1218,7 +1227,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        return $this->_associations->load(HasMany::class, $associated, $options);
+        /** @var \Cake\ORM\Association\HasMany $association */
+        $association = $this->_associations->load(HasMany::class, $associated, $options);
+
+        return $association;
     }
 
     /**
@@ -1269,7 +1281,10 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        return $this->_associations->load(BelongsToMany::class, $associated, $options);
+        /** @var \Cake\ORM\Association\BelongsToMany $association */
+        $association = $this->_associations->load(BelongsToMany::class, $associated, $options);
+
+        return $association;
     }
 
     /**

+ 1 - 1
src/Utility/Inflector.php

@@ -417,7 +417,7 @@ class Inflector
      * @param string $type Inflection type
      * @param string $key Original value
      * @param string|bool $value Inflected value
-     * @return string|bool Inflected value on cache hit or false on cache miss.
+     * @return string|false Inflected value on cache hit or false on cache miss.
      */
     protected static function _cache($type, $key, $value = false)
     {

+ 5 - 1
src/Validation/Validation.php

@@ -1187,7 +1187,11 @@ class Validation
             return $check['tmp_name'];
         }
 
-        return $check;
+        if (is_string($check)) {
+            return $check;
+        }
+
+        return false;
     }
 
     /**

+ 1 - 1
src/Validation/ValidationSet.php

@@ -54,7 +54,7 @@ class ValidationSet implements ArrayAccess, IteratorAggregate, Countable
      *
      * @param bool|string|callable|null $validatePresent Deprecated since 3.6.0 ValidationSet::isPresenceRequired() is deprecated as a setter
      * Use ValidationSet::requirePresence() instead.
-     * @return bool|string
+     * @return bool|string|callable
      */
     public function isPresenceRequired($validatePresent = null)
     {

+ 1 - 0
src/View/Helper.php

@@ -181,6 +181,7 @@ class Helper implements EventListenerInterface
         // We cannot change the key here in 3.x, but the behavior is inverted in this case
         $escape = isset($options['escape']) && $options['escape'] === false;
         if ($escape) {
+            /** @var string $confirm */
             $confirm = h($confirm);
         }
 

+ 1 - 1
src/View/Helper/TimeHelper.php

@@ -365,7 +365,7 @@ class TimeHelper extends Helper
      * @param string|null $format Intl compatible format string.
      * @param bool|string $invalid Default value to display on invalid dates
      * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
-     * @return string Formatted and translated date string
+     * @return string|false Formatted and translated date string or value for `$invalid` on failure.
      * @throws \Exception When the date cannot be parsed
      * @see \Cake\I18n\Time::i18nFormat()
      */

+ 6 - 1
src/View/Helper/UrlHelper.php

@@ -52,8 +52,10 @@ class UrlHelper extends Helper
         }
         $options += $defaults;
 
+        /** @var string $url */
         $url = Router::url($url, $options['fullBase']);
         if ($options['escape']) {
+            /** @var string $url */
             $url = h($url);
         }
 
@@ -209,7 +211,10 @@ class UrlHelper extends Helper
         $parts = array_map('rawurlencode', $parts);
         $encoded = implode('/', $parts);
 
-        return h(str_replace($path, $encoded, $url));
+        /** @var string $url */
+        $url = h(str_replace($path, $encoded, $url));
+
+        return $url;
     }
 
     /**

+ 6 - 6
src/View/ViewBuilder.php

@@ -48,14 +48,14 @@ class ViewBuilder implements JsonSerializable, Serializable
     /**
      * The plugin name to use.
      *
-     * @var string
+     * @var string|null|false
      */
     protected $_plugin;
 
     /**
      * The theme name to use.
      *
-     * @var string
+     * @var string|null|false
      */
     protected $_theme;
 
@@ -254,7 +254,7 @@ class ViewBuilder implements JsonSerializable, Serializable
     /**
      * Gets the plugin name to use.
      *
-     * @return string
+     * @return string|null|false
      */
     public function getPlugin()
     {
@@ -267,7 +267,7 @@ class ViewBuilder implements JsonSerializable, Serializable
      * @deprecated 3.4.0 Use setPlugin()/getPlugin() instead.
      * @param string|null|false $name Plugin name. If null returns current plugin.
      *   Use false to remove the current plugin name.
-     * @return string|$this
+     * @return string|false|null|$this
      */
     public function plugin($name = null)
     {
@@ -341,7 +341,7 @@ class ViewBuilder implements JsonSerializable, Serializable
     /**
      * Gets the view theme to use.
      *
-     * @return string
+     * @return string|null|false
      */
     public function getTheme()
     {
@@ -354,7 +354,7 @@ class ViewBuilder implements JsonSerializable, Serializable
      * @deprecated 3.4.0 Use setTheme()/getTheme() instead.
      * @param string|null|false $theme Theme name. If null returns current theme.
      *   Use false to remove the current theme.
-     * @return string|$this
+     * @return string|false|null|$this
      */
     public function theme($theme = null)
     {

+ 1 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -2520,6 +2520,7 @@ class ValidationTest extends TestCase
 
         $this->assertFalse(Validation::mimeType($image, ['image/png']));
         $this->assertFalse(Validation::mimeType(['tmp_name' => $image], ['image/png']));
+        $this->assertFalse(Validation::mimeType([], ['image/png']));
     }
 
     /**