Skip to content

Add support for validation of missing method synopses #9491

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
Sep 7, 2022
Merged
Show file tree
Hide file tree
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
68 changes: 60 additions & 8 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,8 @@ class FuncInfo {
public $numRequiredArgs;
/** @var string|null */
public $cond;
/** @var bool */
public $isUndocumentable;

/**
* @param ArgInfo[] $args
Expand All @@ -1238,7 +1240,8 @@ public function __construct(
array $args,
ReturnInfo $return,
int $numRequiredArgs,
?string $cond
?string $cond,
bool $isUndocumentable
) {
$this->name = $name;
$this->classFlags = $classFlags;
Expand All @@ -1252,6 +1255,7 @@ public function __construct(
$this->return = $return;
$this->numRequiredArgs = $numRequiredArgs;
$this->cond = $cond;
$this->isUndocumentable = $isUndocumentable;
}

public function isMethod(): bool
Expand Down Expand Up @@ -3218,7 +3222,8 @@ function parseFunctionLike(
int $classFlags,
int $flags,
Node\FunctionLike $func,
?string $cond
?string $cond,
bool $isUndocumentable
): FuncInfo {
try {
$comment = $func->getDocComment();
Expand Down Expand Up @@ -3283,6 +3288,10 @@ function parseFunctionLike(
}
$paramMeta[$varName][$tag->name] = true;
break;

case 'undocumentable':
$isUndocumentable = true;
break;
}
}
}
Expand Down Expand Up @@ -3383,7 +3392,8 @@ function parseFunctionLike(
$args,
$return,
$numRequiredArgs,
$cond
$cond,
$isUndocumentable
);
} catch (Exception $e) {
throw new Exception($name . "(): " .$e->getMessage());
Expand Down Expand Up @@ -3570,6 +3580,12 @@ function parseClass(
throw new Exception("Unknown class kind " . get_class($class));
}

if ($isUndocumentable) {
foreach ($methods as $method) {
$method->isUndocumentable = true;
}
}

return new ClassInfo(
$name,
$flags,
Expand Down Expand Up @@ -3674,7 +3690,8 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
0,
0,
$stmt,
$cond
$cond,
$fileInfo->isUndocumentable
);
continue;
}
Expand Down Expand Up @@ -3731,7 +3748,8 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac
$classFlags,
$classStmt->flags | $abstractFlag,
$classStmt,
$cond
$cond,
$fileInfo->isUndocumentable
);
} else if ($classStmt instanceof Stmt\EnumCase) {
$enumCaseInfos[] = new EnumCaseInfo(
Expand Down Expand Up @@ -4342,7 +4360,7 @@ function replaceClassSynopses(string $targetDirectory, array $classMap, iterable
foreach ($missingClassSynopses as $className => $info) {
/** @var ClassInfo $info */
if (!$info->isUndocumentable) {
echo "Warning: Missing class synopsis page for $className\n";
echo "Warning: Missing class synopsis for $className\n";
}
}
}
Expand Down Expand Up @@ -4388,7 +4406,8 @@ function generateMethodSynopses(array $funcMap, array $aliasMap): array {
* @param array<string, FuncInfo> $aliasMap
* @return array<string, string>
*/
function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $aliasMap): array {
function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $aliasMap, bool $isVerify): array {
$existingMethodSynopses = [];
$methodSynopses = [];

$it = new RecursiveIteratorIterator(
Expand All @@ -4407,6 +4426,27 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
continue;
}

if ($isVerify) {
$matches = [];
preg_match("/<refname>\s*([\w:]+)\s*<\/refname>\s*<refpurpose>\s*&Alias;\s*<(?:function|methodname)>\s*([\w:]+)\s*<\/(?:function|methodname)>\s*<\/refpurpose>/i", $xml, $matches);
$aliasName = $matches[1] ?? null;
$alias = $funcMap[$aliasName] ?? null;
$funcName = $matches[2] ?? null;
$func = $funcMap[$funcName] ?? null;

if ($alias &&
!$alias->isUndocumentable &&
($func === null || $func->alias === null || $func->alias->__toString() !== $aliasName) &&
($alias->alias === null || $alias->alias->__toString() !== $funcName)
) {
echo "Warning: $aliasName()" . ($alias->alias ? " is an alias of " . $alias->alias->__toString() . "(), but it" : "") . " is incorrectly documented as an alias for $funcName()\n";
}

if ($aliasName) {
$existingMethodSynopses[$aliasName] = $aliasName;
}
}

if (stripos($xml, "<methodsynopsis") === false && stripos($xml, "<constructorsynopsis") === false && stripos($xml, "<destructorsynopsis") === false) {
continue;
}
Expand Down Expand Up @@ -4448,7 +4488,9 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
if (!isset($funcMap[$funcName])) {
continue;
}

$funcInfo = $funcMap[$funcName];
$existingMethodSynopses[$funcInfo->name->__toString()] = $funcInfo->name->__toString();

$newMethodSynopsis = $funcInfo->getMethodSynopsisElement($funcMap, $aliasMap, $doc);
if ($newMethodSynopsis === null) {
Expand Down Expand Up @@ -4534,6 +4576,16 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
}
}

if ($isVerify) {
$missingMethodSynopses = array_diff_key($funcMap, $existingMethodSynopses);
foreach ($missingMethodSynopses as $functionName => $info) {
/** @var FuncInfo $info */
if (!$info->isUndocumentable) {
echo "Warning: Missing method synopsis for $functionName()\n";
}
}
}

return $methodSynopses;
}

Expand Down Expand Up @@ -4845,7 +4897,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
}

if ($replaceMethodSynopses) {
$methodSynopses = replaceMethodSynopses($targetSynopses, $funcMap, $aliasMap);
$methodSynopses = replaceMethodSynopses($targetSynopses, $funcMap, $aliasMap, $verify);

foreach ($methodSynopses as $filename => $content) {
if (file_put_contents($filename, $content)) {
Expand Down
5 changes: 4 additions & 1 deletion ext/dl_test/dl_test.stub.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

/** @generate-class-entries */
/**
* @generate-class-entries
* @undocumentable
*/

function dl_test_test1(): void {}

Expand Down
2 changes: 1 addition & 1 deletion ext/dl_test/dl_test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion ext/skeleton/skeleton.stub.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

/** @generate-class-entries */
/**
* @generate-class-entries
* @undocumentable
*/

function test1(): void {}

Expand Down
2 changes: 1 addition & 1 deletion ext/skeleton/skeleton_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/zlib/zlib.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ function gzwrite($stream, string $data, ?int $length = null): int|false {}
/**
* @param resource $stream
* @alias fwrite
* @undocumentable gzputs is an alias of gzwrite, but transitive aliases are not yet supported
*/
function gzputs($stream, string $data, ?int $length = null): int|false {}

Expand Down
2 changes: 1 addition & 1 deletion ext/zlib/zlib_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.