Completed
Pull Request — master (#58)
by
unknown
01:40
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
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\PhpTranslation\Extractor;
12
13
use Elao\Enum\ReadableEnumInterface;
14
use Translation\Extractor\Model\SourceCollection;
15
use Translation\Extractor\Model\SourceLocation;
16
17
final class ReflectionEnumExtractor
18
{
19
    /**
20
     * Callable returning list of FCQN candidates for Enums.
21
     * It's necessary for files containing these classes to be already loaded.
22
     *
23
     * @var callable
24
     */
25
    private $classLoader;
26
27
    public function __construct(callable $classLoader = null)
28
    {
29
        $this->classLoader = $classLoader ?: 'get_declared_classes';
30
    }
31
32
    public function getSourceLocations(SourceCollection $sourceCollection)
33
    {
34
        foreach (call_user_func($this->classLoader) as $class) {
35
            if (!\in_array(ReadableEnumInterface::class, class_implements($class), true)) {
36
                continue;
37
            }
38
39
            $reflectionClass = new \ReflectionClass($class);
40
            if ($reflectionClass->isAbstract()) {
41
                continue;
42
            }
43
44
            $line = $reflectionClass->getMethod('readables')->getStartLine();
45
46
            /** @var ReadableEnumInterface $class */
47
            foreach ($class::readables() as $value => $label) {
48
                $sourceCollection->addLocation(new SourceLocation($label, $reflectionClass->getFileName(), $line));
49
            }
50
        }
51
    }
52
}
53