ConstantExtractor::filter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yokai\EnumBundle;
6
7
use ReflectionClass;
8
use ReflectionException;
9
use Yokai\EnumBundle\Exception\CannotExtractConstantsException;
10
11
/**
12
 * @internal
13
 *
14
 * @author Yann Eugoné <[email protected]>
15
 */
16
class ConstantExtractor
17
{
18
    public function extract(string $pattern): array
19
    {
20
        [$class, $patternRegex] = $this->explode($pattern);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $patternRegex does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
21
22
        return $this->filter(
23
            $this->publicConstants($class),
24
            $patternRegex,
0 ignored issues
show
Bug introduced by
The variable $patternRegex does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
25
            $pattern
26
        );
27
    }
28
29
    private function filter(array $constants, string $regexp, string $pattern): array
30
    {
31
        $matchingNames = preg_grep($regexp, array_keys($constants));
32
33
        if (count($matchingNames) === 0) {
34
            throw CannotExtractConstantsException::noConstantMatchingPattern($pattern);
35
        }
36
37
        return array_values(array_intersect_key($constants, array_flip($matchingNames)));
38
    }
39
40
    private function publicConstants(string $class): array
41
    {
42
        try {
43
            $constants = (new ReflectionClass($class))->getReflectionConstants();
44
        } catch (ReflectionException $exception) {
45
            throw CannotExtractConstantsException::classDoNotExists($class);
46
        }
47
48
        $list = [];
49
        foreach ($constants as $constant) {
50
            if (!$constant->isPublic()) {
51
                continue;
52
            }
53
54
            $list[$constant->getName()] = $constant->getValue();
55
        }
56
57
        if (count($list) === 0) {
58
            throw CannotExtractConstantsException::classHasNoPublicConstant($class);
59
        }
60
61
        return $list;
62
    }
63
64
    private function explode(string $pattern): array
65
    {
66
        if (substr_count($pattern, '::') !== 1) {
67
            throw CannotExtractConstantsException::invalidPattern($pattern);
68
        }
69
70
        [$class, $constantsNamePattern] = explode('::', $pattern);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $constantsNamePattern does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
71
72
        if (substr_count($constantsNamePattern, '*') === 0) {
0 ignored issues
show
Bug introduced by
The variable $constantsNamePattern does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
73
            throw CannotExtractConstantsException::invalidPattern($pattern);
74
        }
75
76
        $constantsNameRegexp = sprintf(
77
            '#^%s$#',
78
            str_replace('*', '[0-9a-zA-Z_]+', $constantsNamePattern)
0 ignored issues
show
Bug introduced by
The variable $constantsNamePattern does not exist. Did you mean $pattern?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
79
        );
80
81
        return [$class, $constantsNameRegexp];
82
    }
83
}
84