-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodAnnotation.php
More file actions
139 lines (112 loc) · 4.88 KB
/
Copy pathMethodAnnotation.php
File metadata and controls
139 lines (112 loc) · 4.88 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
<?php declare(strict_types = 1);
/**
* This file is part of ScaleUpStack/Annotations
*
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
* with this source code.
*
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
* @link https://github.com/scaleupstack/annotations
*/
namespace ScaleUpStack\Annotations\Annotation;
use ScaleUpStack\Annotations\Assert;
final class MethodAnnotation extends AbstractAnnotation
{
private $isStatic;
private $returnType;
private $methodName;
private $parameters;
public function __construct(string $tag, string $arguments)
{
// validate tag
$this->validateTag($tag, 'method');
parent::__construct($tag, $arguments);
// validate arguments string and extract information
// extract return type, method name and (yet) unprocessed parameters string
$pattern = '/^' .
'(static[ ]+)?' . // optional "static" declaration plus one or more spaces
'(' . // optional return type (union type possible) plus one or more spaces
'(' .
'(' .
self::PATTERN_DATA_TYPE . preg_quote('|') .
')*' .
self::PATTERN_DATA_TYPE .
')[ ]+' .
')?' .
self::PATTERN_METHOD_NAME . '\(' . // method name plus opening bracket
'(.*)' . // anything till the closing bracket
'\)' . // closing bracket
'$/';
Assert::regex($arguments, $pattern, 'Invalid @method type declaration %s.');
preg_match($pattern, $arguments, $matches);
$this->isStatic = $matches[1] !== '' ? true : false;
$this->returnType = $matches[3] ?: null;
$this->methodName = $matches[9];
// extract parameters from unprocessed parameters string
$parameters = $this->parseParametersString($matches[10]);
Assert::notNull(
$parameters,
sprintf(
'Invalid @method type declaration "%s".',
$arguments
)
);
$this->parameters = $parameters;
}
public function isStatic() : bool
{
return $this->isStatic;
}
public function returnType() : ?string
{
return $this->returnType;
}
public function methodName() : string
{
return $this->methodName;
}
public function parameters() : array
{
return $this->parameters;
}
private function parseParametersString(string $parametersString) : ?array
{
$parameterPattern = '/^' .
'(' . // optional datatype (union type allowed) plus space
'(' .
'(' .
self::PATTERN_DATA_TYPE . preg_quote('|') .
')*' .
self::PATTERN_DATA_TYPE .
') ' .
')?' .
'(' . self::PATTERN_VARIABLE_NAME . ')' . // parameter name
'( = ' . // optional default value
'(' .
'"([^"]|\\\\")*"' . '|' . // in double quotes, or
"'([^']|\\\\')*'" . '|' . // in single quotes, or
'[^"\', ]+' . // without quotes, e.g. numbers or null
')' .
')?' .
'($|, (.+)$)/'; // end or <comma space and rest till end>
$parameters = [];
while ($parametersString) {
$count = preg_match($parameterPattern, $parametersString, $matches);
if (1 !== $count) {
return null;
}
$parameterName = $matches[9];
$dataType = $matches[2] ?: null;
$hasDefaultValue = '' !== $matches[11];
$parameters[$parameterName] = [
'dataType' => $dataType,
'hasDefaultValue' => $hasDefaultValue,
];
if ($hasDefaultValue) {
$parameters[$parameterName]['default'] = $matches[11];
}
$parametersString = array_key_exists(15, $matches) ? $matches[15] : '';
}
return $parameters;
}
}