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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
<?php
/**
* This script checks the constants defined in the curl PHP extension, against those documented on the cURL website:
* https://curl.haxx.se/libcurl/c/symbols-in-versions.html
*
* See the discussion at: https://github.com/php/php-src/pull/2961
*/
const CURL_DOC_FILE = 'https://curl.haxx.se/libcurl/c/symbols-in-versions.html';
const SOURCE_FILE = __DIR__ . '/interface.c';
const MIN_SUPPORTED_CURL_VERSION = '7.15.5';
const IGNORED_CONSTANTS = [
'CURLOPT_PROGRESSDATA'
];
$curlConstants = getCurlConstants();
$sourceConstants = getSourceConstants();
$notInPHP = []; // In cURL doc, but missing from PHP
$notInCurl = []; // In the PHP source, but not in the cURL doc
$outdated = []; // In the PHP source, but removed before the minimum supported cURL version
foreach ($curlConstants as $name => [$introduced, $deprecated, $removed]) {
$inPHP = in_array($name, $sourceConstants);
if ($removed !== null) {
if (version_compare($removed, MIN_SUPPORTED_CURL_VERSION) <= 0) {
// constant removed before the minimum supported version
continue;
}
}
if (! $inPHP) {
$notInPHP[$name] = [$introduced, $removed];
}
}
foreach ($sourceConstants as $name) {
if (! isset($curlConstants[$name])) {
$notInCurl[] = $name;
continue;
}
$removed = $curlConstants[$name][2];
if ($removed === null) {
continue;
}
if (version_compare($removed, MIN_SUPPORTED_CURL_VERSION) <= 0) {
// constant removed before the minimum supported version
$outdated[$name] = $removed;
}
}
$allGood = true;
if ($notInPHP) {
uasort($notInPHP, function($a, $b) {
return version_compare($a[0], $b[0]);
});
$table = new AsciiTable();
$table->add('Constant', 'Introduced', '', 'Removed', '');
foreach ($notInPHP as $name => [$introduced, $removed]) {
if ($removed === null) {
$removed = '';
$removedHex = '';
} else {
$removedHex = getHexVersion($removed);
}
$table->add($name, $introduced, getHexVersion($introduced), $removed, $removedHex);
}
echo "Constants missing from the PHP source:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($notInCurl) {
$table = new AsciiTable();
foreach ($notInCurl as $name) {
$table->add($name);
}
echo "Constants defined in the PHP source, but absent from the cURL documentation:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($outdated) {
uasort($outdated, function($a, $b) {
return version_compare($a, $b);
});
$table = new AsciiTable();
$table->add('Constant', 'Removed');
foreach ($outdated as $name => $version) {
$table->add($name, $version);
}
echo "Constants defined in the PHP source, but removed before the minimum supported cURL version:\n\n";
echo $table, "\n";
$allGood = false;
}
if ($allGood) {
echo "All good! Source code and cURL documentation are in sync.\n";
}
/**
* Loads and parses the cURL constants from the online documentation.
*
* The result is an associative array where the key is the constant name, and the value is a numeric array with:
* - the introduced version
* - the deprecated version (nullable)
* - the removed version (nullable)
*
* @return array
*/
function getCurlConstants() : array
{
$html = file_get_contents(CURL_DOC_FILE);
// Extract the constant list from the HTML file (located in the only <pre> tag in the page)
preg_match('~<pre>([^<]+)</pre>~', $html, $matches);
$constantList = $matches[1];
/**
* Parse the cURL constant lines. Possible formats:
*
* Name Introduced Deprecated Removed
* CURLOPT_CRLFILE 7.19.0
* CURLOPT_DNS_USE_GLOBAL_CACHE 7.9.3 7.11.1
* CURLOPT_FTPASCII 7.1 7.11.1 7.15.5
* CURLOPT_HTTPREQUEST 7.1 - 7.15.5
*/
$regexp = '/^([A-Za-z0-9_]+) +([0-9\.]+)(?: +([0-9\.\-]+))?(?: +([0-9\.]+))?/m';
preg_match_all($regexp, $constantList, $matches, PREG_SET_ORDER);
$constants = [];
foreach ($matches as $match) {
$name = $match[1];
$introduced = $match[2];
$deprecated = $match[3] ?? null;
$removed = $match[4] ?? null;
if (strpos($name, 'CURLOPT_') !== 0) {
// not a CURLOPT_* constant
continue;
}
if (in_array($name, IGNORED_CONSTANTS)) {
// purposefully ignored constant
continue;
}
if ($deprecated === '-') { // deprecated version can be a hyphen
$deprecated = null;
}
$constants[$name] = [$introduced, $deprecated, $removed];
}
return $constants;
}
/**
* Parses the defined cURL constants from the PHP extension source code.
*
* The result is a numeric array whose values are the constant names.
*
* @return array
*/
function getSourceConstants() : array
{
$source = file_get_contents(SOURCE_FILE);
preg_match_all('/REGISTER_CURL_CONSTANT\(([A-Za-z0-9_]+)\)/', $source, $matches);
$constants = [];
foreach ($matches[1] as $name) {
if ($name === '__c') { // macro
continue;
}
if (strpos($name, 'CURLOPT_') !== 0) {
// not a CURLOPT_* constant
continue;
}
$constants[] = $name;
}
return $constants;
}
/**
* Converts a version number to its hex representation as used in the extension source code.
*
* Example: 7.25.1 => 0x071901
*
* @param string $version
*
* @return string
*
* @throws \RuntimeException
*/
function getHexVersion(string $version) : string
{
$parts = explode('.', $version);
if (count($parts) === 2) {
$parts[] = '0';
}
if (count($parts) !== 3) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$hex = '0x';
foreach ($parts as $value) {
if (! ctype_digit($value) || strlen($value) > 3) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$value = (int) $value;
if ($value > 255) {
throw new \RuntimeException('Invalid version number: ' . $version);
}
$value = dechex($value);
if (strlen($value) === 1) {
$value = '0' . $value;
}
$hex .= $value;
}
return $hex;
}
/**
* A simple helper to create ASCII tables.
* It assumes that the same number of columns is always given to add().
*/
class AsciiTable
{
/**
* @var array
*/
private $values = [];
/**
* @var array
*/
private $length = [];
/**
* @var int
*/
private $padding = 4;
/**
* @param string[] $values
*
* @return void
*/
public function add(string ...$values) : void
{
$this->values[] = $values;
foreach ($values as $key => $value) {
$length = strlen($value);
if (isset($this->length[$key])) {
$this->length[$key] = max($this->length[$key], $length);
} else {
$this->length[$key] = $length;
}
}
}
/**
* @return string
*/
public function __toString() : string
{
$result = '';
foreach ($this->values as $values) {
foreach ($values as $key => $value) {
if ($key !== 0) {
$result .= str_repeat(' ', $this->padding);
}
$result .= str_pad($value, $this->length[$key]);
}
$result .= "\n";
}
return $result;
}
}
|