Completed
Push — master ( 454e04...38876e )
by Marco
21s queued 11s
created

ConstantReflector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 51
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A reflect() 13 13 2
A getAllConstants() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\Reflector;
6
7
use Roave\BetterReflection\Identifier\Identifier;
8
use Roave\BetterReflection\Identifier\IdentifierType;
9
use Roave\BetterReflection\Reflection\Reflection;
10
use Roave\BetterReflection\Reflection\ReflectionConstant;
11
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
12
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
13
14 View Code Duplication
class ConstantReflector implements Reflector
0 ignored issues
show
Duplication introduced by Jaroslav Hanslík
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /** @var SourceLocator */
17
    private $sourceLocator;
18
19
    /** @var ClassReflector */
20
    private $classReflector;
21
22
    public function __construct(SourceLocator $sourceLocator, ClassReflector $classReflector)
23
    {
24
        $this->sourceLocator  = $sourceLocator;
25
        $this->classReflector = $classReflector;
26
    }
27
28
    /**
29
     * Create a ReflectionConstant for the specified $constantName.
30
     *
31
     * @return ReflectionConstant
32
     *
33
     * @throws IdentifierNotFound
34
     */
35
    public function reflect(string $constantName) : Reflection
36
    {
37
        $identifier = new Identifier($constantName, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT));
38
39
        /** @var ReflectionConstant|null $constantInfo */
40
        $constantInfo = $this->sourceLocator->locateIdentifier($this->classReflector, $identifier);
41
42
        if ($constantInfo === null) {
43
            throw Exception\IdentifierNotFound::fromIdentifier($identifier);
44
        }
45
46
        return $constantInfo;
47
    }
48
49
    /**
50
     * Get all the constants available in the scope specified by the SourceLocator.
51
     *
52
     * @return array<int, ReflectionConstant>
0 ignored issues
show
Documentation introduced by Jaroslav Hanslík
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54
    public function getAllConstants() : array
55
    {
56
        /** @var array<int,ReflectionConstant> $allConstants */
57
        $allConstants = $this->sourceLocator->locateIdentifiersByType(
58
            $this,
59
            new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT)
60
        );
61
62
        return $allConstants;
63
    }
64
}
65