Passed
Push — 2.x ( 0b5227...cb81b7 )
by butschster
16:17
created

AbstractMergeNode::mergeInheritanceNodes()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 7
nop 1
dl 0
loc 21
ccs 12
cts 13
cp 0.9231
crap 5.0113
rs 9.5222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Parser;
6
7
use Cycle\ORM\Select\LoaderInterface;
8
9
/**
10
 * @internal
11
 */
12
abstract class AbstractMergeNode extends AbstractNode
13
{
14
    protected const OVERWRITE_DATA = false;
15
16
    protected array $results = [];
17
18
    /**
19
     * @param string[] $columns
20
     * @param string[] $primaryKeys
21
     * @param string[] $innerKeys Inner relation keys (for example user_id)
22
     * @param string[]|null $outerKeys Outer (parent) relation keys (for example id = parent.id)
23
     */
24 880
    public function __construct(
25
        private string $discriminatorValue,
26
        array $columns,
27
        array $primaryKeys,
28
        protected array $innerKeys,
29
        ?array $outerKeys
30
    ) {
31 880
        parent::__construct($columns, $outerKeys);
32 880
        $this->setDuplicateCriteria($primaryKeys);
33
    }
34
35 880
    protected function push(array &$data): void
36
    {
37 880
        $this->results[] = &$data;
38
    }
39
40 880
    public function mergeInheritanceNodes(bool $includeRole = false): void
41
    {
42 880
        if ($this->parent === null) {
43
            return;
44
        }
45
46 880
        parent::mergeInheritanceNodes($includeRole);
47
48 880
        $roleField = $includeRole ? [LoaderInterface::ROLE_KEY => $this->discriminatorValue] : [];
49 880
        foreach ($this->results as $item) {
50 880
            if ($this->isEmptyKeys($this->innerKeys, $item)) {
51 184
                continue;
52
            }
53 880
            $this->parent->mergeData(
54 880
                $this->indexName,
0 ignored issues
show
Bug introduced by
It seems like $this->indexName can also be of type null; however, parameter $index of Cycle\ORM\Parser\AbstractNode::mergeData() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
                /** @scrutinizer ignore-type */ $this->indexName,
Loading history...
55 880
                $this->intersectData($this->innerKeys, $item),
56 880
                $item + $roleField,
57
                static::OVERWRITE_DATA
58
            );
59
        }
60 880
        $this->results = [];
61
    }
62
63
    /**
64
     * @psalm-pure
65
     */
66 880
    private function isEmptyKeys(array $keys, array $data): bool
67
    {
68 880
        foreach ($keys as $key) {
69 880
            if (isset($data[$key])) {
70 880
                return false;
71
            }
72
        }
73 184
        return true;
74
    }
75
}
76