Completed
Pull Request — master (#3)
by Peter
04:21
created

ConstantDetector::detect()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 9
cp 0.8889
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0218
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
        $reflection = new \ReflectionClass($class);
21
22 5
        if (PHP_VERSION_ID >= 70100) {
23
            // Since PHP-7.1 visibility modifiers are allowed for class constants
24
            // for enumerations we are only interested in public once.
25 5
            $constants = [];
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
        return $reflection->getConstants();
37
    }
38
}
39