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

valkyrjaio / valkyrja / 15659580871

15 Jun 2025 04:44AM UTC coverage: 47.202%. Remained the same
15659580871

push

github

MelechMizrachi
PHP CS Fixer.

37 of 75 new or added lines in 13 files covered. (49.33%)

2 existing lines in 2 files now uncovered.

5331 of 11294 relevant lines covered (47.2%)

16.12 hits per line

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

1.32
/src/Valkyrja/Annotation/Annotations.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\Annotation;
15

16
use ReflectionException;
17
use Valkyrja\Annotation\Constant\Property;
18
use Valkyrja\Annotation\Contract\Annotations as Contract;
19
use Valkyrja\Annotation\Model\Contract\Annotation;
20
use Valkyrja\Annotation\Parser\Contract\Parser;
21
use Valkyrja\Reflection\Contract\Reflection;
22

23
use function array_merge;
24

25
/**
26
 * Class Annotations.
27
 *
28
 * @author Melech Mizrachi
29
 */
30
class Annotations implements Contract
31
{
32
    /**
33
     * Cache index constants.
34
     */
35
    /** @var string */
36
    protected const CLASS_CACHE = 'class';
37
    /** @var string */
38
    protected const CLASS_MEMBERS_CACHE = 'class.members';
39
    /** @var string */
40
    protected const CLASS_AND_MEMBERS_CACHE = 'class.and.members';
41
    /** @var string */
42
    protected const PROPERTY_CACHE = 'property';
43
    /** @var string */
44
    protected const PROPERTIES_CACHE = 'properties';
45
    /** @var string */
46
    protected const METHOD_CACHE = 'method';
47
    /** @var string */
48
    protected const METHODS_CACHE = 'methods';
49
    /** @var string */
50
    protected const FUNCTION_CACHE = 'function';
51

52
    /**
53
     * Cached annotations.
54
     *
55
     * @var array<string, Annotation[]>
56
     */
57
    protected static array $annotations = [];
58

59
    /**
60
     * Annotations constructor.
61
     */
62
    public function __construct(
63
        protected Parser $parser,
64
        protected Reflection $reflection
65
    ) {
66
    }
8✔
67

68
    /**
69
     * @inheritDoc
70
     */
71
    public function getParser(): Parser
72
    {
73
        return $this->parser;
×
74
    }
75

76
    /**
77
     * @inheritDoc
78
     */
79
    public function setParser(Parser $parser): void
80
    {
81
        $this->parser = $parser;
×
82
    }
83

84
    /**
85
     * @inheritDoc
86
     *
87
     * @throws ReflectionException
88
     */
89
    public function forClass(string $class): array
90
    {
91
        $index = static::CLASS_CACHE . $class;
×
92

93
        return self::$annotations[$index]
×
94
            ??= $this->setAnnotationValues(
×
NEW
95
                [
×
NEW
96
                    Property::CLASS_NAME => $class,
×
NEW
97
                ],
×
NEW
98
                ...$this->parser->getAnnotations((string) $this->reflection->forClass($class)->getDocComment())
×
NEW
99
            );
×
100
    }
101

102
    /**
103
     * @inheritDoc
104
     *
105
     * @throws ReflectionException
106
     */
107
    public function forClassMembers(string $class): array
108
    {
109
        $index = static::CLASS_MEMBERS_CACHE . $class;
×
110

111
        return self::$annotations[$index]
×
112
            ??= array_merge(
×
NEW
113
                $this->forClassProperties($class),
×
NEW
114
                $this->forClassMethods($class)
×
NEW
115
            );
×
116
    }
117

118
    /**
119
     * @inheritDoc
120
     *
121
     * @throws ReflectionException
122
     */
123
    public function forClassAndMembers(string $class): array
124
    {
125
        $index = static::CLASS_AND_MEMBERS_CACHE . $class;
×
126

127
        return self::$annotations[$index]
×
128
            ??= array_merge(
×
NEW
129
                $this->forClass($class),
×
NEW
130
                $this->forClassMembers($class)
×
NEW
131
            );
×
132
    }
133

134
    /**
135
     * @inheritDoc
136
     *
137
     * @throws ReflectionException
138
     */
139
    public function forClassProperty(string $class, string $property): array
140
    {
141
        $index      = static::PROPERTY_CACHE . $class . $property;
×
142
        $reflection = $this->reflection->forClassProperty($class, $property);
×
143

144
        return self::$annotations[$index]
×
145
            ??= $this->setAnnotationValues(
×
NEW
146
                [
×
NEW
147
                    Property::CLASS_NAME => $class,
×
NEW
148
                    Property::PROPERTY   => $property,
×
NEW
149
                    Property::STATIC     => $reflection->isStatic(),
×
NEW
150
                ],
×
NEW
151
                ...$this->parser->getAnnotations((string) $reflection->getDocComment())
×
NEW
152
            );
×
153
    }
154

155
    /**
156
     * @inheritDoc
157
     *
158
     * @throws ReflectionException
159
     */
160
    public function forClassProperties(string $class): array
161
    {
162
        $index = static::PROPERTIES_CACHE . $class;
×
163

164
        if (isset(self::$annotations[$index])) {
×
165
            return self::$annotations[$index];
×
166
        }
167

168
        $annotations = [];
×
169

170
        // Get the class's reflection
171
        // Iterate through the properties
172
        foreach ($this->reflection->forClass($class)->getProperties() as $property) {
×
173
            // Set the annotations in the list
174
            $annotations[] = $this->forClassProperty($class, $property->getName());
×
175
        }
176

177
        return self::$annotations[$index] = array_merge(...$annotations);
×
178
    }
179

180
    /**
181
     * @inheritDoc
182
     *
183
     * @throws ReflectionException
184
     */
185
    public function forClassMethod(string $class, string $method): array
186
    {
187
        $index      = static::METHOD_CACHE . $class . $method;
×
188
        $reflection = $this->reflection->forClassMethod($class, $method);
×
189

190
        return self::$annotations[$index]
×
191
            ??= $this->setAnnotationValues(
×
NEW
192
                [
×
NEW
193
                    Property::CLASS_NAME => $class,
×
NEW
194
                    Property::METHOD     => $method,
×
NEW
195
                    Property::STATIC     => $reflection->isStatic(),
×
NEW
196
                ],
×
NEW
197
                ...$this->parser->getAnnotations((string) $reflection->getDocComment())
×
NEW
198
            );
×
199
    }
200

201
    /**
202
     * @inheritDoc
203
     *
204
     * @throws ReflectionException
205
     */
206
    public function forClassMethods(string $class): array
207
    {
208
        $index = static::METHODS_CACHE . $class;
×
209

210
        if (isset(self::$annotations[$index])) {
×
211
            return self::$annotations[$index];
×
212
        }
213

214
        $annotations = [];
×
215

216
        // Get the class's reflection
217
        // Iterate through the methods
218
        foreach ($this->reflection->forClass($class)->getMethods() as $method) {
×
219
            // Set the annotations in the list
220
            $annotations[] = $this->forClassMethod($class, $method->getName());
×
221
        }
222

223
        return self::$annotations[$index] = array_merge(...$annotations);
×
224
    }
225

226
    /**
227
     * @inheritDoc
228
     *
229
     * @throws ReflectionException
230
     */
231
    public function forFunction(string $function): array
232
    {
233
        $index = static::FUNCTION_CACHE . $function;
×
234

235
        return self::$annotations[$index]
×
236
            ??= $this->setAnnotationValues(
×
NEW
237
                [
×
NEW
238
                    Property::FUNCTION => $function,
×
NEW
239
                ],
×
NEW
240
                ...$this->parser->getAnnotations(
×
NEW
241
                    (string) $this->reflection->forFunction($function)->getDocComment()
×
NEW
242
                )
×
NEW
243
            );
×
244
    }
245

246
    /**
247
     * Set the base annotation model values.
248
     *
249
     * @param array{class?: class-string, property?: non-empty-string, method?: non-empty-string, function?: callable-string, static?: bool} $properties     The properties
250
     * @param Annotation                                                                                                                     ...$annotations The annotations
251
     *
252
     * @return Annotation[]
253
     */
254
    protected function setAnnotationValues(array $properties, Annotation ...$annotations): array
255
    {
256
        foreach ($annotations as $annotation) {
×
257
            $annotation->setClass($properties[Property::CLASS_NAME] ?? null);
×
258
            $annotation->setProperty($properties[Property::PROPERTY] ?? null);
×
259
            $annotation->setMethod($properties[Property::METHOD] ?? null);
×
260
            $annotation->setFunction($properties[Property::FUNCTION] ?? null);
×
261
            $annotation->setStatic($properties[Property::STATIC] ?? false);
×
262
        }
263

264
        return $annotations;
×
265
    }
266
}
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