|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Docblock package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gossi\docblock\tags; |
|
11
|
|
|
|
|
12
|
|
|
use phootwork\lang\Text; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Represents tags which are in the format |
|
16
|
|
|
* |
|
17
|
|
|
* @tag [Type] [Variable] [Description] |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractVarTypeTag extends AbstractTypeTag { |
|
20
|
|
|
protected string $variable = ''; |
|
|
|
|
|
|
21
|
|
|
protected bool $isVariadic = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @see https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/src/phpDocumentor/Reflection/DocBlock/Tag/ParamTag.php Original Method: setContent() |
|
25
|
|
|
* @see \gossi\docblock\tags\AbstractTypeTag::parse() |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $content |
|
28
|
|
|
*/ |
|
29
|
15 |
|
protected function parse(string $content): void { |
|
30
|
15 |
|
$parts = preg_split('/(\s+)/Su', $content, 3, PREG_SPLIT_DELIM_CAPTURE); |
|
31
|
|
|
|
|
32
|
15 |
|
$this->parseType($parts); |
|
33
|
15 |
|
$this->parseVariable($parts); |
|
34
|
15 |
|
$this->setDescription(implode('', $parts)); |
|
35
|
15 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Parses the type from the extracted parts |
|
39
|
|
|
* |
|
40
|
|
|
* @param string[] $parts |
|
41
|
|
|
*/ |
|
42
|
15 |
|
private function parseType(array &$parts): void { |
|
43
|
|
|
// if the first item that is encountered is not a variable; it is a type |
|
44
|
15 |
|
if (isset($parts[0]) |
|
45
|
15 |
|
&& (strlen($parts[0]) > 0) |
|
46
|
15 |
|
&& !str_starts_with($parts[0], '$') |
|
47
|
15 |
|
&& !str_starts_with($parts[0], '...$')) { |
|
48
|
8 |
|
$this->type = array_shift($parts); |
|
49
|
8 |
|
array_shift($parts); |
|
50
|
|
|
} |
|
51
|
15 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Parses the variable from the extracted parts |
|
55
|
|
|
* |
|
56
|
|
|
* @param string[] $parts |
|
57
|
|
|
*/ |
|
58
|
15 |
|
private function parseVariable(array &$parts): void { |
|
59
|
|
|
// if the next item starts with a $ or ...$ it must be the variable name |
|
60
|
15 |
|
if (isset($parts[0]) |
|
61
|
15 |
|
&& (strlen($parts[0]) > 0) |
|
62
|
15 |
|
&& (str_starts_with($parts[0], '$') || str_starts_with($parts[0], '...$'))) { |
|
63
|
7 |
|
$this->variable = array_shift($parts); |
|
64
|
7 |
|
array_shift($parts); |
|
65
|
|
|
|
|
66
|
7 |
|
if (str_starts_with($this->variable, '...')) { |
|
67
|
3 |
|
$this->isVariadic = true; |
|
68
|
3 |
|
$this->variable = substr($this->variable, 3); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
15 |
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
public function toString(): string { |
|
74
|
8 |
|
$type = $this->type === '' ? '' : $this->type . ' '; |
|
75
|
8 |
|
$var = $this->variable !== '' |
|
76
|
8 |
|
? ($this->isVariadic ? '...' : '') . $this->variable . ' ' : ''; |
|
77
|
|
|
|
|
78
|
8 |
|
return trim(sprintf('@%s %s%s%s', $this->tagName, $type, $var, $this->description)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the variable name, starting with `$` |
|
83
|
|
|
* |
|
84
|
|
|
* @return string the variable name |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function getExpression(): string { |
|
87
|
2 |
|
return $this->variable; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Sets the variable name |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $variable the new variable name |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function setVariable(string $variable): self { |
|
98
|
3 |
|
if (str_starts_with($variable, '...')) { |
|
99
|
1 |
|
$this->setVariadic(true); |
|
100
|
1 |
|
$variable = substr($variable, 3); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
$this->variable = str_starts_with($variable, '$') ? $variable : "\$$variable"; |
|
104
|
|
|
|
|
105
|
3 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the variable name |
|
110
|
|
|
* |
|
111
|
|
|
* @return string the variable name |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function getVariable(): string { |
|
114
|
2 |
|
$variable = new Text($this->variable); |
|
115
|
|
|
|
|
116
|
2 |
|
return $variable->isEmpty() ? '' : $variable->slice(1)->toString(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns if the variable is variadic |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool if the variable is variadic |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function isVariadic(): bool { |
|
125
|
3 |
|
return $this->isVariadic; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Sets whether the variable should be variadic |
|
130
|
|
|
* |
|
131
|
|
|
* @param bool $variadic |
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function setVariadic(bool $variadic): self { |
|
136
|
2 |
|
$this->isVariadic = $variadic; |
|
137
|
|
|
|
|
138
|
2 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|