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

VirtualProperty::__construct()   B

Complexity

Conditions 10
Paths 98

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 98
nop 4
dl 0
loc 35
rs 7.6666
ccs 1
cts 1
cp 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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