Skip to content

Commit 81abd8d

Browse files
committed
Allow gen_stub.php to parse and ignore extended docblock types
Extended docblock types, according to psalm or phpstan conventions may include array shapes, callable signatures etc.. These are now ignored by ignoring any nested parenthesized expression (followed by optional :type) at the end of the type.
1 parent edacfbd commit 81abd8d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

build/gen_stub.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -3185,7 +3185,7 @@ public function getType(): string {
31853185
$matches = [];
31863186

31873187
if ($this->name === "param") {
3188-
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)\s*\$\w+.*$/', $value, $matches);
3188+
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)\s*(?:[{(]|\$\w+).*$/', $value, $matches);
31893189
} elseif ($this->name === "return" || $this->name === "var") {
31903190
preg_match('/^\s*([\w\|\\\\\[\]<>, ]+)/', $value, $matches);
31913191
}
@@ -3206,16 +3206,17 @@ public function getVariableName(): string {
32063206
$matches = [];
32073207

32083208
if ($this->name === "param") {
3209-
preg_match('/^\s*[\w\|\\\\\[\]]+\s*\$(\w+).*$/', $value, $matches);
3209+
// Allow for parsing extended types like callable(string):mixed in docblocks
3210+
preg_match('/^\s*(?<type>[\w\|\\\\]+(?<parens>\((?<inparens>(?:(?&parens)|[^(){}[\]]*+))++\)|\{(?&inparens)\}|\[(?&inparens)\])*+(?::(?&type))?)\s*\$(?<name>\w+).*$/', $value, $matches);
32103211
} elseif ($this->name === "prefer-ref") {
3211-
preg_match('/^\s*\$(\w+).*$/', $value, $matches);
3212+
preg_match('/^\s*\$(?<name>\w+).*$/', $value, $matches);
32123213
}
32133214

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

3218-
return $matches[1];
3219+
return $matches["name"];
32193220
}
32203221
}
32213222

0 commit comments

Comments
 (0)