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

TargetEntityAnnotationListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 25 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B accept() 10 23 4

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
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Annotation\Listener\Mapping\Property;
6
7
use AmaTeam\ElasticSearch\API\Annotation\Mapping\TargetEntity;
8
use AmaTeam\ElasticSearch\API\Entity\Descriptor;
9
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\View;
10
use AmaTeam\ElasticSearch\Entity\Annotation\PropertyAnnotationListenerInterface;
11
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations as StructureOperations;
14
use phpDocumentor\Reflection\TypeResolver;
15
use phpDocumentor\Reflection\Types\ContextFactory;
16
use ReflectionProperty;
17
18
class TargetEntityAnnotationListener implements PropertyAnnotationListenerInterface
19
{
20
    /**
21
     * @var TypeResolver
22
     */
23
    private $resolver;
24
    /**
25
     * @var ContextFactory
26
     */
27
    private $contextFactory;
28
29
    public function __construct()
30
    {
31
        $this->resolver = new TypeResolver();
32
        $this->contextFactory = new ContextFactory();
33
    }
34
35
    public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void
36
    {
37
        if (!($annotation instanceof TargetEntity)) {
38
            return;
39
        }
40
        $context = $this->contextFactory->createFromReflector($property);
41
        $type = $this->resolver->resolve($annotation->value, $context);
42
        $className = (string) $type;
43
        $structure = StructureOperations::toMutable($descriptor->getMapping());
44
        $descriptor->setMapping($structure);
45
        $Mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property);
46
        $Mapping = MappingOperations::toMutable($Mapping);
47
        $structure->setProperty($property->getName(), $Mapping);
48 View Code Duplication
        if (empty($annotation->views)) {
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...
49
            $view = ViewOperations::toMutable($Mapping->getDefaultView());
50
            $view->setTargetEntity($className);
51
            $Mapping->setDefaultView($view);
52
            return;
53
        }
54 View Code Duplication
        foreach ($annotation->views as $name) {
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...
55
            $view = ViewOperations::toMutable($Mapping->getView($name) ?? new View());
56
            $view->setTargetEntity($className);
57
            $Mapping->setView($name, $view);
58
        }
59
    }
60
}
61