Failed Conditions
Push — new-parser-ast-metadata ( 990edc...704204 )
by Michael
02:25
created

FallbackReferenceResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 45
ccs 14
cts 22
cp 0.6364
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 30 6
A getSubjectNamespaceName() 0 11 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Parser\Reference;
6
7
use Doctrine\Annotations\Parser\Ast\Reference;
8
use Doctrine\Annotations\Parser\Scope;
9
use ReflectionClass;
10
use ReflectionFunction;
11
use ReflectionMethod;
12
use ReflectionProperty;
13
use Reflector;
14
use function explode;
15
use function strpos;
16
use function strtolower;
17
18
/**
19
 * @internal
20
 */
21
final class FallbackReferenceResolver implements ReferenceResolver
22
{
23 4
    public function resolve(Reference $reference, Scope $scope) : string
24
    {
25 4
        $identifier = $reference->getIdentifier();
26 4
        $imports    = $scope->getImports();
27
28 4
        if ($reference->isFullyQualified()) {
29
            return $identifier;
30
        }
31
32 4
        $identifierLower = strtolower($identifier);
33
34 4
        if (isset($imports[$identifierLower])) {
35 1
            return $imports[$identifierLower];
36
        }
37
38 3
        if (strpos($identifierLower, '\\') !== false) {
39
            $namespacePart = explode('\\', $identifierLower, 2)[0];
40
41
            if (isset($imports[$namespacePart])) {
42
                return $imports[$namespacePart] . '\\' . explode('\\', $identifier, 2)[1];
43
            }
44
        }
45
46 3
        $namespace = $this->getSubjectNamespaceName($scope->getSubject());
47
48 3
        if ($namespace === '') {
49 3
            return $identifier;
50
        }
51
52
        return $namespace . '\\' . $identifier;
53
    }
54
55 3
    private function getSubjectNamespaceName(Reflector $subject) : string
56
    {
57 3
        if ($subject instanceof ReflectionClass || $subject instanceof ReflectionFunction) {
58 3
            return $subject->getNamespaceName();
59
        }
60
61
        if ($subject instanceof ReflectionProperty || $subject instanceof ReflectionMethod) {
62
            return $subject->getDeclaringClass()->getNamespaceName();
63
        }
64
65
        assert(false, 'Unsupported Reflector');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
66
    }
67
}
68