Skip to content

Generate the new class synopsis markup #11809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
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;
}
}
}
Comment on lines +3054 to +3065
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just to check that Error and Exception have an implements Throwable? that seems kinda overkill? logically every other exception must be extending one of these two classes so maybe it makes more sense to just special case those two classes and let the other loop do its job.

Either case this is also fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to rely on the fact that exceptions currently either extend Error or Exception so that's why I used the more general approach. I'm fine with special casing the logic if you want to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh it's fine with me :)


return false;
}

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