Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

MappingOperations   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 10.87 %

Importance

Changes 0
Metric Value
wmc 12
dl 5
loc 46
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extend() 0 8 2
B merge() 5 21 7
A toMutable() 0 6 2
A from() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping\Structure;
4
5
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\Mapping;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\MappingInterface;
7
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations as PropertyMappingOperations;
8
9
class MappingOperations
10
{
11
    public static function merge(MappingInterface... $mappings): Mapping
12
    {
13
        $target = new Mapping();
14
        foreach ($mappings as $source) {
15
            if ($source->getIgnoredParentProperties() !== null) {
16
                $target->setIgnoredParentProperties($source->getIgnoredParentProperties());
17
            }
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
            foreach ($source->getProperties() as $name => $property) {
26
                $current = $target->getProperty($name);
27
                $pool = $current ? [$current, $property] : [$property];
28
                $target->setProperty($name, PropertyMappingOperations::merge(...$pool));
29
            }
30
        }
31
        return $target;
32
    }
33
34
    public static function from(MappingInterface $mapping): Mapping
35
    {
36
        return static::merge($mapping);
37
    }
38
39
    public static function toMutable(MappingInterface $mapping): Mapping
40
    {
41
        if ($mapping instanceof Mapping) {
42
            return $mapping;
43
        }
44
        return static::from($mapping);
45
    }
46
47
    public static function extend(MappingInterface $parent, MappingInterface $child): Mapping
48
    {
49
        $target = static::from($parent);
50
        $ignoredProperties = $child->getIgnoredParentProperties() ?? [];
51
        foreach ($ignoredProperties as $property) {
52
            $target->removeProperty($property);
53
        }
54
        return static::merge($target, $child);
55
    }
56
}
57