Skip to content

Commit 33b0a4a

Browse files
gen_stub: Infer constants' types from values
Don't require `@var` with a type when the type can be inferred from a literal value in the stub file.
1 parent d6d0a2a commit 33b0a4a

25 files changed

+31
-102
lines changed

build/gen_stub.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4874,7 +4874,24 @@ function parseConstLike(
48744874
}
48754875

48764876
if ($type === null && $phpDocType === null) {
4877-
throw new Exception("Missing type for constant " . $name->__toString());
4877+
if ($const->value instanceof Node\Scalar\Float_) {
4878+
$phpDocType = 'float';
4879+
} elseif ($const->value instanceof Node\Scalar\Int_
4880+
|| ($const->value instanceof Expr\UnaryMinus
4881+
&& $const->value->expr instanceof Node\Scalar\Int_
4882+
)
4883+
) {
4884+
$phpDocType = 'int';
4885+
} elseif ($const->value instanceof Node\Scalar\String_) {
4886+
$phpDocType = 'string';
4887+
} elseif ($const->value instanceof Expr\ConstFetch
4888+
&& $const->value->name instanceof Node\Name\FullyQualified
4889+
&& $const->value->name->name === 'false'
4890+
) {
4891+
$phpDocType = 'bool';
4892+
} else {
4893+
throw new Exception("Missing type for constant " . $name->__toString());
4894+
}
48784895
}
48794896

48804897
$constType = $type ? Type::fromNode($type) : null;

ext/date/php_date.stub.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,62 @@
33
/** @generate-class-entries */
44

55
/**
6-
* @var string
76
* @cvalue DATE_FORMAT_RFC3339
87
*/
98
const DATE_ATOM = "Y-m-d\\TH:i:sP";
109

1110
/**
12-
* @var string
1311
* @cvalue DATE_FORMAT_COOKIE
1412
*/
1513
const DATE_COOKIE = "l, d-M-Y H:i:s T";
1614

1715
/**
18-
* @var string
1916
* @cvalue DATE_FORMAT_ISO8601
2017
*/
2118
const DATE_ISO8601 = "Y-m-d\\TH:i:sO";
2219

2320
/**
24-
* @var string
2521
* @cvalue DATE_FORMAT_ISO8601_EXPANDED
2622
*/
2723
const DATE_ISO8601_EXPANDED = "X-m-d\\TH:i:sP";
2824

2925
/**
30-
* @var string
3126
* @cvalue DATE_FORMAT_RFC822
3227
*/
3328
const DATE_RFC822 = "D, d M y H:i:s O";
3429

3530
/**
36-
* @var string
3731
* @cvalue DATE_FORMAT_RFC850
3832
*/
3933
const DATE_RFC850 = "l, d-M-y H:i:s T";
4034

4135
/**
42-
* @var string
4336
* @cvalue DATE_FORMAT_RFC1036
4437
*/
4538
const DATE_RFC1036 = "D, d M y H:i:s O";
4639

4740
/**
48-
* @var string
4941
* @cvalue DATE_FORMAT_RFC1123
5042
*/
5143
const DATE_RFC1123 = "D, d M Y H:i:s O";
5244

5345
/**
54-
* @var string
5546
* @cvalue DATE_FORMAT_RFC7231
5647
*/
5748
#[\Deprecated(since: '8.5', message: "as this format ignores the associated timezone and always uses GMT")]
5849
const DATE_RFC7231 = "D, d M Y H:i:s \\G\\M\\T";
5950

6051
/**
61-
* @var string
6252
* @cvalue DATE_FORMAT_RFC2822
6353
*/
6454
const DATE_RFC2822 = "D, d M Y H:i:s O";
6555

6656
/**
67-
* @var string
6857
* @cvalue DATE_FORMAT_RFC3339
6958
*/
7059
const DATE_RFC3339 = "Y-m-d\\TH:i:sP";
7160

7261
/**
73-
* @var string
7462
* @cvalue DATE_FORMAT_RFC3339_EXTENDED
7563
*/
7664
const DATE_RFC3339_EXTENDED = "Y-m-d\\TH:i:s.vP";

ext/date/php_date_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/dba/dba.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ final class Connection
1414

1515
namespace {
1616
#ifdef DBA_LMDB
17-
/** @var int */
1817
const DBA_LMDB_USE_SUB_DIR = 0;
1918
/**
2019
* @var int

ext/dba/dba_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/dl_test/dl_test.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @undocumentable
66
*/
77

8-
/** @var int */
98
const DL_TEST_CONST = 42;
109

1110
function dl_test_test1(): void {}

ext/dl_test/dl_test_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/gd/gd.stub.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,11 @@
449449
* /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
450450
* as error, use the values for now...
451451
*/
452-
/** @var int */
453452
const PNG_NO_FILTER = 0x00;
454-
/** @var int */
455453
const PNG_FILTER_NONE = 0x08;
456-
/** @var int */
457454
const PNG_FILTER_SUB = 0x10;
458-
/** @var int */
459455
const PNG_FILTER_UP = 0x20;
460-
/** @var int */
461456
const PNG_FILTER_AVG = 0x40;
462-
/** @var int */
463457
const PNG_FILTER_PAETH = 0x80;
464458
/** @var int */
465459
const PNG_ALL_FILTERS = 0x08 | 0x10 | 0x20 | 0x40 | 0x80;

ext/gd/gd_arginfo.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/mysqli/mysqli.stub.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,6 @@
589589
*/
590590
const MYSQLI_TRANS_COR_NO_RELEASE = UNKNOWN;
591591

592-
/**
593-
* @var bool
594-
*/
595592
#[\Deprecated(since: '8.2', message: 'as it is always false')]
596593
const MYSQLI_IS_MARIADB = false;
597594

0 commit comments

Comments
 (0)