• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / reflection / 17533079061

07 Sep 2025 07:47PM UTC coverage: 79.856%. Remained the same
17533079061

push

github

BenMorel
Migrate to PHPUnit 10

111 of 139 relevant lines covered (79.86%)

10.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.5
/src/ImportResolver.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Reflection;
6

7
use Doctrine\Common\Annotations\TokenParser;
8
use InvalidArgumentException;
9
use ReflectionClass;
10
use ReflectionClassConstant;
11
use ReflectionMethod;
12
use ReflectionParameter;
13
use ReflectionProperty;
14
use Reflector;
15
use RuntimeException;
16

17
use function file_get_contents;
18
use function sprintf;
19
use function strpos;
20
use function strtolower;
21
use function substr;
22

23
/**
24
 * Resolves class names using the rules PHP uses internally.
25
 *
26
 * @see http://www.php.net/manual/en/language.namespaces.importing.php
27
 */
28
class ImportResolver
29
{
30
    private readonly string $namespace;
31

32
    /**
33
     * @var array<string, string>
34
     */
35
    private array $aliases;
36

37
    /**
38
     * Class constructor.
39
     *
40
     * @param Reflector $context A reflection of the context in which the types will be resolved.
41
     *                           The context can be a class, property, method or parameter.
42
     *
43
     * @throws InvalidArgumentException If the class or file name cannot be inferred from the context.
44
     */
45
    public function __construct(Reflector $context)
46
    {
47
        $class = $this->getDeclaringClass($context);
4✔
48

49
        if ($class === null) {
4✔
50
            throw $this->invalidArgumentException('declaring class', $context);
×
51
        }
52

53
        $fileName = $class->getFileName();
4✔
54

55
        if ($fileName === false) {
4✔
56
            throw $this->invalidArgumentException('file name', $context);
×
57
        }
58

59
        $source = @file_get_contents($fileName);
4✔
60

61
        if ($source === false) {
4✔
62
            throw new RuntimeException('Could not read ' . $fileName);
×
63
        }
64

65
        $parser = new TokenParser($source);
4✔
66

67
        $this->namespace = $class->getNamespaceName();
4✔
68
        $this->aliases = $parser->parseUseStatements($this->namespace);
4✔
69
    }
70

71
    /**
72
     * @param string $type A class or interface name.
73
     *
74
     * @return string The fully qualified class name.
75
     */
76
    public function resolve(string $type): string
77
    {
78
        $pos = strpos($type, '\\');
4✔
79

80
        if ($pos === 0) {
4✔
81
            return substr($type, 1); // Already fully qualified.
1✔
82
        }
83

84
        if ($pos === false) {
3✔
85
            $first = $type;
3✔
86
            $next = '';
3✔
87
        } else {
88
            $first = substr($type, 0, $pos);
3✔
89
            $next = substr($type, $pos);
3✔
90
        }
91

92
        $first = strtolower($first);
3✔
93

94
        if (isset($this->aliases[$first])) {
3✔
95
            return $this->aliases[$first] . $next;
2✔
96
        }
97

98
        return $this->namespace . '\\' . $type;
1✔
99
    }
100

101
    /**
102
     * Returns the ReflectionClass of the given Reflector.
103
     */
104
    private function getDeclaringClass(Reflector $reflector): ?ReflectionClass
105
    {
106
        if ($reflector instanceof ReflectionClass) {
4✔
107
            return $reflector;
4✔
108
        }
109

110
        if ($reflector instanceof ReflectionClassConstant) {
×
111
            return $reflector->getDeclaringClass();
×
112
        }
113

114
        if ($reflector instanceof ReflectionProperty) {
×
115
            return $reflector->getDeclaringClass();
×
116
        }
117

118
        if ($reflector instanceof ReflectionMethod) {
×
119
            return $reflector->getDeclaringClass();
×
120
        }
121

122
        if ($reflector instanceof ReflectionParameter) {
×
123
            return $reflector->getDeclaringClass();
×
124
        }
125

126
        return null;
×
127
    }
128

129
    private function invalidArgumentException(string $inferring, Reflector $reflector): InvalidArgumentException
130
    {
131
        return new InvalidArgumentException(sprintf(
×
132
            'Cannot infer the %s from the given %s',
×
133
            $inferring,
×
134
            $reflector::class,
×
135
        ));
×
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc