Completed
Push — master ( bbf7d6...c195d3 )
by Peter
02:14
created

ConstantDetector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 57.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 32
ccs 8
cts 14
cp 0.5714
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B detect() 0 24 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 3
    public static function detect($class)
19
    {
20 3
        $constants = [];
21 3
        $reflection = new \ReflectionClass($class);
22
23 3
        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
            foreach ($reflection->getReflectionConstants() as $constant) {
27
                if ($constant->isPublic()) {
28
                    $constants[$constant->getName()] = $constant->getValue();
29
                }
30
            }
31
32
            return $constants;
33
        }
34
35
        // In PHP < 7.1 all class constants were public by definition
36 3
        foreach ($reflection->getConstants() as $constant_name => $constant_value) {
37 3
            $constants[$constant_name] = $constant_value;
38 3
        }
39
40 3
        return $constants;
41
    }
42
}
43