Failed Conditions
Pull Request — new-parser-ast-metadata (#5)
by
unknown
02:14
created

FallbackReferenceResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
wmc 13
eloc 23
dl 0
loc 51
ccs 21
cts 25
cp 0.84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubjectNamespaceName() 0 11 5
B resolve() 0 36 8
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 ReflectionFunctionAbstract;
12
use ReflectionMethod;
13
use ReflectionProperty;
14
use Reflector;
15
use function assert;
16
use function explode;
17
use function strpos;
18
use function strtolower;
19
20
/**
21
 * @internal
22
 */
23
final class FallbackReferenceResolver implements ReferenceResolver
24
{
25 44
    public function resolve(Reference $reference, Scope $scope) : string
26
    {
27 44
        $identifier = $reference->getIdentifier();
28 44
        $imports    = $scope->getImports();
29
30 44
        if ($reference->isFullyQualified()) {
31 14
            return $identifier;
32
        }
33
34 34
        $identifierLower = strtolower($identifier);
35
36 34
        if (isset($imports[$identifierLower])) {
37 20
            return $imports[$identifierLower];
38
        }
39
40 16
        if (strpos($identifierLower, '\\') !== false) {
41 1
            $namespacePart = explode('\\', $identifierLower, 2)[0];
42
43 1
            if (isset($imports[$namespacePart])) {
44
                return $imports[$namespacePart] . '\\' . explode('\\', $identifier, 2)[1];
45
            }
46
        }
47
48 16
        $subject = $scope->getSubject();
49
50 16
        if (! $subject instanceof ReflectionClass && ! $subject instanceof ReflectionFunctionAbstract) {
51 6
            return $identifier;
52
        }
53
54 10
        $namespace = $this->getSubjectNamespaceName($scope->getSubject());
55
56 10
        if ($namespace === '') {
57 4
            return $identifier;
58
        }
59
60 6
        return $namespace . '\\' . $identifier;
61
    }
62
63 10
    private function getSubjectNamespaceName(Reflector $subject) : string
64
    {
65 10
        if ($subject instanceof ReflectionClass || $subject instanceof ReflectionFunction) {
66 10
            return $subject->getNamespaceName();
67
        }
68
69
        if ($subject instanceof ReflectionProperty || $subject instanceof ReflectionMethod) {
70
            return $subject->getDeclaringClass()->getNamespaceName();
71
        }
72
73
        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...
74
    }
75
}
76