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

MappingOperations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 14.29 %

Importance

Changes 0
Metric Value
wmc 8
dl 5
loc 35
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 5 16 4
A toMutable() 0 3 2
A fromReflection() 0 5 1
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\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