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

valkyrjaio / valkyrja / 16015663376

02 Jul 2025 03:52AM UTC coverage: 38.284% (-5.8%) from 44.066%
16015663376

push

github

web-flow
Merge pull request #38 from valkyrjaio/config-update

Config update

156 of 338 new or added lines in 42 files covered. (46.15%)

679 existing lines in 118 files now uncovered.

4122 of 10767 relevant lines covered (38.28%)

9.53 hits per line

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

68.97
/src/Valkyrja/Reflection/Reflection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Reflection;
15

16
use Closure;
17
use ReflectionClass;
18
use ReflectionClassConstant;
19
use ReflectionFunction;
20
use ReflectionFunctionAbstract;
21
use ReflectionMethod;
22
use ReflectionNamedType;
23
use ReflectionParameter;
24
use ReflectionProperty;
25
use UnitEnum;
26
use Valkyrja\Reflection\Contract\Reflection as Contract;
27
use Valkyrja\Reflection\Exception\RuntimeException;
28

29
use function class_exists;
30
use function interface_exists;
31
use function is_callable;
32

33
/**
34
 * Class Reflection.
35
 *
36
 * @author Melech Mizrachi
37
 */
38
class Reflection implements Contract
39
{
40
    /**
41
     * Cached reflection classes.
42
     *
43
     * @var array<string, ReflectionClass>
44
     */
45
    protected array $classReflections = [];
46

47
    /**
48
     * Cached reflection classes.
49
     *
50
     * @var array<string, ReflectionClassConstant>
51
     */
52
    protected array $constantReflections = [];
53

54
    /**
55
     * Cached reflection classes.
56
     *
57
     * @var array<string, ReflectionProperty>
58
     */
59
    protected array $propertyReflections = [];
60

61
    /**
62
     * Cached reflection classes.
63
     *
64
     * @var array<string, ReflectionMethod>
65
     */
66
    protected array $methodReflections = [];
67

68
    /**
69
     * Cached reflection classes.
70
     *
71
     * @var array<string, ReflectionFunction>
72
     */
73
    protected array $functionReflections = [];
74

75
    /**
76
     * @inheritDoc
77
     */
78
    public function forClass(string $class): ReflectionClass
79
    {
80
        $index = $class;
22✔
81

82
        return $this->classReflections[$index]
22✔
83
            ??= new ReflectionClass($class);
22✔
84
    }
85

86
    /**
87
     * @inheritDoc
88
     */
89
    public function forClassConstant(string $class, string $const): ReflectionClassConstant
90
    {
91
        $index = $class . $const;
2✔
92

93
        return $this->constantReflections[$index]
2✔
94
            ??= $this->forClass($class)->getReflectionConstant($const)
2✔
95
            ?: throw new RuntimeException("Failed to retrieve constant $const for $class");
×
96
    }
97

98
    /**
99
     * @inheritDoc
100
     */
101
    public function forClassProperty(string $class, string $property): ReflectionProperty
102
    {
103
        $index = $class . $property;
2✔
104

105
        return $this->propertyReflections[$index]
2✔
106
            ??= $this->forClass($class)->getProperty($property);
2✔
107
    }
108

109
    /**
110
     * @inheritDoc
111
     */
112
    public function forClassMethod(string $class, string $method): ReflectionMethod
113
    {
114
        $index = $class . $method;
6✔
115

116
        return $this->methodReflections[$index]
6✔
117
            ??= $this->forClass($class)->getMethod($method);
6✔
118
    }
119

120
    /**
121
     * @inheritDoc
122
     */
123
    public function forFunction(string $function): ReflectionFunction
124
    {
125
        $index = $function;
2✔
126

127
        return $this->functionReflections[$index]
2✔
128
            ??= new ReflectionFunction($function);
2✔
129
    }
130

131
    /**
132
     * @inheritDoc
133
     */
134
    public function forClosure(Closure $closure): ReflectionFunction
135
    {
136
        return new ReflectionFunction($closure);
2✔
137
    }
138

139
    /**
140
     * @inheritDoc
141
     */
142
    public function getDependencies(ReflectionFunctionAbstract $reflection): array
143
    {
144
        return $this->getDependenciesFromParameters(...$reflection->getParameters());
4✔
145
    }
146

147
    /**
148
     * @inheritDoc
149
     */
150
    public function getDependenciesFromParameters(ReflectionParameter ...$parameters): array
151
    {
152
        // Setup to find any injectable objects through the service container
153
        $dependencies = [];
4✔
154

155
        // Iterate through the method's parameters
156
        foreach ($parameters as $parameter) {
4✔
UNCOV
157
            $type = $parameter->getType();
×
158

159
            if (
160
                // The type is a ReflectionNamedType
UNCOV
161
                $type instanceof ReflectionNamedType
×
162
                // The name is valid
UNCOV
163
                && ($name = $type->getName())
×
164
                // The name is not a callable
UNCOV
165
                && ! is_callable($name)
×
166
                // The class or interface exists
UNCOV
167
                && (class_exists($name) || interface_exists($name))
×
168
                // and it isn't an enum
UNCOV
169
                && ! is_a($name, UnitEnum::class, true)
×
170
                // It's not built in
UNCOV
171
                && ! $type->isBuiltin()
×
172
            ) {
173
                // Set the injectable in the array
UNCOV
174
                $dependencies[] = $name;
×
175
            }
176
        }
177

178
        return $dependencies;
4✔
179
    }
180
}
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