-
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathDocPage.php
More file actions
207 lines (174 loc) · 7.08 KB
/
DocPage.php
File metadata and controls
207 lines (174 loc) · 7.08 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
207
<?php
declare(strict_types=1);
namespace Safe\XmlDocParser;
use Safe\Generator\FileCreator;
use Safe\Templating\Filesystem;
use function explode;
use function strpos;
class DocPage
{
public function __construct(private readonly string $path)
{
}
public static function findDocDir(): string
{
return __DIR__ . '/../../doc';
}
public static function findReferenceDir(): string
{
return DocPage::findDocDir() . '/doc-en/en/reference';
}
// Ignore function if it was removed before PHP 8.1
private function getIsDeprecated(string $file): bool
{
if (preg_match('/&warn\.deprecated\.function-(\d+-\d+-\d+)\.removed-(\d+-\d+-\d+)/', $file, $matches)) {
$removedVersion = $matches[2];
[$major, $minor] = explode('-', $removedVersion);
if ($major < 8 || ($major == 8 && $minor == 0)) {
return true;
}
}
if (preg_match('/&warn\.removed\.function-(\d+-\d+-\d+)/', $file, $matches)) {
$removedVersion = $matches[1];
[$major, $minor] = explode('-', $removedVersion);
if ($major < 8 || ($major == 8 && $minor == 0)) {
return true;
}
}
return false;
}
public function getErrorType(): ErrorType
{
$file = file_get_contents($this->path);
if ($file === false) {
throw new \RuntimeException('An error occurred while reading '.$this->path);
}
if ($this->getIsDeprecated($file)) {
return ErrorType::UNKNOWN;
}
// Only evaluate the text inside the `<refsect1 role="returnvalues">...</refsect1>` section of the doc page.
// This minimizes 'false positives', where text such as "returns false when ..." could be matched outside
// the function's dedicated Return Values section.
$returnDocs = $this->extractSection('returnvalues', $file);
$detectErrorType = require Filesystem::generatorDir() . '/config/detectErrorType.php';
return $detectErrorType($returnDocs);
}
/**
* @return \SimpleXMLElement[]
*/
public function getMethodSynopsis(): array
{
/** @var string[] $cleanedFunctions */
$cleanedFunctions = [];
$file = \file_get_contents($this->path);
if ($file === false) {
throw new \RuntimeException('An error occurred while reading '.$this->path);
}
// Only evaluate the synopsis inside the `<refsect1 role="description">...</refsect1>` section of the doc page.
// Other synopses might occur in the `<refsect1 role="parameters">...</refsect1>` section, but these describe
// handlers, callbacks, and other callable-type arguments, not the function itself.
$fileDescriptionSection = $this->extractSection('description', $file);
if (!preg_match_all('/<\/?methodsynopsis[\s\S]*?>[\s\S]*?<\/methodsynopsis>/m', $fileDescriptionSection, $functions, PREG_SET_ORDER, 0)) {
return [];
}
$functions = $this->arrayFlatten($functions);
foreach ($functions as $function) {
$cleaningFunction = \str_replace(['&false;', '&true;', '&null;'], ['false', 'true', 'null'], $function);
$cleaningFunction = preg_replace('/&(.*);/m', '', $cleaningFunction);
if (!\is_string($cleaningFunction)) {
throw new \RuntimeException('Error occurred in preg_replace');
}
$cleanedFunctions[] = $cleaningFunction;
}
$functionObjects = [];
foreach ($cleanedFunctions as $cleanedFunction) {
$functionObject = \simplexml_load_string($cleanedFunction);
if ($functionObject) {
$functionObjects[] = $functionObject;
}
}
return $functionObjects;
}
/**
* Loads the XML file, resolving all DTD declared entities.
*/
public function loadAndResolveFile(): \SimpleXMLElement
{
$content = \file_get_contents($this->path);
if ($content === false) {
throw new \RuntimeException('An error occurred while reading '.$this->path);
}
$strpos = \strpos($content, '?>')+2;
if (!\file_exists(DocPage::findDocDir() . '/entities/generated.ent')) {
self::buildEntities();
}
$path = \realpath(DocPage::findDocDir() . '/entities/generated.ent');
$content = \substr($content, 0, $strpos)
.'<!DOCTYPE refentry SYSTEM "'.$path.'">'
.\substr($content, $strpos+1);
libxml_use_internal_errors(true);
$elem = \simplexml_load_string($content, \SimpleXMLElement::class, LIBXML_DTDLOAD | LIBXML_NOENT);
if ($elem === false) {
throw new \RuntimeException('Invalid XML file for '.$this->path);
}
$elem->registerXPathNamespace('docbook', 'http://docbook.org/ns/docbook');
return $elem;
}
/**
* Returns the module name in Camelcase.
*/
public function getModule(): string
{
return $this->toCamelCase(\basename(\dirname($this->path, 2)));
}
private function extractSection(string $sectionName, string $file): string
{
$regexpBase = '/<refsect1\s+role="%s">[\s\S]*?<\/refsect1>/m';
$regexpString = sprintf($regexpBase, preg_quote($sectionName, '/'));
preg_match_all($regexpString, $file, $output);
$output = implode('', $this->arrayFlatten((array) $output));
return $output;
}
private function toCamelCase(string $str): string
{
$tokens = preg_split("/[_ ]+/", $str);
if ($tokens === false) {
throw new \RuntimeException('Unexpected preg_split error'); // @codeCoverageIgnore
}
$str = '';
foreach ($tokens as $token) {
$str .= ucfirst($token);
}
return $str;
}
/**
* @param mixed[] $array multidimensional string array
* @return string[]
*/
private function arrayFlatten(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $this->arrayFlatten($value));
} else {
$result[$key] = strval($value);
}
}
return $result;
}
public static function buildEntities(): void
{
$file1 = \file_get_contents(DocPage::findDocDir() . '/doc-en/en/language-defs.ent') ?: '';
$file2 = \file_get_contents(DocPage::findDocDir() . '/doc-en/en/language-snippets.ent') ?: '';
$file3 = \file_get_contents(DocPage::findDocDir() . '/doc-en/en/extensions.ent') ?: '';
$file4 = \file_get_contents(DocPage::findDocDir() . '/doc-en/doc-base/entities/global.ent') ?: '';
$completeFile = $file1 . self::extractXmlHeader($file2) . self::extractXmlHeader($file3) . $file4;
\file_put_contents(DocPage::findDocDir() . '/entities/generated.ent', $completeFile);
}
private static function extractXmlHeader(string $content): string
{
$strpos = strpos($content, '?>')+2;
return substr($content, $strpos);
}
}