Failed Conditions
Push — new-parser-ast-metadata ( 6127e0...5a4a16 )
by Michael
12s
created

getSubjectNamespaceName()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.125

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 3
cts 6
cp 0.5
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 1
crap 8.125
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 19
    public function resolve(Reference $reference, Scope $scope) : string
26
    {
27 19
        $identifier = $reference->getIdentifier();
28 19
        $imports    = $scope->getImports();
29
30 19
        if ($reference->isFullyQualified()) {
31 7
            return $identifier;
32
        }
33
34 12
        $identifierLower = strtolower($identifier);
35
36 12
        if (isset($imports[$identifierLower])) {
37 2
            return $imports[$identifierLower];
38
        }
39
40 10
        if (strpos($identifierLower, '\\') !== false) {
41
            $namespacePart = explode('\\', $identifierLower, 2)[0];
42
43
            if (isset($imports[$namespacePart])) {
44
                return $imports[$namespacePart] . '\\' . explode('\\', $identifier, 2)[1];
45
            }
46
        }
47
48 10
        $subject = $scope->getSubject();
49
50 10
        if (! $subject instanceof ReflectionClass && ! $subject instanceof ReflectionFunctionAbstract) {
51 1
            return $identifier;
52
        }
53
54 9
        $namespace = $this->getSubjectNamespaceName($scope->getSubject());
55
56 9
        if ($namespace === '') {
57 4
            return $identifier;
58
        }
59
60 5
        return $namespace . '\\' . $identifier;
61
    }
62
63 9
    private function getSubjectNamespaceName(Reflector $subject) : string
64
    {
65 9
        if ($subject instanceof ReflectionClass || $subject instanceof ReflectionFunction) {
66 9
            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