Completed
Push — master ( a6b3a4...c6ed8e )
by Peter
03:08
created

ConstantDetector::detect()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 8
cts 11
cp 0.7272
rs 8.5125
cc 5
eloc 11
nc 5
nop 1
crap 5.5069
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
32 5
            return $constants;
33
        }
34
35
        // In PHP < 7.1 all class constants were public by definition
36
        foreach ($reflection->getConstants() as $constant_name => $constant_value) {
37
            $constants[$constant_name] = $constant_value;
38
        }
39
40
        return $constants;
41
    }
42
}
43