Passed
Push — feature/initial-implementation ( 70fbec...2d1ee1 )
by Fike
02:35
created

Resolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Inheritance;
6
7
use AmaTeam\ElasticSearch\API\Entity\EntityInterface;
8
use AmaTeam\ElasticSearch\Entity\Entity;
9
use AmaTeam\ElasticSearch\Entity\Mapping\Inheritance\Combiner;
10
11
class Resolver
12
{
13
    public static function resolve(EntityInterface ...$hierarchy): EntityInterface
14
    {
15
        $cursor = new Entity('-');
16
        foreach ($hierarchy as $entry) {
17
            $cursor = static::combine($cursor, $entry);
18
        }
19
        return $cursor;
20
    }
21
22
    public static function combine(EntityInterface $parent, EntityInterface $child): EntityInterface
23
    {
24
        $result = (new Entity($child->getName()))
25
            ->setRootLevelDocument($child->isRootLevelDocument())
26
            // TODO: proper indexing merging scheme
27
            ->setIndexing($child->getIndexing())
28
            ->setMapping(Combiner::combine($parent->getMapping(), $child->getMapping()));
29
        if ($child->getParentName()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $child->getParentName() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
            $result->setParentName($child->getParentName());
31
        }
32
        return $result;
33
    }
34
}
35