/
Method.php
249 lines (218 loc) · 8.26 KB
/
Method.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Safe;
use Safe\PhpStanFunctions\PhpStanFunction;
use Safe\PhpStanFunctions\PhpStanFunctionMapReader;
use Safe\PhpStanFunctions\PhpStanType;
class Method
{
const FALSY_TYPE = 1;
const NULLSY_TYPE = 2;
const EMPTY_TYPE = 3;
/**
* @var \SimpleXMLElement
*/
private $functionObject;
/**
* @var \SimpleXMLElement
*/
private $rootEntity;
/**
* @var string
*/
private $moduleName;
/**
* @var Parameter[]|null
*/
private $params = null;
/**
* @var int
*/
private $errorType;
/**
* The function prototype from the phpstan internal documentation (functionMap.php)
* @var PhpStanFunction|null
*/
private $phpstanSignarure;
/**
* @var PhpStanType
*/
private $returnType;
public function __construct(\SimpleXMLElement $_functionObject, \SimpleXMLElement $rootEntity, string $moduleName, PhpStanFunctionMapReader $phpStanFunctionMapReader, int $errorType)
{
$this->functionObject = $_functionObject;
$this->rootEntity = $rootEntity;
$this->moduleName = $moduleName;
$this->errorType = $errorType;
$functionName = $this->getFunctionName();
$this->phpstanSignarure = $phpStanFunctionMapReader->hasFunction($functionName) ? $phpStanFunctionMapReader->getFunction($functionName) : null;
$this->returnType = $this->phpstanSignarure ? $this->phpstanSignarure->getReturnType() : new PhpStanType($this->functionObject->type->__toString());
}
public function getFunctionName(): string
{
return $this->functionObject->methodname->__toString();
}
public function getErrorType(): int
{
return $this->errorType;
}
public function getSignatureReturnType(): string
{
return $this->returnType->getSignatureType($this->errorType);
}
/**
* @return Parameter[]
*/
public function getParams(): array
{
if ($this->params === null) {
if (!isset($this->functionObject->methodparam)) {
return [];
}
$phpStanFunction = $this->phpstanSignarure;
$params = [];
$i=1;
foreach ($this->functionObject->methodparam as $param) {
$notes = $this->stripReturnFalseText($this->getStringForXPath("(//docbook:refsect1[@role='parameters']//docbook:varlistentry)[$i]//docbook:note//docbook:para"));
$i++;
if (preg_match('/This parameter has been removed in PHP (\d+\.\d+\.\d+)/', $notes, $matches)) {
$removedVersion = $matches[1];
[$major, $minor] = explode('.', $removedVersion);
if ($major < 7 || ($major == 7 && $minor == 0)) {
// Ignore parameter if it was removed before PHP 7.1
continue;
}
}
$params[] = new Parameter($param, $phpStanFunction, $i-2);
}
$this->params = $params;
}
return $this->params;
}
public function getPhpDoc(): string
{
$str = "\n/**\n".
implode("\n", array_map(function (string $line) {
return rtrim(' * '.ltrim($line));
}, \explode("\n", \strip_tags($this->getDocBlock()))))
."\n */\n";
return $str;
}
private function getDocBlock(): string
{
$str = $this->stripReturnFalseText($this->getStringForXPath("//docbook:refsect1[@role='description']/docbook:para"));
$str .= "\n\n";
$i=1;
foreach ($this->getParams() as $parameter) {
$str .= '@param '.$parameter->getDocBlockType().' $'.$parameter->getParameterName().' ';
$str .= $this->getStringForXPath("(//docbook:refsect1[@role='parameters']//docbook:varlistentry)[$i]//docbook:para")."\n";
$i++;
}
$str .= $this->getReturnDocBlock();
$str .= '@throws '.FileCreator::toExceptionName($this->getModuleName()). "\n";
return $str;
}
public function getReturnDocBlock(): string
{
$returnDoc = $this->getStringForXPath("//docbook:refsect1[@role='returnvalues']/docbook:para");
$returnDoc = $this->stripReturnFalseText($returnDoc);
$bestReturnType = $this->getDocBlockReturnType();
if ($bestReturnType !== 'void') {
return '@return '.$bestReturnType. ' ' .$returnDoc."\n";
}
return '';
}
private function stripReturnFalseText(string $string): string
{
$string = \strip_tags($string);
switch ($this->errorType) {
case self::NULLSY_TYPE:
$string = $this->removeString($string, ', or NULL if an error occurs');
$string = $this->removeString($string, ' and NULL on failure');
$string = $this->removeString($string, ' or NULL on failure');
break;
case self::FALSY_TYPE:
$string = $this->removeString($string, 'or FALSE on failure');
$string = $this->removeString($string, '. Returns FALSE on error');
$string = $this->removeString($string, 'may return FALSE');
$string = $this->removeString($string, 'and FALSE on failure');
$string = $this->removeString($string, 'on success, or FALSE otherwise');
$string = $this->removeString($string, 'or FALSE on error');
$string = $this->removeString($string, 'or FALSE if an error occurred');
$string = $this->removeString($string, ' Returns FALSE otherwise.');
$string = $this->removeString($string, ' and FALSE if an error occurred');
$string = $this->removeString($string, 'the function will return TRUE, or FALSE otherwise');
break;
case self::EMPTY_TYPE:
$string = $this->removeString($string, ' or an empty string on error');
break;
default:
throw new \RuntimeException('Incorrect error type.');
}
return $string;
}
/**
* Removes a string, even if the string is split on multiple lines.
*/
private function removeString(string $string, string $search): string
{
$search = str_replace(' ', '\s+', $search);
$result = preg_replace('/[\s\,]*'.$search.'/m', '', $string);
if ($result === null) {
throw new \RuntimeException('An error occurred while calling preg_replace');
}
return $result;
}
private function getStringForXPath(string $xpath): string
{
$paragraphs = $this->rootEntity->xpath($xpath);
if ($paragraphs === false) {
throw new \RuntimeException('Error while performing Xpath request.');
}
$str = '';
foreach ($paragraphs as $paragraph) {
$str .= $this->getInnerXml($paragraph)."\n\n";
}
return trim($str);
}
private function getDocBlockReturnType(): string
{
return $this->returnType->getDocBlockType($this->errorType);
}
private function getInnerXml(\SimpleXMLElement $SimpleXMLElement): string
{
$element_name = $SimpleXMLElement->getName();
$inner_xml = $SimpleXMLElement->asXML();
if ($inner_xml === false) {
throw new \RuntimeException('Unable to serialize to XML');
}
$inner_xml = str_replace(['<'.$element_name.'>', '</'.$element_name.'>'], '', $inner_xml);
$inner_xml = trim($inner_xml);
return $inner_xml;
}
public function getModuleName(): string
{
return $this->moduleName;
}
/**
* The function is overloaded if at least one parameter is optional with no default value and this parameter is not by reference.
*
* @return bool
*/
public function isOverloaded(): bool
{
foreach ($this->getParams() as $parameter) {
if ($parameter->isOptionalWithNoDefault() && !$parameter->isByReference()) {
return true;
}
}
return false;
}
public function cloneAndRemoveAParameter(): Method
{
$new = clone $this;
$params = $this->getParams();
\array_pop($params);
$new->params = $params;
return $new;
}
}