Skip to content

Commit

Permalink
Use new class synopsis generating markup (#11809)
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Jul 28, 2023
1 parent 585bdf2 commit 566cf37
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions build/gen_stub.php
Expand Up @@ -2735,27 +2735,15 @@ public function getClassSynopsisDocument(array $classMap, iterable $allConstInfo
public function getClassSynopsisElement(DOMDocument $doc, array $classMap, iterable $allConstInfos): ?DOMElement {

$classSynopsis = $doc->createElement("classsynopsis");
if ($this->type === "interface") {
$classSynopsis->setAttribute("class", "interface");
}
$classSynopsis->appendChild(new DOMText("\n "));
$classSynopsis->setAttribute("class", $this->type === "interface" ? "interface" : "class");

$ooElement = self::createOoElement($doc, $this, true, false, false, 4);
$exceptionOverride = $this->isException($classMap) ? "exception" : null;
$ooElement = self::createOoElement($doc, $this, $exceptionOverride, true, null, 4);
if (!$ooElement) {
return null;
}
$classSynopsis->appendChild(new DOMText("\n "));
$classSynopsis->appendChild($ooElement);
$classSynopsis->appendChild(new DOMText("\n\n "));

$classSynopsisInfo = $doc->createElement("classsynopsisinfo");
$classSynopsisInfo->appendChild(new DOMText("\n "));
$ooElement = self::createOoElement($doc, $this, false, true, false, 5);
if (!$ooElement) {
return null;
}
$classSynopsisInfo->appendChild($ooElement);

$classSynopsis->appendChild($classSynopsisInfo);

foreach ($this->extends as $k => $parent) {
$parentInfo = $classMap[$parent->toString()] ?? null;
Expand All @@ -2766,33 +2754,32 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap, itera
$ooElement = self::createOoElement(
$doc,
$parentInfo,
null,
false,
false,
$k === 0 && $this->type === "class",
5
$k === 0 ? "extends" : null,
4
);
if (!$ooElement) {
return null;
}

$classSynopsisInfo->appendChild(new DOMText("\n\n "));
$classSynopsisInfo->appendChild($ooElement);
$classSynopsis->appendChild(new DOMText("\n\n "));
$classSynopsis->appendChild($ooElement);
}

foreach ($this->implements as $interface) {
foreach ($this->implements as $k => $interface) {
$interfaceInfo = $classMap[$interface->toString()] ?? null;
if (!$interfaceInfo) {
throw new Exception("Missing implemented interface " . $interface->toString());
}

$ooElement = self::createOoElement($doc, $interfaceInfo, false, false, false, 5);
$ooElement = self::createOoElement($doc, $interfaceInfo, null, false, $k === 0 ? "implements" : null, 4);
if (!$ooElement) {
return null;
}
$classSynopsisInfo->appendChild(new DOMText("\n\n "));
$classSynopsisInfo->appendChild($ooElement);
$classSynopsis->appendChild(new DOMText("\n\n "));
$classSynopsis->appendChild($ooElement);
}
$classSynopsisInfo->appendChild(new DOMText("\n "));

/** @var array<string, Name> $parentsWithInheritedConstants */
$parentsWithInheritedConstants = [];
Expand Down Expand Up @@ -2921,9 +2908,9 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap, itera
private static function createOoElement(
DOMDocument $doc,
ClassInfo $classInfo,
bool $overrideToClass,
?string $typeOverride,
bool $withModifiers,
bool $isExtends,
?string $modifierOverride,
int $indentationLevel
): ?DOMElement {
$indentation = str_repeat(" ", $indentationLevel);
Expand All @@ -2933,12 +2920,12 @@ private static function createOoElement(
return null;
}

$type = $overrideToClass ? "class" : $classInfo->type;
$type = $typeOverride !== null ? $typeOverride : $classInfo->type;

$ooElement = $doc->createElement("oo$type");
$ooElement->appendChild(new DOMText("\n$indentation "));
if ($isExtends) {
$ooElement->appendChild($doc->createElement('modifier', 'extends'));
if ($modifierOverride !== null) {
$ooElement->appendChild($doc->createElement('modifier', $modifierOverride));
$ooElement->appendChild(new DOMText("\n$indentation "));
} elseif ($withModifiers) {
if ($classInfo->flags & Class_::MODIFIER_FINAL) {
Expand Down Expand Up @@ -3046,6 +3033,40 @@ private function collectInheritedMembers(
}
}

/** @param array<string, ClassInfo> $classMap */
private function isException(array $classMap): bool
{
if ($this->name->toString() === "Throwable") {
return true;
}

foreach ($this->extends as $parentName) {
$parent = $classMap[$parentName->toString()] ?? null;
if ($parent === null) {
throw new Exception("Missing parent class " . $parentName->toString());
}

if ($parent->isException($classMap)) {
return true;
}
}

if ($this->type === "class") {
foreach ($this->implements as $interfaceName) {
$interface = $classMap[$interfaceName->toString()] ?? null;
if ($interface === null) {
throw new Exception("Missing implemented interface " . $interfaceName->toString());
}

if ($interface->isException($classMap)) {
return true;
}
}
}

return false;
}

private function hasConstructor(): bool
{
foreach ($this->funcInfos as $funcInfo) {
Expand Down

0 comments on commit 566cf37

Please sign in to comment.