Skip to content

Commit d55cb7f

Browse files
committed
Add assertion for documented constant values
1 parent 5ac166d commit d55cb7f

File tree

5 files changed

+100
-38
lines changed

5 files changed

+100
-38
lines changed

Zend/zend_constants.stub.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,91 @@
77
* @cname E_ERROR
88
*/
99
const E_ERROR = UNKNOWN;
10+
1011
/**
1112
* @var int
1213
* @cname E_WARNING
1314
*/
1415
const E_WARNING = UNKNOWN;
16+
1517
/**
1618
* @var int
1719
* @cname E_PARSE
1820
*/
1921
const E_PARSE = UNKNOWN;
22+
2023
/**
2124
* @var int
2225
* @cname E_NOTICE
2326
*/
2427
const E_NOTICE = UNKNOWN;
28+
2529
/**
2630
* @var int
2731
* @cname E_CORE_ERROR
2832
*/
2933
const E_CORE_ERROR = UNKNOWN;
34+
3035
/**
3136
* @var int
3237
* @cname E_CORE_WARNING
3338
*/
3439
const E_CORE_WARNING = UNKNOWN;
40+
3541
/**
3642
* @var int
3743
* @cname E_COMPILE_ERROR
3844
*/
3945
const E_COMPILE_ERROR = UNKNOWN;
46+
4047
/**
4148
* @var int
4249
* @cname E_COMPILE_WARNING
4350
*/
4451
const E_COMPILE_WARNING = UNKNOWN;
52+
4553
/**
4654
* @var int
4755
* @cname E_USER_ERROR
4856
*/
4957
const E_USER_ERROR = UNKNOWN;
58+
5059
/**
5160
* @var int
5261
* @cname E_USER_WARNING
5362
*/
5463
const E_USER_WARNING = UNKNOWN;
64+
5565
/**
5666
* @var int
5767
* @cname E_USER_NOTICE
5868
*/
5969
const E_USER_NOTICE = UNKNOWN;
70+
6071
/**
6172
* @var int
6273
* @cname E_STRICT
6374
*/
6475
const E_STRICT = UNKNOWN;
76+
6577
/**
6678
* @var int
6779
* @cname E_RECOVERABLE_ERROR
6880
*/
6981
const E_RECOVERABLE_ERROR = UNKNOWN;
82+
7083
/**
7184
* @var int
7285
* @cname E_DEPRECATED
7386
*/
7487
const E_DEPRECATED = UNKNOWN;
88+
7589
/**
7690
* @var int
7791
* @cname E_USER_DEPRECATED
7892
*/
7993
const E_USER_DEPRECATED = UNKNOWN;
94+
8095
/**
8196
* @var int
8297
* @cname E_ALL

Zend/zend_constants_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: f7d158c81a29e7afdf299a34e9c6b3ed6408e582 */
2+
* Stub hash: f417ea0f3a43036f0abc87856c83f4c93395405a */
33

44

55

build/gen_stub.php

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ protected function getFieldSynopsisValueString(iterable $allConstInfos): ?string
18301830
{
18311831
$value = EvaluatedValue::createFromExpression($this->value, null, $this->cname, $allConstInfos);
18321832
if ($value->originatingConst) {
1833-
return $value->originatingConst->name->__toString();
1833+
return $value->originatingConst->getFieldSynopsisValueString($allConstInfos);
18341834
}
18351835

18361836
return $this->valueString;
@@ -1846,11 +1846,6 @@ public function getDeclaration(iterable $allConstInfos): string
18461846
throw new Exception("Constant " . $this->name->__toString() . " must have a simple built-in type");
18471847
}
18481848

1849-
$value = EvaluatedValue::createFromExpression($this->value, $type, $this->cname, $allConstInfos);
1850-
if (!$value->isUnknownConstValue && !$value->originatingConst) {
1851-
throw new Exception("Constant " . $this->name->__toString() . " must have either UNKNOWN or a constant value");
1852-
}
1853-
18541849
$value = EvaluatedValue::createFromExpression($this->value, $type, $this->cname, $allConstInfos);
18551850
if ($value->isUnknownConstValue && !$value->cConstName) {
18561851
throw new Exception("Constant " . $this->name->__toString() . " must have a @cname annotation");
@@ -1863,10 +1858,11 @@ public function getDeclaration(iterable $allConstInfos): string
18631858
}
18641859

18651860
if ($this->name->isClassConst()) {
1866-
$code .= $this->getClassConstDeclaration($code, $value, $allConstInfos);
1861+
$code .= $this->getClassConstDeclaration($value, $allConstInfos);
18671862
} else {
1868-
$code .= $this->getConstDeclaration($code, $value, $allConstInfos);
1863+
$code .= $this->getGlobalConstDeclaration($value, $allConstInfos);
18691864
}
1865+
$code .= $this->getValueAssertion($value);
18701866

18711867
if ($this->cond) {
18721868
$code .= "#endif\n";
@@ -1878,7 +1874,7 @@ public function getDeclaration(iterable $allConstInfos): string
18781874
/**
18791875
* @param iterable<ConstInfo> $allConstInfos
18801876
*/
1881-
private function getConstDeclaration(string $code, EvaluatedValue $value, iterable $allConstInfos): string
1877+
private function getGlobalConstDeclaration(EvaluatedValue $value, iterable $allConstInfos): string
18821878
{
18831879
$constName = str_replace('\\', '\\\\', $this->name->__toString());
18841880
$constValue = $value->value;
@@ -1888,34 +1884,38 @@ private function getConstDeclaration(string $code, EvaluatedValue $value, iterab
18881884
if ($this->isDeprecated) {
18891885
$flags .= " | CONST_DEPRECATED";
18901886
}
1891-
18921887
if ($value->type->isNull()) {
1893-
$code .= " REGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
1894-
} elseif ($value->type->isBool()) {
1895-
$code .= " REGISTER_BOOL_CONSTANT(\"$constName\", " . ($cConstValue ?: ($constValue ? "true" : "false")) . ", $flags);\n";
1896-
} elseif ($value->type->isInt()) {
1897-
$code .= " REGISTER_LONG_CONSTANT(\"$constName\", " . ($cConstValue ?: (int) $constValue) . ", $flags);\n";
1898-
} elseif ($value->type->isFloat()) {
1899-
$code .= " REGISTER_DOUBLE_CONSTANT(\"$constName\", " . ($cConstValue ?: (int) $constValue) . ", $flags);\n";
1900-
} elseif ($value->type->isString()) {
1901-
$code .= " REGISTER_STRING_CONSTANT(\"$constName\", " . ($cConstValue ?: '"' . addslashes($constValue) . '"') . ", $flags);\n";
1902-
} else {
1903-
throw new Exception("Unimplemented constant type");
1888+
return " REGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
19041889
}
19051890

1906-
return $code;
1907-
}
1891+
if ($value->type->isBool()) {
1892+
return " REGISTER_BOOL_CONSTANT(\"$constName\", " . ($cConstValue ?: ($constValue ? "true" : "false")) . ", $flags);\n";
1893+
}
1894+
1895+
if ($value->type->isInt()) {
1896+
return " REGISTER_LONG_CONSTANT(\"$constName\", " . ($cConstValue ?: (int) $constValue) . ", $flags);\n";
1897+
}
1898+
1899+
if ($value->type->isFloat()) {
1900+
return " REGISTER_DOUBLE_CONSTANT(\"$constName\", " . ($cConstValue ?: (float) $constValue) . ", $flags);\n";
1901+
}
1902+
1903+
if ($value->type->isString()) {
1904+
return " REGISTER_STRING_CONSTANT(\"$constName\", " . ($cConstValue ?: '"' . addslashes($constValue) . '"') . ", $flags);\n";
1905+
}
1906+
1907+
throw new Exception("Unimplemented constant type");}
19081908

19091909
/**
19101910
* @param iterable<ConstInfo> $allConstInfos
19111911
*/
1912-
private function getClassConstDeclaration(string $code, EvaluatedValue $value, iterable $allConstInfos): string
1912+
private function getClassConstDeclaration(EvaluatedValue $value, iterable $allConstInfos): string
19131913
{
19141914
$constName = $this->getVariableLikeName();
19151915

19161916
$zvalCode = $value->initializeZval("const_{$constName}_value", $allConstInfos);
19171917

1918-
$code .= "\n" . $zvalCode;
1918+
$code = "\n" . $zvalCode;
19191919

19201920
$code .= "\tzend_string *const_{$constName}_name = zend_string_init_interned(\"$constName\", sizeof(\"$constName\") - 1, 1);\n";
19211921
$nameCode = "const_{$constName}_name";
@@ -1926,6 +1926,42 @@ private function getClassConstDeclaration(string $code, EvaluatedValue $value, i
19261926
return $code;
19271927
}
19281928

1929+
private function getValueAssertion(EvaluatedValue $value): string
1930+
{
1931+
if ($value->isUnknownConstValue || $value->originatingConst || $value->cConstName === null) {
1932+
return "";
1933+
}
1934+
1935+
$cName = $value->cConstName;
1936+
$constValue = $value->value;
1937+
1938+
if ($value->type->isNull()) {
1939+
return " ZEND_ASSERT($cName == NULL);\n";
1940+
}
1941+
1942+
if ($value->type->isBool()) {
1943+
$cValue = $constValue ? "true" : "false";
1944+
return " ZEND_ASSERT($cName == $cValue);\n";
1945+
}
1946+
1947+
if ($value->type->isInt()) {
1948+
$cValue = (int) $constValue;
1949+
return " ZEND_ASSERT($cName == $cValue);\n";
1950+
}
1951+
1952+
if ($value->type->isFloat()) {
1953+
$cValue = (float) $constValue;
1954+
return " ZEND_ASSERT($cName == $cValue);\n";
1955+
}
1956+
1957+
if ($value->type->isString()) {
1958+
$cValue = '"' . addslashes($constValue) . '"';
1959+
return " ZEND_ASSERT(strcmp($cName, $cValue) == 0);\n";
1960+
}
1961+
1962+
throw new Exception("Unimplemented constant type");
1963+
}
1964+
19291965
protected function getFlagsAsString(): string
19301966
{
19311967
$flags = parent::getFlagsAsString();

ext/date/php_date.stub.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,67 @@
66
* @var string
77
* @cname DATE_FORMAT_RFC3339
88
*/
9-
const DATE_ATOM = UNKNOWN;
9+
const DATE_ATOM = "Y-m-d\\TH:i:sP";
1010

1111
/**
1212
* @var string
1313
* @cname DATE_FORMAT_COOKIE
1414
*/
15-
const DATE_COOKIE = UNKNOWN;
15+
const DATE_COOKIE = "l, d-M-Y H:i:s T";
1616

1717
/**
1818
* @var string
1919
* @cname DATE_FORMAT_ISO8601
2020
*/
21-
const DATE_ISO8601 = UNKNOWN;
21+
const DATE_ISO8601 = "Y-m-d\\TH:i:sO";
2222

2323
/**
2424
* @var string
2525
* @cname DATE_FORMAT_RFC822
2626
*/
27-
const DATE_RFC822 = UNKNOWN;
27+
const DATE_RFC822 = "D, d M y H:i:s O";
2828

2929
/**
3030
* @var string
3131
* @cname DATE_FORMAT_RFC850
3232
*/
33-
const DATE_RFC850 = UNKNOWN;
33+
const DATE_RFC850 = "l, d-M-y H:i:s T";
3434

3535
/**
3636
* @var string
3737
* @cname DATE_FORMAT_RFC1036
3838
*/
39-
const DATE_RFC1036 = UNKNOWN;
39+
const DATE_RFC1036 = "D, d M y H:i:s O";
4040

4141
/**
4242
* @var string
4343
* @cname DATE_FORMAT_RFC1123
4444
*/
45-
const DATE_RFC1123 = UNKNOWN;
45+
const DATE_RFC1123 = "D, d M Y H:i:s O";
4646

4747
/**
4848
* @var string
4949
* @cname DATE_FORMAT_RFC7231
5050
*/
51-
const DATE_RFC7231 = UNKNOWN;
51+
const DATE_RFC7231 = "D, d M Y H:i:s \\G\\M\\T";
5252

5353
/**
5454
* @var string
5555
* @cname DATE_FORMAT_RFC2822
5656
*/
57-
const DATE_RFC2822 = UNKNOWN;
57+
const DATE_RFC2822 = "D, d M Y H:i:s O";
5858

5959
/**
6060
* @var string
6161
* @cname DATE_FORMAT_RFC3339
6262
*/
63-
const DATE_RFC3339 = UNKNOWN;
63+
const DATE_RFC3339 = "Y-m-d\\TH:i:sP";
6464

6565
/**
6666
* @var string
6767
* @cname DATE_FORMAT_RFC3339_EXTENDED
6868
*/
69-
const DATE_RFC3339_EXTENDED = UNKNOWN;
69+
const DATE_RFC3339_EXTENDED = "Y-m-d\\TH:i:s.vP";
7070

7171
/** @var string */
7272
const DATE_RSS = DATE_RFC1123;

ext/date/php_date_arginfo.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 40d98476fcd1d68ad7b121e96804223f8fc987b2 */
2+
* Stub hash: 1a8742a3fa28a5e8f9326c635f0de60217df1a7e */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
@@ -751,16 +751,27 @@ static const zend_function_entry class_DatePeriod_methods[] = {
751751
static void register_php_date_consts(int module_number)
752752
{
753753
REGISTER_STRING_CONSTANT("DATE_ATOM", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
754+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC3339, "Y-m-d\\TH:i:sP") == 0);
754755
REGISTER_STRING_CONSTANT("DATE_COOKIE", DATE_FORMAT_COOKIE, CONST_CS | CONST_PERSISTENT);
756+
ZEND_ASSERT(strcmp(DATE_FORMAT_COOKIE, "l, d-M-Y H:i:s T") == 0);
755757
REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
758+
ZEND_ASSERT(strcmp(DATE_FORMAT_ISO8601, "Y-m-d\\TH:i:sO") == 0);
756759
REGISTER_STRING_CONSTANT("DATE_RFC822", DATE_FORMAT_RFC822, CONST_CS | CONST_PERSISTENT);
760+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC822, "D, d M y H:i:s O") == 0);
757761
REGISTER_STRING_CONSTANT("DATE_RFC850", DATE_FORMAT_RFC850, CONST_CS | CONST_PERSISTENT);
762+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC850, "l, d-M-y H:i:s T") == 0);
758763
REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
764+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC1036, "D, d M y H:i:s O") == 0);
759765
REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
766+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC1123, "D, d M Y H:i:s O") == 0);
760767
REGISTER_STRING_CONSTANT("DATE_RFC7231", DATE_FORMAT_RFC7231, CONST_CS | CONST_PERSISTENT);
768+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC7231, "D, d M Y H:i:s \\G\\M\\T") == 0);
761769
REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
770+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC2822, "D, d M Y H:i:s O") == 0);
762771
REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
772+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC3339, "Y-m-d\\TH:i:sP") == 0);
763773
REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
774+
ZEND_ASSERT(strcmp(DATE_FORMAT_RFC3339_EXTENDED, "Y-m-d\\TH:i:s.vP") == 0);
764775
REGISTER_STRING_CONSTANT("DATE_RSS", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
765776
REGISTER_STRING_CONSTANT("DATE_W3C", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
766777
REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);

0 commit comments

Comments
 (0)