Completed
Push — master ( e61af3...334344 )
by Peter
05:18
created

ConstantDetector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 30
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B detect() 0 22 5
1
<?php
2
3
/**
4
 * Carousel project.
5
 *
6
 * @author Peter Gribanov <[email protected]>
7
 */
8
9
namespace GpsLab\Component\Enum;
10
11
class ConstantDetector
12
{
13
    /**
14
     * @param string $class
15
     *
16
     * @return array
17
     */
18 5
    public static function detect($class)
19
    {
20 5
        $constants = [];
21 5
        $reflection = new \ReflectionClass($class);
22
23 5
        if (PHP_VERSION_ID >= 70100) {
24
            // Since PHP-7.1 visibility modifiers are allowed for class constants
25
            // for enumerations we are only interested in public once.
26 5
            foreach ($reflection->getReflectionConstants() as $constant) {
27 5
                if ($constant->isPublic()) {
28 5
                    $constants[$constant->getName()] = $constant->getValue();
29
                }
30
            }
31
        } else {
32
            // In PHP < 7.1 all class constants were public by definition
33
            foreach ($reflection->getConstants() as $constant_name => $constant_value) {
34
                $constants[$constant_name] = $constant_value;
35
            }
36
        }
37
38 5
        return $constants;
39
    }
40
}
41