Skip to content

Commit 64058f5

Browse files
committed
Add warning for missing argument types in stubs
1 parent 0d330e1 commit 64058f5

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

build/gen_stub.php

Lines changed: 26 additions & 4 deletions
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,13 +644,16 @@ 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
}
637652

638653
$args = [];
639654
$numRequiredArgs = 0;
640655
$foundVariadic = false;
656+
$hasParameterWarning = false;
641657
foreach ($func->getParams() as $i => $param) {
642658
$varName = $param->var->name;
643659
$preferRef = !empty($paramMeta[$varName]['preferRef']);
@@ -656,6 +672,12 @@ function parseFunctionLike(
656672
}
657673

658674
$type = $param->type ? Type::fromNode($param->type) : null;
675+
if ($type === null && !isset($docParamTypes[$varName]) && !$hasParameterWarning) {
676+
$hasParameterWarning = true;
677+
//throw new Exception("Missing parameter type for function $name()");
678+
echo "Warning: Missing parameter type for function $name()\n";
679+
}
680+
659681
if ($param->default instanceof Expr\ConstFetch &&
660682
$param->default->name->toLowerString() === "null" &&
661683
$type && !$type->isNullable()
@@ -1103,7 +1125,7 @@ function initPhpParser() {
11031125
$optind = null;
11041126
$options = getopt("f", ["force-regeneration"], $optind);
11051127
$forceRegeneration = isset($options["f"]) || isset($options["force-regeneration"]);
1106-
$location = $argv[$optind + 1] ?? ".";
1128+
$location = $argv[$optind] ?? ".";
11071129

11081130
if (is_file($location)) {
11091131
// Generate single file.

0 commit comments

Comments
 (0)