Skip to content

Invalid logic of Phalcon\Config::merge() #13768

Closed
@zatrepalek

Description

Hi, I found this bug. Probably related to #13201 (stale - closed).

Expected and Actual Behavior

If an array is passed to merge() method string (number-like) keys are not merged correctly

Correct behaviour (for string-like keys):

$params1 = ['a' => 'aaa', 'b' => ['bar' => 'rrr', 'baz' => 'zzz']];
$config = new \Phalcon\Config($params1);
$config->toArray()
// output
array (
  'a' => 'aaa',
  'b' => 
  array (
    'bar' => 'rrr',
    'baz' => 'zzz',
  ),
)

$params2 = ['c' => 'ccc', 'b' => ['baz' => 'xxx']];
$config->merge(new \Phalcon\Config($params2));
$config->toArray()
// correct merge
array (
  'a' => 'aaa',
  'b' => 
  array (
    'bar' => 'rrr',
    'baz' => 'xxx',
  ),
  'c' => 'ccc',
)

Wrong behaviour (for integer-like keys):

$params1 = ['1' => 'aaa', '2' => ['11' => 'rrr', '12' => 'zzz']];
$config = new \Phalcon\Config($params1);
$config->toArray()
// output (string keys casted to integers)
array (
  1 => 'aaa',
  2 => 
  array (
    11 => 'rrr',
    12 => 'zzz',
  ),
)

$params2 = ['3' => 'ccc', '2' => ['12' => 'xxx']];
$config->merge(new \Phalcon\Config($params2));
$config->toArray()
// wrong merge (casted to integers and invalid merge logic for sub array)
array (
  1 => 'aaa',
  2 => 
  array (
    11 => 'rrr',
    12 => 'zzz',
    2 => 'xxx',
  ),
  3 => 'ccc',
)

Details

  • Phalcon version: 3.4.2
  • PHP Version: 7.1.25
  • Operating System: macOS 10.13.6 (host), Debian 9 (docker)
  • Installation type: Compiling from source (build/php7/64bits)
  • Zephir version: (Version 0.10.14-975ad02db4)
  • Server: Nginx

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions