@@ -69,18 +69,20 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
69
69
$ constInfos = $ fileInfo ->getAllConstInfos ();
70
70
$ context ->allConstInfos = array_merge ($ context ->allConstInfos , $ constInfos );
71
71
72
- $ generatedConstCode = false ;
73
72
$ constCode = generateConstCode ($ constInfos , $ stubHash );
74
- if (($ context ->forceRegeneration || $ stubHash !== $ oldStubHash ) && $ constCode !== "" && file_put_contents ($ constFile , $ constCode )) {
73
+ if (($ context ->forceRegeneration || $ stubHash !== $ oldStubHash ) && $ constCode !== "" ) {
74
+ if (file_put_contents ($ constFile , $ constCode ) === false ) {
75
+ throw new Exception ("Failed to write $ constFile " );
76
+ }
77
+
75
78
echo "Saved $ constFile \n" ;
76
- $ generatedConstCode = true ;
77
79
}
78
80
79
81
$ arginfoCode = generateArgInfoCode (
80
82
basename ($ stubFilenameWithoutExtension ),
81
83
$ fileInfo ,
82
84
$ context ->allConstInfos ,
83
- $ generatedConstCode ? basename ($ constFile ) : null ,
85
+ $ constCode ? basename ($ constFile ) : null ,
84
86
$ stubHash
85
87
);
86
88
if (($ context ->forceRegeneration || $ stubHash !== $ oldStubHash ) && file_put_contents ($ arginfoFile , $ arginfoCode )) {
@@ -776,10 +778,25 @@ private function setTypes(?Type $type, ?Type $phpDocType): void
776
778
777
779
interface ConstOrClassConstName {
778
780
public function __toString (): string ;
781
+ public function equals (ConstOrClassConstName $ const ): bool ;
779
782
public function isClassConst (): bool ;
783
+ public function isUnknown (): bool ;
784
+ }
785
+
786
+ abstract class AbstractConstName implements ConstOrClassConstName
787
+ {
788
+ public function equals (ConstOrClassConstName $ const ): bool
789
+ {
790
+ return $ this ->__toString () === $ const ->__toString ();
791
+ }
792
+
793
+ public function isUnknown (): bool
794
+ {
795
+ return strtolower ($ this ->__toString ()) === "unknown " ;
796
+ }
780
797
}
781
798
782
- class ConstName implements ConstOrClassConstName {
799
+ class ConstName extends AbstractConstName {
783
800
/** @var string */
784
801
public $ const ;
785
802
@@ -799,7 +816,7 @@ public function __toString(): string
799
816
}
800
817
}
801
818
802
- class ClassConstName implements ConstOrClassConstName {
819
+ class ClassConstName extends AbstractConstName {
803
820
/** @var Name */
804
821
public $ class ;
805
822
/** @var string */
@@ -1453,14 +1470,14 @@ public static function createFromExpression(Expr $expr, ?string $cConstName, ite
1453
1470
function (Expr $ expr ) use ($ allConstInfos , &$ cConstName , &$ originatingConst , &$ isUnknownConstValue ) {
1454
1471
if ($ expr instanceof Expr \ConstFetch || $ expr instanceof Expr \ClassConstFetch) {
1455
1472
if ($ expr instanceof Expr \ClassConstFetch) {
1456
- $ constName = $ expr ->class -> toString () . " :: " . $ expr ->name ->toString ();
1473
+ $ constName = new ClassConstName ( $ expr ->class , $ expr ->name ->toString () );
1457
1474
} else {
1458
- $ constName = $ expr ->name ->toString ();
1475
+ $ constName = new ConstName ( $ expr ->name ->toString () );
1459
1476
}
1460
1477
1461
- if ($ constName !== " UNKNOWN " ) {
1478
+ if ($ constName-> isUnknown () === false ) {
1462
1479
foreach ($ allConstInfos as $ const ) {
1463
- if ($ const -> name -> __toString () === $ constName ) {
1480
+ if ($ constName -> equals ( $ const -> name ) ) {
1464
1481
if ($ cConstName === null ) {
1465
1482
$ cConstName = $ const ->cname ;
1466
1483
}
@@ -1472,6 +1489,8 @@ function (Expr $expr) use ($allConstInfos, &$cConstName, &$originatingConst, &$i
1472
1489
return self ::createFromExpression ($ const ->value , $ cConstName , $ allConstInfos )->value ;
1473
1490
}
1474
1491
}
1492
+
1493
+ throw new Exception ("Constant " . $ constName ->__toString () . " cannot be found " );
1475
1494
}
1476
1495
1477
1496
if ($ isUnknownConstValue === null ) {
@@ -1530,6 +1549,8 @@ public function initializeZval(string $zvalName): string
1530
1549
throw new Exception ("Unknown const value " );
1531
1550
}
1532
1551
1552
+ $ cConstValue = $ this ->getCConstValue ();
1553
+
1533
1554
$ code = "\tzval $ zvalName; \n" ;
1534
1555
1535
1556
switch ($ this ->type ) {
@@ -1538,29 +1559,29 @@ public function initializeZval(string $zvalName): string
1538
1559
break ;
1539
1560
1540
1561
case "boolean " :
1541
- $ code .= "\tZVAL_BOOL(& $ zvalName, " . ($ this -> cConstName ?: ($ this ->value ? "true " : "false " )) . "); \n" ;
1562
+ $ code .= "\tZVAL_BOOL(& $ zvalName, " . ($ cConstValue ?: ($ this ->value ? "true " : "false " )) . "); \n" ;
1542
1563
break ;
1543
1564
1544
1565
case "integer " :
1545
- $ code .= "\tZVAL_LONG(& $ zvalName, " . ($ this -> cConstName ?: $ this ->value ) . "); \n" ;
1566
+ $ code .= "\tZVAL_LONG(& $ zvalName, " . ($ cConstValue ?: $ this ->value ) . "); \n" ;
1546
1567
break ;
1547
1568
1548
1569
case "double " :
1549
- $ code .= "\tZVAL_DOUBLE(& $ zvalName, " . ($ this -> cConstName ?: $ this ->value ) . "); \n" ;
1570
+ $ code .= "\tZVAL_DOUBLE(& $ zvalName, " . ($ cConstValue ?: $ this ->value ) . "); \n" ;
1550
1571
break ;
1551
1572
1552
1573
case "string " :
1553
1574
if (!$ this ->cConstName && $ this ->value === "" ) {
1554
1575
$ code .= "\tZVAL_EMPTY_STRING(& $ zvalName); \n" ;
1555
1576
} else {
1556
- $ constValue = $ this -> cConstName ?: '" ' . addslashes ($ this ->value ) . '" ' ;
1577
+ $ constValue = $ cConstValue ?: '" ' . addslashes ($ this ->value ) . '" ' ;
1557
1578
$ code .= "\tzend_string * {$ zvalName }_str = zend_string_init( $ constValue, sizeof( $ constValue) - 1, 1); \n" ;
1558
1579
$ code .= "\tZVAL_STR(& $ zvalName, {$ zvalName }_str); \n" ;
1559
1580
}
1560
1581
break ;
1561
1582
1562
1583
case "array " :
1563
- if (!$ this -> cConstName && empty ($ this ->value )) {
1584
+ if (!$ cConstValue && empty ($ this ->value )) {
1564
1585
$ code .= "\tZVAL_EMPTY_ARRAY(& $ zvalName); \n" ;
1565
1586
} else {
1566
1587
throw new Exception ("Unimplemented default value " );
@@ -1573,6 +1594,19 @@ public function initializeZval(string $zvalName): string
1573
1594
1574
1595
return $ code ;
1575
1596
}
1597
+
1598
+ public function getCConstValue (): ?string
1599
+ {
1600
+ if ($ this ->cConstName ) {
1601
+ return $ this ->cConstName ;
1602
+ }
1603
+
1604
+ if ($ this ->originatingConst ) {
1605
+ return $ this ->originatingConst ->cname ;
1606
+ }
1607
+
1608
+ return null ;
1609
+ }
1576
1610
}
1577
1611
1578
1612
abstract class VariableLike
@@ -1594,14 +1628,14 @@ abstract protected function getVariableLikeName(): string;
1594
1628
1595
1629
abstract protected function addTypeToFieldSynopsis (DOMDocument $ doc , DOMElement $ fieldsynopsisElement ): void ;
1596
1630
1597
- abstract protected function getFieldsynopsisDefaultLinkend (): string ;
1631
+ abstract protected function getFieldSynopsisDefaultLinkend (): string ;
1598
1632
1599
- abstract protected function getFieldsynopsisName (): string ;
1633
+ abstract protected function getFieldSynopsisName (): string ;
1600
1634
1601
1635
/**
1602
1636
* @param iterable<ConstInfo> $allConstInfos
1603
1637
*/
1604
- abstract protected function getFieldsynopsisValueString (iterable $ allConstInfos ): ?string ;
1638
+ abstract protected function getFieldSynopsisValueString (iterable $ allConstInfos ): ?string ;
1605
1639
1606
1640
protected function getFlagsAsString (): string
1607
1641
{
@@ -1626,17 +1660,17 @@ public function getFieldSynopsisElement(DOMDocument $doc, iterable $allConstInfo
1626
1660
1627
1661
$ this ->addTypeToFieldSynopsis ($ doc , $ fieldsynopsisElement );
1628
1662
1629
- $ varnameElement = $ doc ->createElement ("varname " , $ this ->getFieldsynopsisName ());
1663
+ $ varnameElement = $ doc ->createElement ("varname " , $ this ->getFieldSynopsisName ());
1630
1664
if ($ this ->link ) {
1631
1665
$ varnameElement ->setAttribute ("linkend " , $ this ->link );
1632
1666
} else {
1633
- $ varnameElement ->setAttribute ("linkend " , $ this ->getFieldsynopsisDefaultLinkend ());
1667
+ $ varnameElement ->setAttribute ("linkend " , $ this ->getFieldSynopsisDefaultLinkend ());
1634
1668
}
1635
1669
1636
1670
$ fieldsynopsisElement ->appendChild (new DOMText ("\n " ));
1637
1671
$ fieldsynopsisElement ->appendChild ($ varnameElement );
1638
1672
1639
- $ valueString = $ this ->getFieldsynopsisValueString ($ allConstInfos );
1673
+ $ valueString = $ this ->getFieldSynopsisValueString ($ allConstInfos );
1640
1674
if ($ valueString ) {
1641
1675
$ fieldsynopsisElement ->appendChild (new DOMText ("\n " ));
1642
1676
$ initializerElement = $ doc ->createElement ("initializer " , $ valueString );
@@ -1715,14 +1749,14 @@ protected function getVariableTypeCode(): string
1715
1749
return "const " ;
1716
1750
}
1717
1751
1718
- protected function getFieldsynopsisDefaultLinkend (): string
1752
+ protected function getFieldSynopsisDefaultLinkend (): string
1719
1753
{
1720
1754
$ className = str_replace (["\\" , "_ " ], ["- " , "- " ], $ this ->name ->class ->toLowerString ());
1721
1755
1722
1756
return "$ className.constants. " . strtolower (str_replace ("_ " , "- " , $ this ->name ->const ));
1723
1757
}
1724
1758
1725
- protected function getFieldsynopsisName (): string
1759
+ protected function getFieldSynopsisName (): string
1726
1760
{
1727
1761
return $ this ->name ->__toString ();
1728
1762
}
@@ -1738,7 +1772,7 @@ protected function addTypeToFieldSynopsis(DOMDocument $doc, DOMElement $fieldsyn
1738
1772
/**
1739
1773
* @param iterable<ConstInfo> $allConstInfos
1740
1774
*/
1741
- protected function getFieldsynopsisValueString (iterable $ allConstInfos ): ?string
1775
+ protected function getFieldSynopsisValueString (iterable $ allConstInfos ): ?string
1742
1776
{
1743
1777
$ value = EvaluatedValue::createFromExpression ($ this ->value , $ this ->cname , $ allConstInfos );
1744
1778
if ($ value ->isUnknownConstValue ) {
@@ -1825,9 +1859,9 @@ public function getDeclaration(iterable $allConstInfos): string
1825
1859
1826
1860
private function getConstDeclaration (string $ code , EvaluatedValue $ value ): string
1827
1861
{
1828
- $ cConstName = $ value ->cConstName ;
1829
1862
$ constName = str_replace ('\\' , '\\\\' , $ this ->name ->__toString ());
1830
1863
$ constValue = $ value ->value ;
1864
+ $ cConstValue = $ value ->getCConstValue ();
1831
1865
1832
1866
$ flags = "CONST_CS | CONST_PERSISTENT " ;
1833
1867
if ($ this ->isDeprecated ) {
@@ -1839,16 +1873,16 @@ private function getConstDeclaration(string $code, EvaluatedValue $value): strin
1839
1873
$ code .= " REGISTER_NULL_CONSTANT( \"$ constName \", $ flags); \n" ;
1840
1874
break ;
1841
1875
case "boolean " :
1842
- $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstName ?: ($ constValue ? "true " : "false " )) . ", $ flags); \n" ;
1876
+ $ code .= " REGISTER_BOOL_CONSTANT( \"$ constName \", " . ($ cConstValue ?: ($ constValue ? "true " : "false " )) . ", $ flags); \n" ;
1843
1877
break ;
1844
1878
case "integer " :
1845
- $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags); \n" ;
1879
+ $ code .= " REGISTER_LONG_CONSTANT( \"$ constName \", " . ($ cConstValue ?: (int ) $ constValue ) . ", $ flags); \n" ;
1846
1880
break ;
1847
1881
case "double " :
1848
- $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstName ?: (int ) $ constValue ) . ", $ flags); \n" ;
1882
+ $ code .= " REGISTER_DOUBLE_CONSTANT( \"$ constName \", " . ($ cConstValue ?: (int ) $ constValue ) . ", $ flags); \n" ;
1849
1883
break ;
1850
1884
case "string " :
1851
- $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstName ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", $ flags); \n" ;
1885
+ $ code .= " REGISTER_STRING_CONSTANT( \"$ constName \", " . ($ cConstValue ?: '" ' . addslashes ($ constValue ) . '" ' ) . ", $ flags); \n" ;
1852
1886
break ;
1853
1887
default :
1854
1888
throw new Exception ("Unimplemented constant type " );
@@ -1951,22 +1985,22 @@ protected function getVariableLikeName(): string
1951
1985
return $ this ->name ->property ;
1952
1986
}
1953
1987
1954
- protected function getFieldsynopsisDefaultLinkend (): string
1988
+ protected function getFieldSynopsisDefaultLinkend (): string
1955
1989
{
1956
1990
$ className = str_replace (["\\" , "_ " ], ["- " , "- " ], $ this ->name ->class ->toLowerString ());
1957
1991
1958
1992
return "$ className.props. " . strtolower (str_replace ("_ " , "- " , $ this ->name ->property ));
1959
1993
}
1960
1994
1961
- protected function getFieldsynopsisName (): string
1995
+ protected function getFieldSynopsisName (): string
1962
1996
{
1963
- return $ this ->name ->__toString () ;
1997
+ return $ this ->name ->property ;
1964
1998
}
1965
1999
1966
2000
/**
1967
2001
* @param iterable<ConstInfo> $allConstInfos
1968
2002
*/
1969
- protected function getFieldsynopsisValueString (iterable $ allConstInfos ): ?string
2003
+ protected function getFieldSynopsisValueString (iterable $ allConstInfos ): ?string
1970
2004
{
1971
2005
return $ this ->defaultValueString ;
1972
2006
}
@@ -2085,9 +2119,11 @@ protected function addModifiersToFieldSynopsis(DOMDocument $doc, DOMElement $fie
2085
2119
2086
2120
protected function addTypeToFieldSynopsis (DOMDocument $ doc , DOMElement $ fieldsynopsisElement ): void
2087
2121
{
2088
- if ($ this ->type ) {
2122
+ $ type = $ this ->phpDocType ?? $ this ->type ;
2123
+
2124
+ if ($ type ) {
2089
2125
$ fieldsynopsisElement ->appendChild (new DOMText ("\n " ));
2090
- $ fieldsynopsisElement ->appendChild ($ this -> type ->getTypeForDoc ($ doc ));
2126
+ $ fieldsynopsisElement ->appendChild ($ type ->getTypeForDoc ($ doc ));
2091
2127
}
2092
2128
}
2093
2129
@@ -3298,7 +3334,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
3298
3334
3299
3335
$ fileInfo ->classInfos [] = parseClass (
3300
3336
$ className , $ stmt , $ constInfos , $ propertyInfos , $ methodInfos , $ enumCaseInfos
3301
- );
3337
+ );
3302
3338
continue ;
3303
3339
}
3304
3340
@@ -3459,25 +3495,26 @@ function findEquivalentFuncInfo(array $generatedFuncInfos, FuncInfo $funcInfo):
3459
3495
return null ;
3460
3496
}
3461
3497
3462
- /** @param iterable<FuncInfo> $funcInfos */
3498
+ /** @param iterable $infos */
3463
3499
function generateCodeWithConditions (
3464
- iterable $ funcInfos , string $ separator , Closure $ codeGenerator ): string {
3500
+ iterable $ infos , string $ separator , Closure $ codeGenerator ): string {
3465
3501
$ code = "" ;
3466
- foreach ($ funcInfos as $ funcInfo ) {
3467
- $ funcCode = $ codeGenerator ($ funcInfo );
3468
- if ($ funcCode === null ) {
3502
+ foreach ($ infos as $ info ) {
3503
+ $ infoCode = $ codeGenerator ($ info );
3504
+ if ($ infoCode === null ) {
3469
3505
continue ;
3470
3506
}
3471
3507
3472
3508
$ code .= $ separator ;
3473
- if ($ funcInfo ->cond ) {
3474
- $ code .= "#if {$ funcInfo ->cond }\n" ;
3475
- $ code .= $ funcCode ;
3509
+ if ($ info ->cond ) {
3510
+ $ code .= "#if {$ info ->cond }\n" ;
3511
+ $ code .= $ infoCode ;
3476
3512
$ code .= "#endif \n" ;
3477
3513
} else {
3478
- $ code .= $ funcCode ;
3514
+ $ code .= $ infoCode ;
3479
3515
}
3480
3516
}
3517
+
3481
3518
return $ code ;
3482
3519
}
3483
3520
0 commit comments