Passed
Pull Request — master (#1332)
by Asmir
02:30
created

VirtualProperty   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 53
rs 10
ccs 8
cts 9
cp 0.8889
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Annotation;
6
7
use JMS\Serializer\Exception\InvalidArgumentException;
8
9
/**
10
 * @Annotation
11
 * @Target({"METHOD", "CLASS"})
12
 *
13
 * @author Alexander Klimenkov <[email protected]>
14
 */
15
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
16
final class VirtualProperty
17
{
18
    /**
19
     * @var string|null
20
     */
21 36
    public $exp = null;
22
23 36
    /**
24 8
     * @var string|null
25 8
     */
26
    public $name = null;
27
28 36
    /**
29 9
     * @var array
30
     */
31
    public $options = [];
32 9
33
    public function __construct(array $data = [], ?string $name = null, ?string $exp = null, ?array $options = [])
34 36
    {
35
        if (isset($data['value'])) {
36
            $data['name'] = $data['value'];
37
            unset($data['value']);
38
        }
39
40
        foreach ($data as $key => $value) {
41
            if (!property_exists(self::class, $key)) {
42
                throw new InvalidArgumentException(sprintf('Unknown property "%s" on annotation "%s".', $key, self::class));
43
            }
44
45
            $this->{$key} = $value;
46
        }
47
48
        if (null !== $name) {
49
            $this->name = $name;
50
        }
51
52
        if (null !== $exp) {
53
            $this->exp = $exp;
54
        }
55
56
        if (0 !== count($options)) {
57
            $this->options = $options;
58
        }
59
60
        foreach ($options as $option) {
61
            if (is_array($option) && class_exists($option[0])) {
62
                $this->options[] = new $option[0]([], ...$option[1]);
63
64
                continue;
65
            }
66
67
            $this->options[] = $option;
68
        }
69
    }
70
}
71