Completed
Push — master ( ba55b5...96e451 )
by Zoltán
02:51
created

EnumerationBase::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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 {
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 __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...
54
        $calledClass = get_called_class();
55
        $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...
56
57
        if(array_key_exists(EnumerationFieldDefinitionInterface::class, class_implements($calledClass))) {
58
            $fields = static::defineFields()[$name];
59
            array_unshift($fields, $args[0]);
60
61
            $args = $fields;
62
        }
63
64
        $reflector = new ReflectionClass($calledClass);
65
        return $reflector->newInstanceArgs($args);
66
    }
67
68
    // ==========================================
69
    // StringConvertibleInterface implementation
70
    // ==========================================
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function __toString() {
76
        return $this->value;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function toString() {
83
        return $this->value;
84
    }
85
86
    // ===================================
87
    // Countable interface implementation
88
    // ===================================
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function count() {
94
        return count(self::toArray());
95
    }
96
97
}
98