Completed
Push — master ( 91cd10...dc7d5f )
by Brent
09:16
created

ValueObject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 20.95 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 22
loc 105
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A all() 12 12 2
A only() 0 6 1
A except() 0 6 1
A fillable() 0 6 1
A toArray() 0 8 2
A getPublicProperties() 10 10 2

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
namespace Spatie\ValueObject;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
abstract class ValueObject
9
{
10
11
    /** @var array */
12
    protected $exceptKeys = [];
13
14
    /** @var array */
15
    protected $onlyKeys = [];
16
17
    /** @var array */
18
    protected $fillable = [];
19
20
    public function __construct(array $parameters)
21
    {
22
        $class = new ReflectionClass(static::class);
23
24
        $properties = $this->getPublicProperties($class);
25
26
        foreach ($properties as $property) {
27
            $value = $parameters[$property->getName()] ?? null;
28
29
            $property->set($value);
30
        }
31
32
        foreach (array_keys($parameters) as $propertyName) {
33
            if (isset($properties[$propertyName])) {
34
                continue;
35
            }
36
37
            throw ValueObjectException::unknownPublicProperty($propertyName, $class);
38
        }
39
    }
40
41 View Code Duplication
    public function all(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43
        $class = new ReflectionClass(static::class);
44
45
        $values = [];
46
47
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
48
            $values[$property->getName()] = $property->getValue($this);
49
        }
50
51
        return $values;
52
    }
53
54
    /**
55
     * @param string ...$keys
56
     *
57
     * @return static
58
     */
59
    public function only(string ...$keys)
60
    {
61
        $this->onlyKeys = array_merge($this->onlyKeys, $keys);
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string ...$keys
68
     *
69
     * @return static
70
     */
71
    public function except(string ...$keys)
72
    {
73
        $this->exceptKeys = array_merge($this->exceptKeys, $keys);
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return static
80
     */
81
    public function fillable()
82
    {
83
        $this->only(...$this->fillable);
84
85
        return $this;
86
    }
87
88
    public function toArray(): array
89
    {
90
        if (count($this->onlyKeys)) {
91
            return Arr::only($this->all(), $this->onlyKeys);
92
        }
93
94
        return Arr::except($this->all(), $this->exceptKeys);
95
    }
96
97
    /**
98
     * @param \ReflectionClass $class
99
     *
100
     * @return array|\Spatie\ValueObject\Property[]
101
     */
102 View Code Duplication
    private function getPublicProperties(ReflectionClass $class): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
        $properties = [];
105
106
        foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
107
            $properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty);
108
        }
109
110
        return $properties;
111
    }
112
}
113