Failed Conditions
Pull Request — new-parser-ast-metadata (#4)
by Michael
05:13 queued 03:35
created

ReflectionConstantResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 50
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveStandaloneConstant() 0 9 2
A resolveClassOrInterfaceConstant() 0 19 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Assembler\Constant;
6
7
use Doctrine\Annotations\Assembler\Constant\Exception\ClassConstantNotFound;
8
use Doctrine\Annotations\Assembler\Constant\Exception\ConstantNotAccessible;
9
use Doctrine\Annotations\Assembler\Constant\Exception\ConstantResolutionException;
10
use Doctrine\Annotations\Assembler\Constant\Exception\InvalidClass;
11
use Doctrine\Annotations\Assembler\Constant\Exception\StandaloneConstantNotFound;
12
use Doctrine\Annotations\Metadata\Reflection\ClassReflectionProvider;
13
use ReflectionException;
14
use function assert;
15
use function constant;
16
use function defined;
17
use function strpos;
18
19
final class ReflectionConstantResolver implements ConstantResolver
20
{
21
    /** @var ClassReflectionProvider */
22
    private $classReflectionProvider;
23
24 18
    public function __construct(ClassReflectionProvider $classReflectionProvider)
25
    {
26 18
        $this->classReflectionProvider = $classReflectionProvider;
27 18
    }
28
29
    /**
30
     * @return mixed
31
     *
32
     * @throws ConstantResolutionException
33
     */
34 7
    public function resolveClassOrInterfaceConstant(string $holderName, string $constantName)
35
    {
36
        try {
37 7
            $classReflection = $this->classReflectionProvider->getClassReflection($holderName);
38 1
        } catch (ReflectionException $e) {
39 1
            throw InvalidClass::new($holderName, $constantName);
40
        }
41
42 6
        $constantReflection = $classReflection->getReflectionConstant($constantName);
43
44 6
        if ($constantReflection === false) {
45 1
            throw ClassConstantNotFound::new($holderName, $constantName);
46
        }
47
48 5
        if (! $constantReflection->isPublic()) {
49 1
            throw ConstantNotAccessible::new($holderName, $constantName);
50
        }
51
52 4
        return $constantReflection->getValue();
53
    }
54
55
    /**
56
     * @return mixed
57
     *
58
     * @throws ConstantResolutionException
59
     */
60 3
    public function resolveStandaloneConstant(string $constantName)
61
    {
62 3
        assert(strpos($constantName, '::') === false);
63
64 3
        if (! defined($constantName)) {
65
            throw StandaloneConstantNotFound::new($constantName);
66
        }
67
68 3
        return constant($constantName);
69
    }
70
}
71