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

ParameterPropertyAnnotationListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 43.48 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B accept() 10 21 5

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\Parameter\Infrastructure\ParameterAnnotationInterface;
8
use AmaTeam\ElasticSearch\API\Annotation\Mapping\Infrastructure\ViewAwareAnnotationInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Descriptor;
10
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\View;
11
use AmaTeam\ElasticSearch\Entity\Annotation\PropertyAnnotationListenerInterface;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations;
14
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations as StructureOperations;
15
use ReflectionProperty;
16
17
class ParameterPropertyAnnotationListener implements PropertyAnnotationListenerInterface
18
{
19
    public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void
20
    {
21
        if (!($annotation instanceof ParameterAnnotationInterface)) {
22
            return;
23
        }
24
        $structure = StructureOperations::toMutable($descriptor->getMapping());
25
        $descriptor->setMapping($structure);
26
        $mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property);
27
        $mapping = MappingOperations::toMutable($mapping);
28
        $structure->setProperty($property->getName(), $mapping);
29
        $views = $annotation instanceof ViewAwareAnnotationInterface ? $annotation->getViews() : null;
30 View Code Duplication
        if (empty($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...
31
            $view = ViewOperations::toMutable($mapping->getDefaultView());
32
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
33
            $mapping->setDefaultView($view);
34
            return;
35
        }
36 View Code Duplication
        foreach ($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...
37
            $view = ViewOperations::toMutable($mapping->getView($name) ?? new View());
38
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
39
            $mapping->setView($name, $view);
40
        }
41
    }
42
}
43