Passed
Pull Request — master (#62)
by Sergei
05:53
created

SaveOrder::applyOrder()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 14
nc 11
nop 2
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Collection\Modifier;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Arrays\Collection\Modifier\ModifierInterface\AfterMergeModifierInterface;
9
use Yiisoft\Arrays\Collection\Modifier\ModifierInterface\BeforeMergeModifierInterface;
10
11
/**
12
 * Remembers the order of elements in the collection it is applied to
13
 * and tried to keep the order while merging.
14
 */
15
final class SaveOrder implements BeforeMergeModifierInterface, AfterMergeModifierInterface
16
{
17
    private array $array = [];
18
19
    private bool $nested = false;
20
21 2
    public function nested(): self
22
    {
23 2
        $new = clone $this;
24 2
        $new->nested = true;
25 2
        return $new;
26
    }
27
28 1
    public function notNested(): self
29
    {
30 1
        $new = clone $this;
31 1
        $new->nested = false;
32 1
        return $new;
33
    }
34
35 5
    public function beforeMerge(array $arrays, int $index): array
36
    {
37 5
        $this->array = $arrays[$index];
38 5
        return $this->array;
39
    }
40
41 5
    public function afterMerge(array $data): array
42
    {
43 5
        return $this->applyOrder($data, $this->array);
44
    }
45
46 5
    private function applyOrder(array $data, array $array): array
47
    {
48 5
        $result = [];
49
50 5
        foreach ($array as $key => $value) {
51 5
            if (is_string($key)) {
52 4
                if (array_key_exists($key, $data)) {
53 4
                    $result[$key] = ArrayHelper::remove($data, $key);
54
                }
55
            } else {
56 1
                foreach ($data as $dataKey => $dataValue) {
57 1
                    if (is_int($dataKey) && $dataValue === $value) {
58 1
                        $result[] = $dataValue;
59 1
                        unset($data[$dataKey]);
60 1
                        break;
61
                    }
62
                }
63
            }
64
65 5
            if ($this->nested && is_array($value) && is_array($result[$key])) {
66 2
                $result[$key] = $this->applyOrder($result[$key], $value);
67
            }
68
        }
69
70 5
        return array_merge($result, $data);
71
    }
72
}
73