Passed
Pull Request — v3 (#729)
by
unknown
30:59
created

Recursively::handleArrayIteratorCopyOrReference()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 31
rs 9.6111
cc 5
nc 6
nop 4
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Helpers\Arr;
20
21
use ArrayIterator;
22
use Closure;
23
use RecursiveArrayIterator;
24
use RecursiveIteratorIterator;
25
26
/**
27
 * This helper contains functions to work with arrays.
28
 *
29
 * @package GameQ\Helpers
30
 */
31
trait Recursively
32
{
33
    /**
34
     * This function is responsible for handling behaivour specific to PHP versions before 8.1.
35
     *
36
     * @param array &$data
37
     * @param RecursiveIteratorIterator $recursiveIterator
38
     * @param RecursiveArrayIterator $iterator
39
     * @param Closure $callback
40
     * @return void
41
     */
42
    protected static function handleArrayIteratorCopyOrReference(
43
        array &$data,
44
        RecursiveIteratorIterator $recursiveIterator,
45
        RecursiveArrayIterator $iterator,
46
        Closure $callback
47
    ) {
48
        /* ArrayIterator before PHP 8.1 does use a copy instead of reference */
49
        if (PHP_VERSION_ID < 80100) {
50
            /* Hash the current state of the iterator */
51
            $hashes = static::hashes((array) $iterator);
52
53
            /* Continue with the provided callback */
54
            $callback();
55
56
            /* Determine if the current iterator has been modified */
57
            if (! empty($diff = array_diff_assoc(static::hashes((array) $iterator), $hashes))) {
58
                /* Determine path to the current iterator */
59
                $path = [];
60
                for ($depth = 0; $depth < $recursiveIterator->getDepth(); $depth++) {
61
                    $path[] = $recursiveIterator->getSubIterator($depth)->key();
62
                }
63
64
                /* Process all modified values */
65
                foreach (array_keys($diff) as $modified) {
66
                    /* Write the modified value to the original array */
67
                    static::set($data, array_merge($path, [$modified]), $iterator->offsetGet($modified));
68
                }
69
            }
70
        } else {
71
            /* There is no need to write back any changes when ArrayIterator does use a reference */
72
            $callback();
73
        }
74
    }
75
76
    protected static function getArrayIteratorCopyOrReference(array &$data, ArrayIterator $arrayIterator)
77
    {
78
        if (PHP_VERSION_ID < 80100) {
79
            /* Return the actual array reference */
80
            return $data;
81
        } else {
82
            /* Return the ArrayIterator's internal reference */
83
            return $arrayIterator->getArrayCopy();
84
        }
85
    }
86
}
87