Passed
Pull Request — v3 (#729)
by
unknown
38:39 queued 03:37
created

WithArrayHelpers   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 10
dl 0
loc 35
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyRecursively() 0 26 2
1
<?php
2
3
namespace GameQ\Concerns;
4
5
use Closure;
6
use RecursiveArrayIterator;
7
use RecursiveIteratorIterator;
8
9
/**
10
 * This Trait provides simple helpers concerned with handling arrays.
11
 */
12
trait WithArrayHelpers
13
{
14
    /**
15
     * This helper does process each element of the provided array recursively.
16
     * It does so allowing for modifications to the provided array and without
17
     * using actual recursive calls.
18
     *
19
     * @return array
20
     */
21
    protected static function applyRecursively(array $data, Closure $callback)
22
    {
23
        /* Initialize the RecursiveArrayIterator for the provided data */
24
        $arrayIterator = new RecursiveArrayIterator($data);
25
26
        /* Configure the Iterator for the RecursiveIterator */
27
        $recursiveIterator = new RecursiveIteratorIterator(
28
            $arrayIterator,
29
            RecursiveIteratorIterator::SELF_FIRST
30
        );
31
32
        /* Traverse the provided data */
33
        foreach ($recursiveIterator as $key => $value) {
34
            /* Get the current sub iterator with Type hinting */
35
            /** @var RecursiveArrayIterator */
36
            $subIterator = $recursiveIterator->getSubIterator();
37
38
            /* Process the element with the provided closure */
39
            $callback($value, $key, $subIterator);
40
41
            /* Update (possibly) changed value */
42
            $subIterator->offsetSet($key, $value);
0 ignored issues
show
Bug introduced by
The method offsetSet() does not exist on RecursiveIterator. It seems like you code against a sub-type of RecursiveIterator such as Phar or RecursiveCachingIterator or SimpleXMLElement or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            $subIterator->/** @scrutinizer ignore-call */ 
43
                          offsetSet($key, $value);
Loading history...
43
        }
44
45
        /* Return the processed data */
46
        return $arrayIterator->getArrayCopy();
47
    }
48
}
49