GetConstantsTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A constantValueExists() 0 3 1
A getConstants() 0 7 2
1
<?php
2
3
namespace GinoPane\PHPolyglot\Supplemental;
4
5
use ReflectionClass;
6
7
/**
8
 * Trait GetConstantsTrait
9
 *
10
 * Get all object's constants as associative array
11
 *
12
 * @author Sergey <Gino Pane> Karavay
13
 */
14
trait GetConstantsTrait
15
{
16
    /**
17
     * Used to store an array of constants
18
     *
19
     * @var array
20
     */
21
    private static $constants = [];
22
23
    /**
24
     * Fills constants array if it is empty and returns it
25
     *
26
     * @return array
27
     */
28
    private function getConstants(): array
29
    {
30
        if (empty(self::$constants)) {
31
            self::$constants = (new ReflectionClass($this))->getConstants();
32
        }
33
34
        return self::$constants;
35
    }
36
37
    /**
38
     * Fills constants array if it is empty and returns it
39
     *
40
     * @param mixed $value
41
     *
42
     * @return bool
43
     */
44
    private function constantValueExists($value): bool
45
    {
46
        return array_search($value, $this->getConstants()) !== false;
47
    }
48
}
49