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

Jagepard / Rudra-Annotation / 20488691127

24 Dec 2025 02:58PM UTC coverage: 97.917%. Remained the same
20488691127

push

github

web-flow
Delete LICENSE

47 of 48 relevant lines covered (97.92%)

2.79 hits per line

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

96.77
/src/Annotation.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * @author  Jagepard <jagepard@yandex.ru>
11
 * @license https://mozilla.org/MPL/2.0/  MPL-2.0
12
 */
13

14
namespace Rudra\Annotation;
15

16
use ReflectionClass;
17
use ReflectionMethod;
18
use Rudra\Exceptions\LogicException;
19

20
class Annotation implements AnnotationInterface
21
{
22
    /**
23
     * Parameter separator
24
     * --------------------
25
     * Разделитель параметров
26
     * 
27
     * in the line  ',', example: key='param', key2='param2'
28
     * in the array ';', example: {key:'param'; key2:'param2'}
29
     */
30
    const DELIMITER = ["string" => ',', "array" => ';'];
31

32
    /**
33
     * Assignment mark
34
     * --------------------
35
     * Знак присваивания
36
     * 
37
     * in the line  '=', example: key='param'
38
     * in the array ':', example: {key:'param'}
39
     */
40
    const ASSIGNMENT = ["string" => '=', "array" => ':'];
41

42
    /**
43
     * @param string $className
44
     * @param string|null $methodName
45
     * @return array
46
     */
47
    public function getAnnotations(string $className, ?string $methodName = null): array
3✔
48
    {
49
        $docBlock = $this->getReflection($className, $methodName)->getDocComment();
3✔
50

51
        if (is_string($docBlock)) {
3✔
52
            return $this->parseAnnotations($docBlock);
2✔
53
        }
54

55
        return [];
1✔
56
    }
57

58
    /**
59
     * @param string $className
60
     * @param string|null $methodName
61
     * @return array
62
     */
63
    public function getAttributes(string $className, ?string $methodName = null): array
2✔
64
    {
65
        if (version_compare(PHP_VERSION, '8.0', '<')) {
2✔
66
            throw new LogicException('Attributes are only supported in PHP 8.0 and above.');
×
67
        }
68

69
        $reflection = $this->getReflection($className, $methodName);
2✔
70
        $attributes = [];
2✔
71

72
        foreach ($reflection->getAttributes() as $attribute) {
2✔
73
            $attributeName = $this->extractShortClassName($attribute->getName());
2✔
74
            $attributes[$attributeName][] = $attribute->getArguments();
2✔
75
        }
76

77
        return $attributes;
2✔
78
    }
79

80
    /**
81
     * @param string $fullyQualifiedName
82
     * @return string
83
     */
84
    private function extractShortClassName(string $fullyQualifiedName): string
2✔
85
    {
86
        return basename(str_replace('\\', '/', $fullyQualifiedName));
2✔
87
    }
88

89
    /**
90
     * @param string $className
91
     * @param string|null $methodName
92
     * @return ReflectionClass|ReflectionMethod
93
     */
94
    private function getReflection(string $className, ?string $methodName = null): ReflectionClass|ReflectionMethod
5✔
95
    {
96
        return isset($methodName)
5✔
97
            ? new ReflectionMethod($className, $methodName)
3✔
98
            : new ReflectionClass($className);
5✔
99
    }
100

101
    /**
102
     * @param string $docBlock
103
     * @return array
104
     */
105
    private function parseAnnotations(string $docBlock): array
3✔
106
    {
107
        $annotations = [];
3✔
108

109
        /**
110
         * $matches[0][0] - @Annotation(param1, param2='param2', param3={param1;param2:'param2'})
111
         * $matches[1][0] - Annotation
112
         * $matches[2][0] - param1, param2 = 'param2', param3={param1;param2:'param2'}
113
         */
114
        if (preg_match_all("/@([A-Za-z_-]+)\((.*)?\)/", $docBlock, $matches)) {
3✔
115
            $count = count($matches[0]);
3✔
116
            $extractor = new ParamsExtractor();
3✔
117

118
            /**
119
             * $annotations = ["Annotation" => [[0 => "param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]]]
120
             */
121
            for ($i = 0; $i < $count; $i++) {
3✔
122
                $annotations[$matches[1][$i]][] = $extractor->getParams(
3✔
123
                    explode(Annotation::DELIMITER["string"], trim($matches[2][$i])),
3✔
124
                    Annotation::ASSIGNMENT["string"]
3✔
125
                );
3✔
126
            }
127
        }
128

129
        return $annotations;
3✔
130
    }
131
}
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