Repository navigation
Expand file tree
/
Copy pathAnnotation.php
More file actions
206 lines (182 loc) · 6.07 KB
/
Copy pathAnnotation.php
File metadata and controls
206 lines (182 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace Codeception\Util;
use ReflectionClass;
use ReflectionMethod;
use Reflector;
use function get_class;
use function in_array;
use function is_object;
use function json_decode;
use function preg_match;
use function preg_match_all;
use function sprintf;
use function substr;
use function trim;
/**
* Simple annotation parser. Take only key-value annotations for methods or class.
*/
class Annotation
{
/**
* @var ReflectionClass[]
*/
protected static array $reflectedClasses = [];
protected static string $regex = '/@%s(?:[ \t]*(.*?))?[ \t]*(?:\*\/)?\r?$/m';
protected ReflectionClass $reflectedClass;
/**
* @var ReflectionClass|ReflectionMethod
*/
protected Reflector $currentReflectedItem;
/**
* Grabs annotation values.
*
* Usage example:
*
* ``` php
* <?php
* Annotation::forClass('MyTestCase')->fetch('guy');
* Annotation::forClass('MyTestCase')->method('testData')->fetch('depends');
* Annotation::forClass('MyTestCase')->method('testData')->fetchAll('depends');
* ```
*/
public static function forClass(object|string $class): self
{
if (is_object($class)) {
$class = get_class($class);
}
if (!isset(static::$reflectedClasses[$class])) {
static::$reflectedClasses[$class] = new ReflectionClass($class);
}
return new self(static::$reflectedClasses[$class]);
}
public static function forMethod(object|string $class, string $method): self
{
return self::forClass($class)->method($method);
}
/**
* Parses raw comment for annotations
*/
public static function fetchAnnotationsFromDocblock(string $annotation, string $docblock): array
{
if (preg_match_all(sprintf(self::$regex, $annotation), $docblock, $matched)) {
return $matched[1];
}
return [];
}
/**
* Fetches all available annotations
*/
public static function fetchAllAnnotationsFromDocblock(string $docblock): array
{
$annotations = [];
if (!preg_match_all(sprintf(self::$regex, '(\w+)'), $docblock, $matched)) {
return $annotations;
}
foreach ($matched[1] as $k => $annotation) {
if (!isset($annotations[$annotation])) {
$annotations[$annotation] = [];
}
$annotations[$annotation][] = $matched[2][$k];
}
return $annotations;
}
public function __construct(ReflectionClass $reflectionClass)
{
$this->currentReflectedItem = $reflectionClass;
$this->reflectedClass = $reflectionClass;
}
public function method(string $method): self
{
$this->currentReflectedItem = $this->reflectedClass->getMethod($method);
return $this;
}
public function fetch(string $annotation): ?string
{
$attr = $this->attribute($annotation);
if ($attr) {
$arguments = $attr->getArguments();
if (count($arguments) === 0) {
return '';
}
return $arguments[0];
}
$docBlock = (string)$this->currentReflectedItem->getDocComment();
if (preg_match(sprintf(self::$regex, $annotation), $docBlock, $matched)) {
return $matched[1];
}
return null;
}
public function fetchAll(string $annotation): array
{
$attr = $this->attribute($annotation);
if ($attr) {
if (!$attr->isRepeated()) {
return $attr->getArguments();
}
$attrs = $this->attributes();
if ($annotation === 'example') {
$annotation = 'examples'; // we renamed this annotation
}
$name = ucfirst($annotation);
$attrs = array_filter($attrs, fn ($a) => $a->getName() === "Codeception\\Attribute\\$name");
if ($annotation === 'examples') {
return array_map(fn (\ReflectionAttribute $a) => $a->getArguments(), $attrs);
}
return array_merge(...array_map(fn (\ReflectionAttribute $a) => $a->getArguments(), $attrs));
}
$docBlock = (string)$this->currentReflectedItem->getDocComment();
if (preg_match_all(sprintf(self::$regex, $annotation), $docBlock, $matched)) {
return $matched[1];
}
return [];
}
public function attributes(): array
{
$attrs = $this->currentReflectedItem->getAttributes();
$attrs = array_filter($attrs);
$attrs = array_filter($attrs, fn (\ReflectionAttribute $a) => str_starts_with($a->getName(), 'Codeception\\Attribute\\'));
return $attrs;
}
public function attribute($name): ?\ReflectionAttribute
{
$attrs = $this->attributes();
if ($name === 'example') {
$name = 'examples'; // we renamed this annotation
}
$name = ucfirst($name);
$attrs = array_filter($attrs, fn ($a) => $a->getName() === "Codeception\\Attribute\\$name");
if (empty($attrs)) {
return null;
}
return reset($attrs);
}
public function raw(): string|false
{
return $this->currentReflectedItem->getDocComment();
}
/**
* Returns an associative array value of annotation
* Either JSON or Doctrine-annotation style allowed
* Returns null if not a valid array data
*/
public static function arrayValue(string $annotation): ?array
{
$annotation = trim($annotation);
$openingBrace = substr($annotation, 0, 1);
// json-style data format
if (in_array($openingBrace, ['{', '['])) {
return json_decode($annotation, true, 512, JSON_THROW_ON_ERROR);
}
// doctrine-style data format
if ($openingBrace === '(') {
preg_match_all('#(\w+)\s*?=\s*?"(.*?)"\s*?[,)]#', $annotation, $matches, PREG_SET_ORDER);
$data = [];
foreach ($matches as $item) {
$data[$item[1]] = $item[2];
}
return $data;
}
return null;
}
}