EnumerationTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnumeration() 0 4 1
A addEnumeration() 0 7 3
A handleAddArray() 0 10 3
A checkEnumeration() 0 9 4
1
<?php
2
namespace AlgoWeb\xsdTypes\Facets;
3
4
trait EnumerationTrait
5
{
6
    /**
7
     * @Exclude
8
     * @var array Defines a list of acceptable values
9
     */
10
    private $enumeration = null;
11
12
    /**
13
     * @param array $enumerationValues Defines a list of acceptable values
14
     */
15
    protected function setEnumeration(array $enumerationValues)
16
    {
17
        $this->enumeration = $enumerationValues;
18
    }
19
20
    /**
21
     * @param string $enumerationValue adds a value to the enumeration set
22
     */
23
    protected function addEnumeration($enumerationValue)
24
    {
25
        if (!is_array($this->enumeration)) {
26
            $this->enumeration = [];
27
        }
28
        $this->handleAddArray($enumerationValue) ? :$this->enumeration[] = $enumerationValue;
29
    }
30
    /**
31
     * @param string $enumerationValue adds a value to the enumeration set
32
     */
33
    private function handleAddArray($enumerationValue)
34
    {
35
        if (!is_array($enumerationValue)) {
36
            return false;
37
        }
38
        foreach ($enumerationValue as $eValue) {
39
            $this->addEnumeration($eValue);
40
        }
41
        return;
42
    }
43
        
44
45
    private function checkEnumeration($v)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $v. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
46
    {
47
        if (is_array($this->enumeration) && 0 != count($this->enumeration) && !in_array($v, $this->enumeration)) {
48
            throw new \InvalidArgumentException(
49
                'The provided value for ' . get_class($this) . ' is not ' .
50
                implode(' || ', $this->enumeration)
51
            );
52
        }
53
    }
54
}
55