Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

MappingOperations::merge()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 5
Ratio 31.25 %

Importance

Changes 0
Metric Value
dl 5
loc 16
c 0
b 0
f 0
rs 9.2
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping\Property;
4
5
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\Mapping;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\MappingInterface;
7
use ReflectionProperty;
8
9
class MappingOperations
10
{
11
    public static function merge(MappingInterface ...$mappings): Mapping
12
    {
13
        $target = new Mapping();
14
        foreach ($mappings as $source) {
15
            $target
16
                ->setName($source->getName())
17
                ->setOriginalName($source->getOriginalName());
18
            $defaultView = ViewOperations::merge($target->getDefaultView(), $source->getDefaultView());
19
            $target->setDefaultView($defaultView);
20 View Code Duplication
            foreach ($source->getViews() as $name => $view) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
                $current = $target->getView($name);
22
                $pool = $current ? [$current, $view] : [$view];
23
                $target->setView($name, ViewOperations::merge(...$pool));
24
            }
25
        }
26
        return $target;
27
    }
28
29
    public static function from(MappingInterface $mapping): Mapping
30
    {
31
        return static::merge($mapping);
32
    }
33
34
    public static function toMutable(MappingInterface $mapping): Mapping
35
    {
36
        return $mapping instanceof Mapping ? $mapping : static::from($mapping);
37
    }
38
39
    public static function fromReflection(ReflectionProperty $property)
40
    {
41
        return (new Mapping())
42
            ->setName($property->getName())
43
            ->setOriginalName($property->getName());
44
    }
45
}
46