Browse Source

Clarify assoc behavior and merging.

mscherer 4 years ago
parent
commit
541e57cbb6

+ 2 - 2
src/Database/Schema/TableSchema.php

@@ -290,7 +290,7 @@ class TableSchema implements TableSchemaInterface, SqlGeneratorInterface
      * Constructor.
      *
      * @param string $table The table name.
-     * @param array $columns The list of columns for the schema.
+     * @param array<string, array> $columns The list of columns for the schema.
      */
     public function __construct(string $table, array $columns = [])
     {
@@ -674,7 +674,7 @@ class TableSchema implements TableSchemaInterface, SqlGeneratorInterface
      */
     public function setOptions(array $options)
     {
-        $this->_options = array_merge($this->_options, $options);
+        $this->_options = $options + $this->_options;
 
         return $this;
     }

+ 5 - 5
src/Datasource/SchemaInterface.php

@@ -53,7 +53,7 @@ interface SchemaInterface
      * - `comment` The comment for the column.
      *
      * @param string $name The name of the column
-     * @param array|string $attrs The attributes for the column or the type name.
+     * @param array<string, mixed>|string $attrs The attributes for the column or the type name.
      * @return $this
      */
     public function addColumn(string $name, array|string $attrs);
@@ -100,7 +100,7 @@ interface SchemaInterface
     public function getColumnType(string $name): ?string;
 
     /**
-     * Sets the type of a column.
+     * Sets the type of column.
      *
      * @param string $name The column to set the type of.
      * @param string $type The type to set the column to.
@@ -132,14 +132,14 @@ interface SchemaInterface
      * Returns an array where the keys are the column names in the schema
      * and the values the database type they have.
      *
-     * @return array
+     * @return array<string, string>
      */
     public function typeMap(): array;
 
     /**
      * Get a hash of columns and their default values.
      *
-     * @return array
+     * @return array<string, mixed>
      */
     public function defaultValues(): array;
 
@@ -160,7 +160,7 @@ interface SchemaInterface
      * Table options allow you to set platform specific table level options.
      * For example the engine type in MySQL.
      *
-     * @return array An array of options.
+     * @return array<string, mixed> An array of options.
      */
     public function getOptions(): array;
 }

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

@@ -122,7 +122,7 @@ class Response extends Message implements ResponseInterface
     /**
      * Constructor
      *
-     * @param array $headers Unparsed headers.
+     * @param array<string> $headers Unparsed headers.
      * @param string $body The response body.
      */
     public function __construct(array $headers = [], string $body = '')
@@ -171,7 +171,7 @@ class Response extends Message implements ResponseInterface
      * - Decodes the status code and reasonphrase.
      * - Parses and normalizes header names + values.
      *
-     * @param array $headers Headers to parse.
+     * @param array<string> $headers Headers to parse.
      * @return void
      */
     protected function _parseHeaders(array $headers): void

+ 4 - 4
src/View/ViewBuilder.php

@@ -107,7 +107,7 @@ class ViewBuilder implements JsonSerializable
      * This options array lets you provide custom constructor
      * arguments to application/plugin view classes.
      *
-     * @var array
+     * @var array<string, mixed>
      */
     protected array $_options = [];
 
@@ -423,7 +423,7 @@ class ViewBuilder implements JsonSerializable
     }
 
     /**
-     * Gets the name of the layout file to render the view inside of.
+     * Gets the name of the layout file to render the view inside.
      *
      * @return string|null
      */
@@ -469,7 +469,7 @@ class ViewBuilder implements JsonSerializable
     public function setOptions(array $options, bool $merge = true)
     {
         if ($merge) {
-            $options = array_merge($this->_options, $options);
+            $options += $this->_options;
         }
         $this->_options = $options;
 
@@ -479,7 +479,7 @@ class ViewBuilder implements JsonSerializable
     /**
      * Gets additional options for the view.
      *
-     * @return array
+     * @return array<string, mixed>
      */
     public function getOptions(): array
     {

+ 6 - 7
tests/TestCase/View/ViewBuilderTest.php

@@ -180,8 +180,8 @@ class ViewBuilderTest extends TestCase
             $builder = new ViewBuilder();
             $builder->{$set}($value);
 
-            $builder->{$set}(['Merged'], true);
-            $this->assertSame(array_merge($value, ['Merged']), $builder->{$get}(), 'Should merge');
+            $builder->{$set}(['merged' => 'Merged'], true);
+            $this->assertSame(['merged' => 'Merged'] + $value, $builder->{$get}(), 'Should merge');
 
             $builder->{$set}($value, false);
             $this->assertSame($value, $builder->{$get}(), 'Should replace');
@@ -346,19 +346,18 @@ class ViewBuilderTest extends TestCase
     }
 
     /**
-     * test setOptions() with 2 strings in array, merge true.
+     * test setOptions() with 2 assoc strings in array, merge true.
      */
     public function testSetOptionsMultiple(): void
     {
         $builder = new ViewBuilder();
-        $builder->setOptions(['oldOption'], false);
+        $builder->setOptions(['key' => 'oldOption'], false);
 
-        $option = ['newOption', 'anotherOption'];
+        $option = ['anotherKey' => 'anotherOption', 'key' => 'newOption'];
         $builder->setOptions($option);
-        $expects = ['oldOption', 'newOption', 'anotherOption'];
+        $expects = ['key' => 'newOption', 'anotherKey' => 'anotherOption'];
 
         $result = $builder->getOptions();
-        $this->assertContainsOnly('string', $result);
         $this->assertEquals($expects, $result);
     }