Skip to content

Commit 444cb5d

Browse files
committed
Improve gen_stub.php
1 parent f695ad3 commit 444cb5d

File tree

5 files changed

+85
-30
lines changed

5 files changed

+85
-30
lines changed

build/gen_stub.php

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
5555
initPhpParser();
5656
$fileInfo = parseStubFile($stubCode);
5757
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
58-
if (($context->forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($arginfoFile, $arginfoCode)) {
59-
echo "Saved $arginfoFile\n";
58+
if ($context->forceRegeneration || $stubHash !== $oldStubHash) {
59+
$context->generatedArginfoFiles[$arginfoFile] = $arginfoCode;
6060
}
6161

6262
if ($fileInfo->generateLegacyArginfo) {
@@ -65,7 +65,7 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo {
6565
}
6666
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
6767
if (($context->forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($legacyFile, $arginfoCode)) {
68-
echo "Saved $legacyFile\n";
68+
$context->generatedArginfoFiles[$legacyFile] = $arginfoCode;
6969
}
7070
}
7171

@@ -98,6 +98,8 @@ class Context {
9898
public $forceParse = false;
9999
/** @var bool */
100100
public $forceRegeneration = false;
101+
/** @var array */
102+
public $generatedArginfoFiles = [];
101103
}
102104

103105
class SimpleType {
@@ -477,13 +479,17 @@ class FuncInfo {
477479
/** @var FunctionOrMethodName */
478480
public $name;
479481
/** @var int */
482+
public $classFlags;
483+
/** @var int */
480484
public $flags;
481485
/** @var string|null */
482486
public $aliasType;
483487
/** @var FunctionName|null */
484488
public $alias;
485489
/** @var bool */
486490
public $isDeprecated;
491+
/** @var bool */
492+
public $verify;
487493
/** @var ArgInfo[] */
488494
public $args;
489495
/** @var ReturnInfo */
@@ -495,29 +501,43 @@ class FuncInfo {
495501

496502
public function __construct(
497503
FunctionOrMethodName $name,
504+
int $classFlags,
498505
int $flags,
499506
?string $aliasType,
500507
?FunctionOrMethodName $alias,
501508
bool $isDeprecated,
509+
bool $verify,
502510
array $args,
503511
ReturnInfo $return,
504512
int $numRequiredArgs,
505513
?string $cond
506514
) {
507515
$this->name = $name;
516+
$this->classFlags = $classFlags;
508517
$this->flags = $flags;
509518
$this->aliasType = $aliasType;
510519
$this->alias = $alias;
511520
$this->isDeprecated = $isDeprecated;
521+
$this->verify = $verify;
512522
$this->args = $args;
513523
$this->return = $return;
514524
$this->numRequiredArgs = $numRequiredArgs;
515525
$this->cond = $cond;
516526
}
517527

528+
public function isMethod(): bool
529+
{
530+
return $this->name->isMethod();
531+
}
532+
533+
public function isFinalMethod(): bool
534+
{
535+
return $this->flags & Class_::MODIFIER_FINAL || $this->classFlags & Class_::MODIFIER_FINAL;
536+
}
537+
518538
public function isInstanceMethod(): bool
519539
{
520-
return !($this->flags & Class_::MODIFIER_STATIC) && $this->name->isMethod() && $this->name->isConstructor() === false;
540+
return !($this->flags & Class_::MODIFIER_STATIC) && $this->isMethod() && $this->name->isConstructor() === false;
521541
}
522542

523543
public function equalsApartFromName(FuncInfo $other): bool {
@@ -757,6 +777,7 @@ function parseDocComment(DocComment $comment): array {
757777
function parseFunctionLike(
758778
PrettyPrinterAbstract $prettyPrinter,
759779
FunctionOrMethodName $name,
780+
int $classFlags,
760781
int $flags,
761782
Node\FunctionLike $func,
762783
?string $cond
@@ -766,6 +787,7 @@ function parseFunctionLike(
766787
$aliasType = null;
767788
$alias = null;
768789
$isDeprecated = false;
790+
$noVerify = false;
769791
$haveDocReturnType = false;
770792
$docParamTypes = [];
771793

@@ -778,7 +800,7 @@ function parseFunctionLike(
778800
$paramMeta[$varName] = [];
779801
}
780802
$paramMeta[$varName]['preferRef'] = true;
781-
} else if ($tag->name === 'alias' || $tag->name === 'implementation-alias' || $tag->name === 'static-method-alias') {
803+
} else if ($tag->name === 'alias' || $tag->name === 'implementation-alias') {
782804
$aliasType = $tag->name;
783805
$aliasParts = explode("::", $tag->getValue());
784806
if (count($aliasParts) === 1) {
@@ -788,6 +810,8 @@ function parseFunctionLike(
788810
}
789811
} else if ($tag->name === 'deprecated') {
790812
$isDeprecated = true;
813+
} else if ($tag->name === 'no-verify') {
814+
$noVerify = true;
791815
} else if ($tag->name === 'return') {
792816
$haveDocReturnType = true;
793817
} else if ($tag->name === 'param') {
@@ -868,10 +892,12 @@ function parseFunctionLike(
868892

869893
return new FuncInfo(
870894
$name,
895+
$classFlags,
871896
$flags,
872897
$aliasType,
873898
$alias,
874899
$isDeprecated,
900+
!$noVerify,
875901
$args,
876902
$return,
877903
$numRequiredArgs,
@@ -942,6 +968,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
942968
$prettyPrinter,
943969
new FunctionName($stmt->namespacedName),
944970
0,
971+
0,
945972
$stmt,
946973
$cond
947974
);
@@ -961,6 +988,11 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
961988
throw new Exception("Not implemented {$classStmt->getType()}");
962989
}
963990

991+
$classFlags = 0;
992+
if ($stmt instanceof Class_) {
993+
$classFlags = $stmt->flags;
994+
}
995+
964996
$flags = $classStmt->flags;
965997
if ($stmt instanceof Stmt\Interface_) {
966998
$flags |= Class_::MODIFIER_ABSTRACT;
@@ -973,6 +1005,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
9731005
$methodInfos[] = parseFunctionLike(
9741006
$prettyPrinter,
9751007
new MethodName($className, $classStmt->name->toString()),
1008+
$classFlags,
9761009
$flags,
9771010
$classStmt,
9781011
$cond
@@ -1314,24 +1347,6 @@ function initPhpParser() {
13141347
exit(1);
13151348
}
13161349

1317-
if ($printParameterStats) {
1318-
$parameterStats = [];
1319-
1320-
foreach ($fileInfos as $fileInfo) {
1321-
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
1322-
foreach ($funcInfo->args as $argInfo) {
1323-
if (!isset($context->parameterStats[$argInfo->name])) {
1324-
$parameterStats[$argInfo->name] = 0;
1325-
}
1326-
$parameterStats[$argInfo->name]++;
1327-
}
1328-
}
1329-
}
1330-
1331-
arsort($parameterStats);
1332-
echo json_encode($parameterStats, JSON_PRETTY_PRINT), "\n";
1333-
}
1334-
13351350
if ($verify) {
13361351
$errors = [];
13371352
$funcMap = [];
@@ -1342,7 +1357,7 @@ function initPhpParser() {
13421357
/** @var FuncInfo $funcInfo */
13431358
$funcMap[$funcInfo->name->__toString()] = $funcInfo;
13441359

1345-
if ($funcInfo->aliasType === "alias" || $funcInfo->aliasType === "static-method-alias") {
1360+
if ($funcInfo->aliasType === "alias") {
13461361
$aliases[] = $funcInfo;
13471362
}
13481363
}
@@ -1354,11 +1369,15 @@ function initPhpParser() {
13541369
continue;
13551370
}
13561371

1372+
if ($aliasFunc->verify === false) {
1373+
continue;
1374+
}
1375+
13571376
$aliasedFunc = $funcMap[$aliasFunc->alias->__toString()];
13581377
$aliasedArgs = $aliasedFunc->args;
13591378
$aliasArgs = $aliasFunc->args;
13601379

1361-
if ($aliasFunc->isInstanceMethod() !== $aliasedFunc->isInstanceMethod() && $aliasFunc->aliasType !== "static-method-alias") {
1380+
if ($aliasFunc->isInstanceMethod() !== $aliasedFunc->isInstanceMethod()) {
13621381
if ($aliasFunc->isInstanceMethod()) {
13631382
$aliasedArgs = array_slice($aliasedArgs, 1);
13641383
}
@@ -1397,6 +1416,13 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
13971416
},
13981417
$aliasArgs, $aliasedArgs
13991418
);
1419+
1420+
if ((!$aliasedFunc->isMethod() || $aliasedFunc->isFinalMethod()) &&
1421+
(!$aliasFunc->isMethod() || $aliasFunc->isFinalMethod()) &&
1422+
$aliasFunc->return != $aliasedFunc->return
1423+
) {
1424+
$errors[] = "{$aliasFunc->name}() and {$aliasedFunc->name}() must have the same return type";
1425+
}
14001426
}
14011427

14021428
echo implode("\n", $errors);
@@ -1405,3 +1431,29 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
14051431
exit(1);
14061432
}
14071433
}
1434+
1435+
foreach ($context->generatedArginfoFiles as $arginfoFile => $arginfoCode) {
1436+
if (file_put_contents($arginfoFile, $arginfoCode)) {
1437+
echo "Saved $arginfoFile\n";
1438+
} else {
1439+
echo "Saving $arginfoFile was unsuccessful\n";
1440+
}
1441+
}
1442+
1443+
if ($printParameterStats) {
1444+
$parameterStats = [];
1445+
1446+
foreach ($fileInfos as $fileInfo) {
1447+
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
1448+
foreach ($funcInfo->args as $argInfo) {
1449+
if (!isset($context->parameterStats[$argInfo->name])) {
1450+
$parameterStats[$argInfo->name] = 0;
1451+
}
1452+
$parameterStats[$argInfo->name]++;
1453+
}
1454+
}
1455+
}
1456+
1457+
arsort($parameterStats);
1458+
echo json_encode($parameterStats, JSON_PRETTY_PRINT), "\n";
1459+
}

ext/mysqli/mysqli.stub.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function commit(int $flags = -1, ?string $name = null) {}
5555

5656
/**
5757
* @return mysqli|null|false
58-
* @static-method-alias mysqli_connect
58+
* @alias mysqli_connect
59+
* @no-verify
5960
*/
6061
public function connect(
6162
?string $hostname = null,

ext/mysqli/mysqli_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: b156847f2617389de5381e5bd5e2fedb27bd7eed */
2+
* Stub hash: a08c78e8f2cd934224679f34b1556c170c97737a */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
55
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)

ext/xmlwriter/php_xmlwriter.stub.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ class XMLWriter
9090
{
9191
/**
9292
* @return bool
93-
* @static-method-alias xmlwriter_open_uri
93+
* @alias xmlwriter_open_uri
94+
* @no-verify
9495
*/
9596
public function openUri(string $uri) {}
9697

9798
/**
9899
* @return bool
99-
* @static-method-alias xmlwriter_open_memory
100+
* @alias xmlwriter_open_memory
101+
* @no-verify
100102
*/
101103
public function openMemory() {}
102104

ext/xmlwriter/php_xmlwriter_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: 8607fe498af4c678f099231d52c3c8aabf5b7e9e */
2+
* Stub hash: 8c40633407c5bf49e47504ae93c2031af3b7e718 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_xmlwriter_open_uri, 0, 1, XMLWriter, MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 0)

0 commit comments

Comments
 (0)