Skip to content

Commit

Permalink
Fix a few more classsynopsis generation issues
Browse files Browse the repository at this point in the history
Closes GH-7480
  • Loading branch information
kocsismate committed Sep 22, 2021
1 parent b9f12d6 commit 8168d31
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
20 changes: 16 additions & 4 deletions Zend/zend_exceptions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ public function getTraceAsString(): string;

class Exception implements Throwable
{
/** @var string Intentionally left untyped for BC reasons */
/**
* Intentionally left untyped for BC reasons
* @var string
*/
protected $message = "";
private string $string = "";
/** @var int Intentionally left untyped for BC reasons */
/**
* Intentionally left untyped for BC reasons
* @var int
*/
protected $code = 0;
protected string $file = "";
protected int $line = 0;
Expand Down Expand Up @@ -75,10 +81,16 @@ final public function getSeverity(): int {}

class Error implements Throwable
{
/** @var string Intentionally left untyped for BC reasons */
/**
* Intentionally left untyped for BC reasons
* @var string
*/
protected $message = "";
private string $string = "";
/** @var int Intentionally left untyped for BC reasons */
/**
* Intentionally left untyped for BC reasons
* @var int
*/
protected $code = 0;
protected string $file = "";
protected int $line;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_exceptions_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 48d8444730a0e1fbc5d81965733deb3d3c11e122 */
* Stub hash: 9d1c2027ebd14e621d9b5b79056ba7300a455be8 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
Expand Down
68 changes: 47 additions & 21 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1390,18 +1390,28 @@ class PropertyInfo
public $flags;
/** @var Type|null */
public $type;
/** @var Type|null */
public $phpDocType;
/** @var Expr|null */
public $defaultValue;
/** @var string|null */
public $defaultValueString;
/** @var bool */
public $isDocReadonly;

public function __construct(PropertyName $name, int $flags, ?Type $type, ?Expr $defaultValue, ?string $defaultValueString, bool $isDocReadonly)
{
public function __construct(
PropertyName $name,
int $flags,
?Type $type,
?Type $phpDocType,
?Expr $defaultValue,
?string $defaultValueString,
bool $isDocReadonly
) {
$this->name = $name;
$this->flags = $flags;
$this->type = $type;
$this->phpDocType = $phpDocType;
$this->defaultValue = $defaultValue;
$this->defaultValueString = $defaultValueString;
$this->isDocReadonly = $isDocReadonly;
Expand Down Expand Up @@ -1527,10 +1537,8 @@ public function getFieldSynopsisElement(DOMDocument $doc): DOMElement
$fieldsynopsisElement->appendChild($doc->createElement("modifier", "readonly"));
}

if ($this->type) {
$fieldsynopsisElement->appendChild(new DOMText("\n "));
$fieldsynopsisElement->appendChild($this->type->getTypeForDoc($doc));
}
$fieldsynopsisElement->appendChild(new DOMText("\n "));
$fieldsynopsisElement->appendChild($this->getFieldSynopsisType()->getTypeForDoc($doc));

$className = str_replace("\\", "-", $this->name->class->toLowerString());
$varnameElement = $doc->createElement("varname", $this->name->property);
Expand All @@ -1549,6 +1557,18 @@ public function getFieldSynopsisElement(DOMDocument $doc): DOMElement
return $fieldsynopsisElement;
}

private function getFieldSynopsisType(): Type {
if ($this->type) {
return $this->type;
}

if ($this->phpDocType) {
return $this->phpDocType;
}

throw new Exception("A property must have a type");
}

/** @return mixed */
private function evaluateDefaultValue(bool &$defaultValueConstant)
{
Expand Down Expand Up @@ -1813,7 +1833,7 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM

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

Expand Down Expand Up @@ -1876,10 +1896,11 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM

foreach ($parentsWithInheritedProperties as $parent) {
$classSynopsis->appendChild(new DOMText("\n "));
$parentClassName = self::getClassSynopsisFilename($parent);
$parentReference = self::getClassSynopsisReference($parent);

$includeElement = $this->createIncludeElement(
$doc,
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.$parentClassName')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Properties;']]))"
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$parentReference')/db:partintro/db:section/db:classsynopsis/db:fieldsynopsis[preceding-sibling::db:classsynopsisinfo[1][@role='comment' and text()='&Properties;']]))"
);
$classSynopsis->appendChild($includeElement);
}
Expand All @@ -1891,13 +1912,13 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM
$classSynopsisInfo->setAttribute("role", "comment");
$classSynopsis->appendChild($classSynopsisInfo);

$className = self::getClassSynopsisFilename($this->name);
$classReference = self::getClassSynopsisReference($this->name);

if ($this->hasConstructor()) {
$classSynopsis->appendChild(new DOMText("\n "));
$includeElement = $this->createIncludeElement(
$doc,
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.$className')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[not(@role='procedural')])"
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:constructorsynopsis[not(@role='procedural')])"
);
$classSynopsis->appendChild($includeElement);
}
Expand All @@ -1906,7 +1927,7 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM
$classSynopsis->appendChild(new DOMText("\n "));
$includeElement = $this->createIncludeElement(
$doc,
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.$className')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[1])"
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"
);
$classSynopsis->appendChild($includeElement);
}
Expand All @@ -1915,7 +1936,7 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM
$classSynopsis->appendChild(new DOMText("\n "));
$includeElement = $this->createIncludeElement(
$doc,
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.$className')/db:refentry/db:refsect1[@role='description']/descendant::db:destructorsynopsis[not(@role='procedural')])"
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$classReference')/db:refentry/db:refsect1[@role='description']/descendant::db:destructorsynopsis[not(@role='procedural')])"
);
$classSynopsis->appendChild($includeElement);
}
Expand All @@ -1929,10 +1950,10 @@ public function getClassSynopsisElement(DOMDocument $doc, array $classMap): ?DOM

foreach ($parentsWithInheritedMethods as $parent) {
$classSynopsis->appendChild(new DOMText("\n "));
$parentClassName = self::getClassSynopsisFilename($parent);
$parentReference = self::getClassSynopsisReference($parent);
$includeElement = $this->createIncludeElement(
$doc,
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.$parentClassName')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[1])"
"xmlns(db=http://docbook.org/ns/docbook) xpointer(id('$parentReference')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])"
);
$classSynopsis->appendChild($includeElement);
}
Expand Down Expand Up @@ -1987,6 +2008,10 @@ public static function getClassSynopsisFilename(Name $name): string {
return strtolower(implode('-', $name->parts));
}

public static function getClassSynopsisReference(Name $name): string {
return "class." . strtolower(implode('-', $name->parts));
}

/**
* @param Name[] $parentsWithInheritedProperties
* @param Name[] $parentsWithInheritedMethods
Expand Down Expand Up @@ -2146,15 +2171,15 @@ public function getType(): string {

if ($this->name === "param") {
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)\s*\$\w+.*$/', $value, $matches);
} elseif ($this->name === "return") {
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)(\s+|$)/', $value, $matches);
} elseif ($this->name === "return" || $this->name === "var") {
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)/', $value, $matches);
}

if (!isset($matches[1])) {
throw new Exception("@$this->name doesn't contain a type or has an invalid format \"$value\"");
}

return $matches[1];
return trim($matches[1]);
}

public function getVariableName(): string {
Expand Down Expand Up @@ -2349,22 +2374,22 @@ function parseProperty(
?DocComment $comment,
PrettyPrinterAbstract $prettyPrinter
): PropertyInfo {
$docType = false;
$phpDocType = null;
$isDocReadonly = false;

if ($comment) {
$tags = parseDocComment($comment);
foreach ($tags as $tag) {
if ($tag->name === 'var') {
$docType = true;
$phpDocType = $tag->getType();
} elseif ($tag->name === 'readonly') {
$isDocReadonly = true;
}
}
}

$propertyType = $type ? Type::fromNode($type) : null;
if ($propertyType === null && !$docType) {
if ($propertyType === null && !$phpDocType) {
throw new Exception("Missing type for property $class::\$$property->name");
}

Expand All @@ -2383,6 +2408,7 @@ function parseProperty(
new PropertyName($class, $property->name->__toString()),
$flags,
$propertyType,
$phpDocType ? Type::fromString($phpDocType) : null,
$property->default,
$property->default ? $prettyPrinter->prettyPrintExpr($property->default) : null,
$isDocReadonly
Expand Down
5 changes: 4 additions & 1 deletion ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,10 @@ public function prepend(...$nodes): void {}

final class DOMException extends Exception
{
/** @var int Intentionally left untyped */
/**
* Intentionally left untyped for BC reasons
* @var int
*/
public $code = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: ccaa66ce5837d475f6231d1a1eadefa8e74db6b9 */
* Stub hash: 74698bea9c5e0635cf91345e8512b9677489510c */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_dom_import_simplexml, 0, 1, DOMElement, 0)
ZEND_ARG_TYPE_INFO(0, node, IS_OBJECT, 0)
Expand Down

0 comments on commit 8168d31

Please sign in to comment.