Browse Source

Merge pull request #16394 from cakephp/5.x-cleanup

Code cleanup.
Mark Story 4 years ago
parent
commit
4b8e5ee41b

+ 1 - 1
src/Controller/Controller.php

@@ -435,7 +435,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
     public function setRequest(ServerRequest $request)
     {
         $this->request = $request;
-        $this->plugin = $request->getParam('plugin') ?: null;
+        $this->plugin = $request->getParam('plugin');
 
         return $this;
     }

+ 2 - 1
src/Database/Expression/CaseExpressionTrait.php

@@ -23,6 +23,7 @@ use Cake\Database\Query;
 use Cake\Database\TypedResultInterface;
 use Cake\Database\ValueBinder;
 use DateTimeInterface;
+use Stringable;
 
 /**
  * Trait that holds shared functionality for case related expressions.
@@ -60,7 +61,7 @@ trait CaseExpressionTrait
             $type = 'datetime';
         } elseif (
             is_object($value) &&
-            method_exists($value, '__toString')
+            $value instanceof Stringable
         ) {
             $type = 'string';
         } elseif (

+ 2 - 1
src/Error/ErrorLogger.php

@@ -19,6 +19,7 @@ namespace Cake\Error;
 use Cake\Core\Configure;
 use Cake\Core\Exception\CakeException;
 use Cake\Core\InstanceConfigTrait;
+use Cake\Http\ServerRequest;
 use Cake\Log\Log;
 use Psr\Http\Message\ServerRequestInterface;
 use Throwable;
@@ -158,7 +159,7 @@ class ErrorLogger implements ErrorLoggerInterface
             $message .= "\nReferer URL: " . $referer;
         }
 
-        if (method_exists($request, 'clientIp')) {
+        if ($request instanceof ServerRequest) {
             $clientIp = $request->clientIp();
             if ($clientIp && $clientIp !== '::1') {
                 $message .= "\nClient IP: " . $clientIp;

+ 4 - 6
src/Error/ExceptionTrap.php

@@ -103,17 +103,15 @@ class ExceptionTrap
 
         if (is_string($class)) {
             /** @var class-string $class */
-            if (!(method_exists($class, 'render') && method_exists($class, 'write'))) {
+            if (!is_subclass_of($class, ExceptionRendererInterface::class)) {
                 throw new InvalidArgumentException(
                     "Cannot use {$class} as an `exceptionRenderer`. " .
-                    'It must implement render() and write() methods.'
+                    'It must be an instance of Cake\Error\ExceptionRendererInterface.'
                 );
             }
 
-            /** @var \Cake\Error\ExceptionRendererInterface $instance */
-            $instance = new $class($exception, $request, $this->_config);
-
-            return $instance;
+            /** @var \Cake\Error\ExceptionRendererInterface */
+            return new $class($exception, $request, $this->_config);
         }
 
         return $class($exception, $request);

+ 2 - 0
src/Http/BaseApplication.php

@@ -29,6 +29,7 @@ use Cake\Core\Plugin;
 use Cake\Core\PluginApplicationInterface;
 use Cake\Core\PluginCollection;
 use Cake\Core\PluginInterface;
+use Cake\Event\EventDispatcherInterface;
 use Cake\Event\EventDispatcherTrait;
 use Cake\Event\EventManager;
 use Cake\Event\EventManagerInterface;
@@ -53,6 +54,7 @@ use Psr\Http\Message\ServerRequestInterface;
 abstract class BaseApplication implements
     ConsoleApplicationInterface,
     ContainerApplicationInterface,
+    EventDispatcherInterface,
     HttpApplicationInterface,
     PluginApplicationInterface,
     RoutingApplicationInterface

+ 4 - 6
src/Http/Cookie/Cookie.php

@@ -301,12 +301,10 @@ class Cookie implements CookieInterface
             unset($data['max-age']);
         }
 
-        if (isset($data['samesite'])) {
-            // Ignore invalid value when parsing headers
-            // https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-4.1
-            if (!in_array($data['samesite'], CookieInterface::SAMESITE_VALUES, true)) {
-                unset($data['samesite']);
-            }
+        // Ignore invalid value when parsing headers
+        // https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-4.1
+        if (isset($data['samesite']) && !in_array($data['samesite'], CookieInterface::SAMESITE_VALUES, true)) {
+            unset($data['samesite']);
         }
 
         $name = (string)$data['name'];

+ 1 - 3
src/Http/Middleware/CsrfProtectionMiddleware.php

@@ -393,7 +393,7 @@ class CsrfProtectionMiddleware implements MiddlewareInterface
      */
     protected function _createCookie(string $value, ServerRequestInterface $request): CookieInterface
     {
-        $cookie = Cookie::create(
+        return Cookie::create(
             $this->_config['cookieName'],
             $value,
             [
@@ -404,7 +404,5 @@ class CsrfProtectionMiddleware implements MiddlewareInterface
                 'samesite' => $this->_config['samesite'],
             ]
         );
-
-        return $cookie;
     }
 }

+ 1 - 1
src/Http/ResponseEmitter.php

@@ -177,7 +177,7 @@ class ResponseEmitter
     protected function emitHeaders(ResponseInterface $response): void
     {
         $cookies = [];
-        if (method_exists($response, 'getCookieCollection')) {
+        if ($response instanceof Response) {
             $cookies = iterator_to_array($response->getCookieCollection());
         }
 

+ 1 - 6
src/Http/ServerRequest.php

@@ -340,12 +340,7 @@ class ServerRequest implements ServerRequestInterface
      */
     public function contentType(): ?string
     {
-        $type = $this->getEnv('CONTENT_TYPE');
-        if ($type) {
-            return $type;
-        }
-
-        return $this->getEnv('HTTP_CONTENT_TYPE');
+        return $this->getEnv('CONTENT_TYPE') ?: $this->getEnv('HTTP_CONTENT_TYPE');
     }
 
     /**

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

@@ -178,7 +178,7 @@ abstract class BaseLog extends AbstractLogger
                     continue;
                 }
 
-                if (method_exists($value, '__serialize')) {
+                if ($value instanceof Serializable) {
                     $replacements['{' . $key . '}'] = serialize($value);
                     continue;
                 }

+ 12 - 27
src/ORM/Table.php

@@ -566,10 +566,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             throw new RuntimeException("Unable to check max alias lengths for  `{$this->getAlias()}` without schema.");
         }
 
-        $maxLength = null;
-        if (method_exists($this->getConnection()->getDriver(), 'getMaxAliasLength')) {
-            $maxLength = $this->getConnection()->getDriver()->getMaxAliasLength();
-        }
+        $maxLength = $this->getConnection()->getDriver()->getMaxAliasLength();
         if ($maxLength === null) {
             return;
         }
@@ -858,10 +855,8 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             ));
         }
 
-        /** @var \Cake\ORM\Behavior $behavior */
-        $behavior = $this->_behaviors->get($name);
-
-        return $behavior;
+        /** @var \Cake\ORM\Behavior */
+        return $this->_behaviors->get($name);
     }
 
     /**
@@ -1042,10 +1037,8 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        /** @var \Cake\ORM\Association\BelongsTo $association */
-        $association = $this->_associations->load(BelongsTo::class, $associated, $options);
-
-        return $association;
+        /** @var \Cake\ORM\Association\BelongsTo */
+        return $this->_associations->load(BelongsTo::class, $associated, $options);
     }
 
     /**
@@ -1088,10 +1081,8 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        /** @var \Cake\ORM\Association\HasOne $association */
-        $association = $this->_associations->load(HasOne::class, $associated, $options);
-
-        return $association;
+        /** @var \Cake\ORM\Association\HasOne */
+        return $this->_associations->load(HasOne::class, $associated, $options);
     }
 
     /**
@@ -1140,10 +1131,8 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        /** @var \Cake\ORM\Association\HasMany $association */
-        $association = $this->_associations->load(HasMany::class, $associated, $options);
-
-        return $association;
+        /** @var \Cake\ORM\Association\HasMany */
+        return $this->_associations->load(HasMany::class, $associated, $options);
     }
 
     /**
@@ -1194,10 +1183,8 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $options += ['sourceTable' => $this];
 
-        /** @var \Cake\ORM\Association\BelongsToMany $association */
-        $association = $this->_associations->load(BelongsToMany::class, $associated, $options);
-
-        return $association;
+        /** @var \Cake\ORM\Association\BelongsToMany */
+        return $this->_associations->load(BelongsToMany::class, $associated, $options);
     }
 
     /**
@@ -2169,9 +2156,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             ->where($primaryKey)
             ->execute();
 
-        $success = $statement->errorCode() === '00000' ? $entity : false;
-
-        return $success;
+        return $statement->errorCode() === '00000' ? $entity : false;
     }
 
     /**

+ 1 - 6
src/TestSuite/ConnectionHelper.php

@@ -91,12 +91,7 @@ class ConnectionHelper
         /** @var \Cake\Database\Connection $connection */
         $connection = ConnectionManager::get($connectionName);
         $collection = $connection->getSchemaCollection();
-
-        if (method_exists($collection, 'listTablesWithoutViews')) {
-            $allTables = $collection->listTablesWithoutViews();
-        } else {
-            $allTables = $collection->listTables();
-        }
+        $allTables = $collection->listTablesWithoutViews();
 
         $tables = $tables !== null ? array_intersect($tables, $allTables) : $allTables;
         $schemas = array_map(fn($table) => $collection->describe($table), $tables);

+ 2 - 1
src/TestSuite/ContainerStubTrait.php

@@ -19,6 +19,7 @@ use Cake\Core\Configure;
 use Cake\Core\ConsoleApplicationInterface;
 use Cake\Core\ContainerInterface;
 use Cake\Core\HttpApplicationInterface;
+use Cake\Event\EventDispatcherInterface;
 use Cake\Event\EventInterface;
 use Closure;
 use LogicException;
@@ -90,7 +91,7 @@ trait ContainerStubTrait
         $appArgs = $this->_appArgs ?: [CONFIG];
 
         $app = new $appClass(...$appArgs);
-        if (!empty($this->containerServices) && method_exists($app, 'getEventManager')) {
+        if (!empty($this->containerServices) && $app instanceof EventDispatcherInterface) {
             $app->getEventManager()->on('Application.buildContainer', [$this, 'modifyContainer']);
         }
 

+ 1 - 1
src/View/Form/EntityContext.php

@@ -583,7 +583,7 @@ class EntityContext implements ContextInterface
             return !is_numeric($part);
         });
         $key = implode('.', $keyParts);
-        $entity = $this->entity($parts) ?: null;
+        $entity = $this->entity($parts);
 
         if (isset($this->_validator[$key])) {
             if (is_object($entity)) {