Skip to content

Commit

Permalink
ArrayHelper::mergeRecursive() accepts no parameters
Browse files Browse the repository at this point in the history
`ArrayHelper::traverse*()` have typed `iterable` first parameter
  • Loading branch information
ElGigi committed May 3, 2021
1 parent f040d97 commit 0f7b33d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.1.6] - 2021-05-03
## [1.2.0] - 2021-05-03

### Changed

- `ArrayHelper::mergeRecursive()` accepts no parameters
- `ArrayHelper::traverse*()` have typed `iterable` first parameter

### Fixed

Expand Down
30 changes: 10 additions & 20 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,18 @@ public static function toXml($array, ?SimpleXMLElement $root = null, ?string $ro
* b_array_merge_recursive() do not merge strings values
* into an array.
*
* @param array $arraySrc Array source
* @param array[] $arrays Arrays to merge
*
* @return array
*/
public static function mergeRecursive(array $arraySrc, array ...$arrays): array
public static function mergeRecursive(array ...$arrays): array
{
$arraySrc = array_shift($arrays);

if (null === $arraySrc) {
return [];
}

foreach ($arrays as $array) {
if (empty($array)) {
continue;
Expand Down Expand Up @@ -140,14 +145,9 @@ public static function mergeRecursive(array $arraySrc, array ...$arrays): array
* @param string $path Path
*
* @return bool
* @throws InvalidArgumentException if first argument is not a traversable data
*/
public static function traverseExists(&$mixed, string $path): bool
public static function traverseExists(iterable &$mixed, string $path): bool
{
if (!is_iterable($mixed)) {
throw new InvalidArgumentException('First argument must be a traversable mixed data');
}

$path = explode('.', $path);

$temp = &$mixed;
Expand Down Expand Up @@ -180,14 +180,9 @@ public static function traverseExists(&$mixed, string $path): bool
* @param mixed $default Default value
*
* @return mixed|null
* @throws InvalidArgumentException if first argument is not a traversable data
*/
public static function traverseGet(&$mixed, string $path, $default = null)
public static function traverseGet(iterable &$mixed, string $path, $default = null)
{
if (!is_iterable($mixed)) {
throw new InvalidArgumentException('First argument must be a traversable mixed data');
}

$path = explode('.', $path);

$temp = &$mixed;
Expand Down Expand Up @@ -220,14 +215,9 @@ public static function traverseGet(&$mixed, string $path, $default = null)
* @param mixed $value Value
*
* @return bool
* @throws InvalidArgumentException if first argument is not a traversable data
*/
public static function traverseSet(&$mixed, string $path, $value): bool
public static function traverseSet(iterable &$mixed, string $path, $value): bool
{
if (!is_iterable($mixed)) {
throw new InvalidArgumentException('First argument must be a traversable mixed data');
}

$path = explode('.', $path);

$temp = &$mixed;
Expand Down
5 changes: 2 additions & 3 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ function b_array_to_xml(array $array, ?SimpleXMLElement $root = null, ?string $r
* b_array_merge_recursive() do not merge strings values
* into an array.
*
* @param array $arraySrc Array source
* @param array[] $arrays Arrays to merge
*
* @return array
*/
function b_array_merge_recursive(array $arraySrc, array ...$arrays): array
function b_array_merge_recursive(array ...$arrays): array
{
return ArrayHelper::mergeRecursive($arraySrc, ...$arrays);
return ArrayHelper::mergeRecursive(...$arrays);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public function testMergeRecursive()
['321' => '321 value'],
)
);

$this->assertEquals([], ArrayHelper::mergeRecursive());
}

public function testTraverseExists()
Expand Down

0 comments on commit 0f7b33d

Please sign in to comment.