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

StructureParameterAnnotationListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 50 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B accept() 10 18 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\Clazz;
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\Structure\View;
11
use AmaTeam\ElasticSearch\Entity\Annotation\StructureAnnotationListenerInterface;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\ViewOperations;
14
use ReflectionClass;
15
16
class StructureParameterAnnotationListener implements StructureAnnotationListenerInterface
17
{
18
    public function accept(ReflectionClass $reflection, Descriptor $descriptor, $annotation): void
19
    {
20
        if (!($annotation instanceof ParameterAnnotationInterface)) {
21
            return;
22
        }
23
        $mapping = MappingOperations::toMutable($descriptor->getMapping());
24
        $descriptor->setMapping($mapping);
25
        $views = $annotation instanceof ViewAwareAnnotationInterface ? $annotation->getViews() : null;
26 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...
27
            $view = ViewOperations::toMutable($mapping->getDefaultView());
28
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
29
            $mapping->setDefaultView($view);
30
            return;
31
        }
32 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...
33
            $view = ViewOperations::toMutable($mapping->getView($name) ?? new View());
34
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
35
            $mapping->setView($name, $view);
36
        }
37
    }
38
}
39