Skip to content

Commit c90c944

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Better document constructors Do not display non-existent constructors Do not list private constructors as inherited
2 parents 0aa1fdf + 7b08fe9 commit c90c944

File tree

1 file changed

+86
-54
lines changed

1 file changed

+86
-54
lines changed

build/gen_stub.php

+86-54
Original file line numberDiff line numberDiff line change
@@ -2700,17 +2700,18 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap, itera
27002700
}
27012701
$classSynopsisInfo->appendChild(new DOMText("\n "));
27022702

2703-
/** @var Name[] $parentsWithInheritedConstants */
2703+
/** @var array<string, Name> $parentsWithInheritedConstants */
27042704
$parentsWithInheritedConstants = [];
2705-
/** @var Name[] $parentsWithInheritedProperties */
2705+
/** @var array<string, Name> $parentsWithInheritedProperties */
27062706
$parentsWithInheritedProperties = [];
2707-
/** @var Name[] $parentsWithInheritedMethods */
2707+
/** @var array<int, array{name: Name, types: int[]}> $parentsWithInheritedMethods */
27082708
$parentsWithInheritedMethods = [];
27092709

27102710
$this->collectInheritedMembers(
27112711
$parentsWithInheritedConstants,
27122712
$parentsWithInheritedProperties,
27132713
$parentsWithInheritedMethods,
2714+
$this->hasConstructor(),
27142715
$classMap
27152716
);
27162717

@@ -2756,43 +2757,41 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap, itera
27562757
"&InheritedProperties;"
27572758
);
27582759

2759-
$isConcreteClass = ($this->type === "class" && !($this->flags & Class_::MODIFIER_ABSTRACT));
2760-
2761-
if ($isConcreteClass || !empty($this->funcInfos)) {
2760+
if (!empty($this->funcInfos)) {
27622761
$classSynopsis->appendChild(new DOMText("\n\n "));
27632762
$classSynopsisInfo = $doc->createElement("classsynopsisinfo", "&Methods;");
27642763
$classSynopsisInfo->setAttribute("role", "comment");
27652764
$classSynopsis->appendChild($classSynopsisInfo);
2766-
}
27672765

2768-
$classReference = self::getClassSynopsisReference($this->name);
2769-
$escapedName = addslashes($this->name->__toString());
2766+
$classReference = self::getClassSynopsisReference($this->name);
2767+
$escapedName = addslashes($this->name->__toString());
27702768

2771-
if ($isConcreteClass || $this->hasConstructor()) {
2772-
$classSynopsis->appendChild(new DOMText("\n "));
2773-
$includeElement = $this->createIncludeElement(
2774-
$doc,
2775-
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[@role='$escapedName'])"
2776-
);
2777-
$classSynopsis->appendChild($includeElement);
2778-
}
2769+
if ($this->hasConstructor()) {
2770+
$classSynopsis->appendChild(new DOMText("\n "));
2771+
$includeElement = $this->createIncludeElement(
2772+
$doc,
2773+
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[@role='$escapedName'])"
2774+
);
2775+
$classSynopsis->appendChild($includeElement);
2776+
}
27792777

2780-
if ($this->hasMethods()) {
2781-
$classSynopsis->appendChild(new DOMText("\n "));
2782-
$includeElement = $this->createIncludeElement(
2783-
$doc,
2784-
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[@role='$escapedName'])"
2785-
);
2786-
$classSynopsis->appendChild($includeElement);
2787-
}
2778+
if ($this->hasMethods()) {
2779+
$classSynopsis->appendChild(new DOMText("\n "));
2780+
$includeElement = $this->createIncludeElement(
2781+
$doc,
2782+
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[@role='$escapedName'])"
2783+
);
2784+
$classSynopsis->appendChild($includeElement);
2785+
}
27882786

2789-
if ($this->hasDestructor()) {
2790-
$classSynopsis->appendChild(new DOMText("\n "));
2791-
$includeElement = $this->createIncludeElement(
2792-
$doc,
2793-
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:destructorsynopsis[@role='$escapedName'])"
2794-
);
2795-
$classSynopsis->appendChild($includeElement);
2787+
if ($this->hasDestructor()) {
2788+
$classSynopsis->appendChild(new DOMText("\n "));
2789+
$includeElement = $this->createIncludeElement(
2790+
$doc,
2791+
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:destructorsynopsis[@role='$escapedName'])"
2792+
);
2793+
$classSynopsis->appendChild($includeElement);
2794+
}
27962795
}
27972796

27982797
if (!empty($parentsWithInheritedMethods)) {
@@ -2802,14 +2801,21 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap, itera
28022801
$classSynopsis->appendChild($classSynopsisInfo);
28032802

28042803
foreach ($parentsWithInheritedMethods as $parent) {
2805-
$classSynopsis->appendChild(new DOMText("\n "));
2806-
$parentReference = self::getClassSynopsisReference($parent);
2807-
$escapedParentName = addslashes($parent->__toString());
2808-
$includeElement = $this->createIncludeElement(
2809-
$doc,
2810-
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$parentReference')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[@role='$escapedParentName'])"
2811-
);
2812-
$classSynopsis->appendChild($includeElement);
2804+
$parentName = $parent["name"];
2805+
$parentMethodsynopsisTypes = $parent["types"];
2806+
2807+
$parentReference = self::getClassSynopsisReference($parentName);
2808+
$escapedParentName = addslashes($parentName->__toString());
2809+
2810+
foreach ($parentMethodsynopsisTypes as $parentMethodsynopsisType) {
2811+
$classSynopsis->appendChild(new DOMText("\n "));
2812+
$includeElement = $this->createIncludeElement(
2813+
$doc,
2814+
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$parentReference')/db:refentry/db:refsect1[@role='description']/descendant::db:{$parentMethodsynopsisType}[@role='$escapedParentName'])"
2815+
);
2816+
2817+
$classSynopsis->appendChild($includeElement);
2818+
}
28132819
}
28142820
}
28152821

@@ -2867,39 +2873,54 @@ public static function getClassSynopsisReference(Name $name): string {
28672873
}
28682874

28692875
/**
2870-
* @param Name[] $parentsWithInheritedConstants
2871-
* @param Name[] $parentsWithInheritedProperties
2872-
* @param Name[] $parentsWithInheritedMethods
2876+
* @param array<string, Name> $parentsWithInheritedConstants
2877+
* @param array<string, Name> $parentsWithInheritedProperties
2878+
* @param array<string, array{name: Name, types: int[]}> $parentsWithInheritedMethods
28732879
* @param array<string, ClassInfo> $classMap
28742880
*/
28752881
private function collectInheritedMembers(
28762882
array &$parentsWithInheritedConstants,
28772883
array &$parentsWithInheritedProperties,
28782884
array &$parentsWithInheritedMethods,
2885+
bool $hasConstructor,
28792886
array $classMap
28802887
): void {
28812888
foreach ($this->extends as $parent) {
28822889
$parentInfo = $classMap[$parent->toString()] ?? null;
2890+
$parentName = $parent->toString();
2891+
28832892
if (!$parentInfo) {
2884-
throw new Exception("Missing parent class " . $parent->toString());
2893+
throw new Exception("Missing parent class $parentName");
28852894
}
28862895

2887-
if (!empty($parentInfo->constInfos) && !isset($parentsWithInheritedConstants[$parent->toString()])) {
2888-
$parentsWithInheritedConstants[$parent->toString()] = $parent;
2896+
if (!empty($parentInfo->constInfos) && !isset($parentsWithInheritedConstants[$parentName])) {
2897+
$parentsWithInheritedConstants[] = $parent;
2898+
}
2899+
2900+
if (!empty($parentInfo->propertyInfos) && !isset($parentsWithInheritedProperties[$parentName])) {
2901+
$parentsWithInheritedProperties[$parentName] = $parent;
28892902
}
28902903

2891-
if (!empty($parentInfo->propertyInfos) && !isset($parentsWithInheritedProperties[$parent->toString()])) {
2892-
$parentsWithInheritedProperties[$parent->toString()] = $parent;
2904+
if (!$hasConstructor && $parentInfo->hasNonPrivateConstructor()) {
2905+
$parentsWithInheritedMethods[$parentName]["name"] = $parent;
2906+
$parentsWithInheritedMethods[$parentName]["types"][] = "constructorsynopsis";
28932907
}
28942908

2895-
if (!isset($parentsWithInheritedMethods[$parent->toString()]) && $parentInfo->hasMethods()) {
2896-
$parentsWithInheritedMethods[$parent->toString()] = $parent;
2909+
if ($parentInfo->hasMethods()) {
2910+
$parentsWithInheritedMethods[$parentName]["name"] = $parent;
2911+
$parentsWithInheritedMethods[$parentName]["types"][] = "methodsynopsis";
2912+
}
2913+
2914+
if ($parentInfo->hasDestructor()) {
2915+
$parentsWithInheritedMethods[$parentName]["name"] = $parent;
2916+
$parentsWithInheritedMethods[$parentName]["types"][] = "destructorsynopsis";
28972917
}
28982918

28992919
$parentInfo->collectInheritedMembers(
29002920
$parentsWithInheritedConstants,
29012921
$parentsWithInheritedProperties,
29022922
$parentsWithInheritedMethods,
2923+
$hasConstructor,
29032924
$classMap
29042925
);
29052926
}
@@ -2921,6 +2942,7 @@ private function collectInheritedMembers(
29212942
$parentsWithInheritedConstants,
29222943
$unusedParentsWithInheritedProperties,
29232944
$unusedParentsWithInheritedMethods,
2945+
$hasConstructor,
29242946
$classMap
29252947
);
29262948
}
@@ -2937,6 +2959,17 @@ private function hasConstructor(): bool
29372959
return false;
29382960
}
29392961

2962+
private function hasNonPrivateConstructor(): bool
2963+
{
2964+
foreach ($this->funcInfos as $funcInfo) {
2965+
if ($funcInfo->name->isConstructor() && !($funcInfo->flags & Class_::MODIFIER_PRIVATE)) {
2966+
return true;
2967+
}
2968+
}
2969+
2970+
return false;
2971+
}
2972+
29402973
private function hasDestructor(): bool
29412974
{
29422975
foreach ($this->funcInfos as $funcInfo) {
@@ -4061,14 +4094,14 @@ static function (FuncInfo $funcInfo) use ($allConstInfos) {
40614094
);
40624095
}
40634096

4064-
/** @param FuncInfo<string, FuncInfo> $funcInfos */
4065-
function generateOptimizerInfo(array $funcInfos): string {
4097+
/** @param array<string, FuncInfo> $funcMap */
4098+
function generateOptimizerInfo(array $funcMap): string {
40664099

40674100
$code = "/* This is a generated file, edit the .stub.php files instead. */\n\n";
40684101

40694102
$code .= "static const func_info_t func_infos[] = {\n";
40704103

4071-
$code .= generateCodeWithConditions($funcInfos, "", static function (FuncInfo $funcInfo) {
4104+
$code .= generateCodeWithConditions($funcMap, "", static function (FuncInfo $funcInfo) {
40724105
return $funcInfo->getOptimizerInfo();
40734106
});
40744107

@@ -4691,7 +4724,6 @@ function initPhpParser() {
46914724

46924725
foreach ($fileInfos as $fileInfo) {
46934726
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
4694-
/** @var FuncInfo $funcInfo */
46954727
$funcMap[$funcInfo->name->__toString()] = $funcInfo;
46964728

46974729
// TODO: Don't use aliasMap for methodsynopsis?

0 commit comments

Comments
 (0)