Skip to content

Commit 36f7689

Browse files
committed
scan methods calls
1 parent 881c93b commit 36f7689

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [1.2.0] - Unreleased
9+
### Added
10+
- Function scanner extracts not only functions calls but also class methods calls.
11+
812
## [1.1.1] - 2019-11-25
913
### Fixed
1014
- Extract comments of functions prepended with echo, print or return [#6]
@@ -25,6 +29,7 @@ First version
2529
[#5]: https://github.com/php-gettext/PHP-Scanner/issues/5
2630
[#6]: https://github.com/php-gettext/PHP-Scanner/issues/6
2731

32+
[1.2.0]: https://github.com/php-gettext/PHP-Scanner/compare/v1.1.1...HEAD
2833
[1.1.1]: https://github.com/php-gettext/PHP-Scanner/compare/v1.1.0...v1.1.1
2934
[1.1.0]: https://github.com/php-gettext/PHP-Scanner/compare/v1.0.1...v1.1.0
3035
[1.0.1]: https://github.com/php-gettext/PHP-Scanner/compare/v1.0.0...v1.0.1

src/PhpNodeVisitor.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Expr\FuncCall;
10+
use PhpParser\Node\Identifier;
1011
use PhpParser\Node\Name;
1112
use PhpParser\NodeVisitor;
1213

@@ -30,18 +31,17 @@ public function beforeTraverse(array $nodes)
3031

3132
public function enterNode(Node $node)
3233
{
33-
if ($node instanceof FuncCall) {
34-
$name = ($node->name instanceof Name) ? $node->name->getLast() : null;
35-
36-
if ($name && ($this->validFunctions === null || in_array($name, $this->validFunctions))) {
37-
$this->functions[] = $this->createFunction($node);
38-
} elseif ($node->getComments()) {
39-
$this->bufferComments = $node;
40-
}
41-
return null;
42-
}
43-
4434
switch ($node->getType()) {
35+
case 'Expr_MethodCall':
36+
case 'Expr_FuncCall':
37+
$name = static::getName($node);
38+
39+
if ($name && ($this->validFunctions === null || in_array($name, $this->validFunctions))) {
40+
$this->functions[] = $this->createFunction($node);
41+
} elseif ($node->getComments()) {
42+
$this->bufferComments = $node;
43+
}
44+
return null;
4545
case 'Stmt_Echo':
4646
case 'Stmt_Return':
4747
case 'Expr_Print':
@@ -67,10 +67,13 @@ public function getFunctions(): array
6767
return $this->functions;
6868
}
6969

70-
protected function createFunction(FuncCall $node): ParsedFunction
70+
/**
71+
* @param FuncCall|MethodCall $node
72+
*/
73+
protected function createFunction(Expr $node): ParsedFunction
7174
{
7275
$function = new ParsedFunction(
73-
$node->name->getLast(),
76+
static::getName($node),
7477
$this->filename,
7578
$node->getStartLine(),
7679
$node->getEndLine()
@@ -114,6 +117,21 @@ protected static function getComment(Comment $comment): string
114117
return trim(implode("\n", $lines));
115118
}
116119

120+
protected static function getName(Node $node): ?string
121+
{
122+
$name = $node->name;
123+
124+
if ($name instanceof Name) {
125+
return $name->getLast();
126+
}
127+
128+
if ($name instanceof Identifier) {
129+
return (string) $name;
130+
}
131+
132+
return null;
133+
}
134+
117135
protected static function getValue(Expr $value)
118136
{
119137
$type = $value->getType();

tests/assets/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
//This comment is related with the first function
33

4-
print(fn1('arg1', 'arg2', 3));
4+
print($fn->fn1('arg1', 'arg2', 3));
55
fn2($var);
66
fn3(fn4('arg4'), 'arg5', fn5(6, 7.5));
77
fn6(['arr']);
@@ -13,7 +13,7 @@
1313
/* Comment to fn10 */ fn10();
1414

1515
//Related comment 1
16-
fn11(/* ALLOW: Related comment 2 */ 'arg9', 'arg10' /* No related comment 3 */);
16+
$trans->fn11(/* ALLOW: Related comment 2 */ 'arg9', 'arg10' /* No related comment 3 */);
1717

1818
/*
1919
Related comment

0 commit comments

Comments
 (0)