Skip to content

Commit

Permalink
Assign discoverable names to properties (#184)
Browse files Browse the repository at this point in the history
* allow properties in functionMap

* add comment on how to declare a property type

* fix typo
  • Loading branch information
IanDelMar authored Jul 14, 2024
1 parent 5758db8 commit f462f3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 8 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
*
* '<class_name>' => [null, '<arg_name>' => '<arg_type>']
*
* For class methods:
*
* '<class_name::method_name>' => ['<return_type>', '<arg_name>' => '<arg_type>']
*
* For class properties:
*
* '<class_name::$property_name>' => [null, '@phpstan-var' => '<property_type>']
*
* @link https://github.com/phpstan/phpstan-src/blob/1.10.x/resources/functionMap.php
*/
return [
Expand Down
12 changes: 5 additions & 7 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ public function enterNode(Node $node)

$symbolName = self::getNodeName($node);

if ($node instanceof ClassMethod) {
if ($node instanceof ClassMethod || $node instanceof Property) {
$parent = $this->stack[count($this->stack) - 2];
\assert($parent instanceof \PhpParser\Node\Stmt\ClassLike);

if ($parent->name !== null) {
$symbolName = sprintf(
'%1$s::%2$s',
'%1$s::%2$s%3$s',
$parent->name->name,
$node->name->name
$node instanceof Property ? '$' : '',
$symbolName
);
}
}
Expand Down Expand Up @@ -125,10 +126,7 @@ private static function getNodeName(Node $node): string
}

if ($node instanceof Property) {
return sprintf(
'property_%s',
uniqid()
);
return $node->props[0]->name->name;
}

return '';
Expand Down

0 comments on commit f462f3f

Please sign in to comment.