Skip to content

Commit a10f887

Browse files
committed
Check missing parameter types in stubs
[skip ci] Closes phpGH-5627
1 parent cb2f689 commit a10f887

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

build/gen_stub.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,23 @@ public function getValue(): string {
576576

577577
public function getVariableName(): string {
578578
$value = $this->getValue();
579-
if ($value === null || strlen($value) === 0 || $value[0] !== '$') {
580-
throw new Exception("@$this->name not followed by variable name");
579+
if ($value === null || strlen($value) === 0) {
580+
throw new Exception("@$this->name doesn't have any value");
581581
}
582582

583-
return substr($value, 1);
583+
$matches = [];
584+
585+
if ($this->name === "param") {
586+
preg_match('/^\s*[\w\|\\\\]+\s*\$(\w+).*$/', $value, $matches);
587+
} elseif ($this->name === "prefer-ref") {
588+
preg_match('/^\s*\$(\w+).*$/', $value, $matches);
589+
}
590+
591+
if (isset($matches[1]) === false) {
592+
throw new Exception("@$this->name doesn't contain variable name or has an invalid format \"$value\"");
593+
}
594+
595+
return $matches[1];
584596
}
585597
}
586598

@@ -610,6 +622,7 @@ function parseFunctionLike(
610622
$alias = null;
611623
$isDeprecated = false;
612624
$haveDocReturnType = false;
625+
$docParamTypes = [];
613626

614627
if ($comment) {
615628
$tags = parseDocComment($comment);
@@ -631,6 +644,8 @@ function parseFunctionLike(
631644
$isDeprecated = true;
632645
} else if ($tag->name === 'return') {
633646
$haveDocReturnType = true;
647+
} else if ($tag->name === 'param') {
648+
$docParamTypes[$tag->getVariableName()] = true;
634649
}
635650
}
636651
}
@@ -656,6 +671,10 @@ function parseFunctionLike(
656671
}
657672

658673
$type = $param->type ? Type::fromNode($param->type) : null;
674+
if ($type === null && !isset($docParamTypes[$varName])) {
675+
throw new Exception("Missing parameter type for function $name()");
676+
}
677+
659678
if ($param->default instanceof Expr\ConstFetch &&
660679
$param->default->name->toLowerString() === "null" &&
661680
$type && !$type->isNullable()
@@ -1103,7 +1122,7 @@ function initPhpParser() {
11031122
$optind = null;
11041123
$options = getopt("f", ["force-regeneration"], $optind);
11051124
$forceRegeneration = isset($options["f"]) || isset($options["force-regeneration"]);
1106-
$location = $argv[$optind + 1] ?? ".";
1125+
$location = $argv[$optind] ?? ".";
11071126

11081127
if (is_file($location)) {
11091128
// Generate single file.

0 commit comments

Comments
 (0)