Browse Source

Merge pull request #12065 from odan/fetch-array-return-type-11795

Fixed return type (array) #11795
Mark Story 7 years ago
parent
commit
a8412d6fc1

+ 7 - 5
src/Controller/Controller.php

@@ -125,18 +125,20 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
      * This object contains all the information about a request and several methods for reading
      * additional information about the request.
      *
-     * @var \Cake\Http\ServerRequest|null
+     * Deprecated 3.6.0: The property will become protected in 4.0.0. Use getRequest()/setRequest instead.
+     *
+     * @var \Cake\Http\ServerRequest
      * @link https://book.cakephp.org/3.0/en/controllers/request-response.html#request
-     * @deprecated 3.6.0 The property will become protected in 4.0.0. Use getRequest()/setRequest instead.
      */
     public $request;
 
     /**
      * An instance of a Response object that contains information about the impending response
      *
-     * @var \Cake\Http\Response|null
+     * Deprecated 3.6.0: The property will become protected in 4.0.0. Use getResponse()/setResponse instead.
+
+     * @var \Cake\Http\Response
      * @link https://book.cakephp.org/3.0/en/controllers/request-response.html#response
-     * @deprecated 3.6.0 The property will become protected in 4.0.0. Use getResponse()/setResponse instead.
      */
     public $response;
 
@@ -577,7 +579,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
     public function invokeAction()
     {
         $request = $this->request;
-        if (!isset($request)) {
+        if (!$request) {
             throw new LogicException('No Request object configured. Cannot invoke action');
         }
         if (!$this->isAction($request->getParam('action'))) {

+ 3 - 1
src/Database/Statement/BufferedStatement.php

@@ -111,7 +111,9 @@ class BufferedStatement extends StatementDecorator
      */
     public function fetchAssoc()
     {
-        return $this->fetch(static::FETCH_TYPE_ASSOC);
+        $result = $this->fetch(static::FETCH_TYPE_ASSOC);
+
+        return $result ?: [];
     }
 
     /**

+ 4 - 2
src/Database/Statement/StatementDecorator.php

@@ -221,11 +221,13 @@ class StatementDecorator implements StatementInterface, Countable, IteratorAggre
      * Returns the next row in a result set as an associative array. Calling this function is the same as calling
      * $statement->fetch(StatementDecorator::FETCH_TYPE_ASSOC). If no results are found false is returned.
      *
-     * @return array|false Result array containing columns and values an an associative array or false if no results
+     * @return array Result array containing columns and values an an associative array or an empty array if no results
      */
     public function fetchAssoc()
     {
-        return $this->fetch(static::FETCH_TYPE_ASSOC);
+        $result = $this->fetch(static::FETCH_TYPE_ASSOC);
+
+        return $result ?: [];
     }
 
     /**

+ 19 - 0
tests/TestCase/Database/QueryTest.php

@@ -4780,6 +4780,25 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test that calling fetchAssoc return an empty associated array.
+     * @return void
+     * @throws \Exception
+     */
+    public function testFetchAssocWithEmptyResult()
+    {
+        $this->loadFixtures('Profiles');
+        $query = new Query($this->connection);
+
+        $results = $query
+            ->select(['id'])
+            ->from('profiles')
+            ->where(['id' => -1])
+            ->execute()
+            ->fetchAssoc();
+        $this->assertSame([], $results);
+    }
+
+    /**
      * Test that calling fetch with with FETCH_TYPE_OBJ return stdClass object.
      * @return void
      * @throws \Exception