Completed
Push — master ( 96e451...8d322f )
by Zoltán
02:44
created

EnumerationBase::size()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace BuildR\Utils\Enumeration;
2
3
use \ReflectionClass;
4
use \Countable;
5
use BuildR\Foundation\Object\StringConvertibleInterface;
6
use BuildR\Utils\Enumeration\Exception\EnumerationException;
7
8
class EnumerationBase implements StringConvertibleInterface, Countable {
0 ignored issues
show
Bug introduced by
There is one abstract method count in this class; you could implement it, or declare this class as abstract.
Loading history...
9
10
    public $value;
11
12
    private static $cache = [];
13
14
    public function __construct($value) {
15
        if(!$this->isValid($value)) {
16
            throw EnumerationException::invalidValue($value);
17
        }
18
19
        $this->value = $value;
20
    }
21
22
    public function isValid($value) {
23
        return (bool) in_array($value, self::toArray());
24
    }
25
26
    public function getValue() {
27
        return $this->value;
28
    }
29
30
    public static function toArray() {
31
        $enumClass = get_called_class();
32
33
        if(!array_key_exists($enumClass, self::$cache)) {
34
            $reflector = new ReflectionClass($enumClass);
35
            self::$cache[$enumClass] = $reflector->getConstants();
36
        }
37
38
        return self::$cache[$enumClass];
39
    }
40
41
    public static function getKeys() {
42
        return array_keys(self::toArray());
43
    }
44
45
    public static function isValidKey($key) {
46
        if(!is_string($key)) {
47
            throw EnumerationException::invalidKeyType(gettype($key));
48
        }
49
50
        return array_key_exists($key, self::toArray());
51
    }
52
53
    public static function size() {
54
        return count(self::toArray());
55
    }
56
57
    public static function __callStatic($name, $arguments) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
        $calledClass = get_called_class();
59
        $args[] = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
60
61
        if(array_key_exists(EnumerationFieldDefinitionInterface::class, class_implements($calledClass))) {
62
            $fields = static::defineFields()[$name];
63
            array_unshift($fields, $args[0]);
64
65
            $args = $fields;
66
        }
67
68
        $reflector = new ReflectionClass($calledClass);
69
        return $reflector->newInstanceArgs($args);
70
    }
71
    // ==========================================
72
    // StringConvertibleInterface implementation
73
74
    // ==========================================
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function __toString() {
80
        return $this->value;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function toString() {
87
        return $this->value;
88
    }
89
90
}
91