Completed
Pull Request — master (#58)
by
unknown
14:30
created

ReflectionEnumExtractor::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
4
namespace Elao\Enum\Bridge\PhpTranslation\Extractor;
5
6
use Elao\Enum\ReadableEnumInterface;
7
use Translation\Extractor\Model\SourceCollection;
8
use Translation\Extractor\Model\SourceLocation;
9
10
final class ReflectionEnumExtractor
11
{
12
    /**
13
     * Callable returning list of FCQN candidates for Enums.
14
     * It's necessary for files containing these classes to be already loaded.
15
     *
16
     * @var callable
17
     */
18
    private $classLoader;
19
20
    public function __construct(callable $classLoader = null)
21
    {
22
        $this->classLoader = $classLoader ?: 'get_declared_classes';
23
    }
24
25
    public function getSourceLocations(SourceCollection $sourceCollection)
26
    {
27
        foreach (call_user_func($this->classLoader) as $class) {
28
            if (!\in_array(ReadableEnumInterface::class, class_implements($class), true)) {
29
                continue;
30
            }
31
32
            $reflectionClass = new \ReflectionClass($class);
33
            if ($reflectionClass->isAbstract()) {
34
                continue;
35
            }
36
37
            $line = $reflectionClass->getMethod('readables')->getStartLine();
38
39
            /** @var ReadableEnumInterface $class */
40
            foreach ($class::readables() as $value => $label) {
41
                $sourceCollection->addLocation(new SourceLocation($label, $reflectionClass->getFileName(), $line));
42
            }
43
        }
44
    }
45
}
46