Passed
Push — develop ( 355f63...51016a )
by Mathieu
02:23
created

AbstractAnnotation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
dl 0
loc 30
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A initAnnotation() 0 19 5
1
<?php
2
3
namespace Neimheadh\SonataAnnotationBundle\Annotation;
4
5
use ReflectionException;
6
use ReflectionMethod;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
/**
10
 * Sonata admin annotation.
11
 */
12
abstract class AbstractAnnotation implements AnnotationInterface
13
{
14
15
    /**
16
     * Initialize annotation.
17
     *
18
     * @param array $values Annotation value list.
19
     *
20
     * @return void
21
     * @throws ReflectionException
22
     */
23
    protected function initAnnotation(
24
        array $values
25
    ): void {
26
        $accessor = PropertyAccess::createPropertyAccessor();
27
28
        $method = new ReflectionMethod($this, '__construct');
29
        $params = $method->getParameters();
30
31
        if (array_keys($values) === ['value']) {
32
            $values = is_array(
33
                $values['value']
34
            ) ? $values['value'] : [$values['value']];
35
36
            foreach ($values as $i => $value) {
37
                $accessor->setValue($this, $params[$i]->getName(), $value);
38
            }
39
        } else {
40
            foreach ($values as $arg => $value) {
41
                $accessor->setValue($this, $arg, $value);
42
            }
43
        }
44
    }
45
46
}