1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Analyzer\Invocations; |
4
|
|
|
|
5
|
|
|
use PhpParser\NodeVisitorAbstract; |
6
|
|
|
use PhpParser\Node; |
7
|
|
|
|
8
|
|
|
class Visitor extends NodeVisitorAbstract { |
9
|
|
|
private $invocations; |
10
|
|
|
private $file_path; |
11
|
|
|
|
12
|
|
|
public function __construct( $file_path, $invocations ) { |
13
|
|
|
$this->file_path = $file_path; |
14
|
|
|
$this->invocations = $invocations; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function enterNode( Node $node ) { |
18
|
|
|
if ( $node instanceof Node\Expr\New_ ) { |
|
|
|
|
19
|
|
|
$this->invocations->add( |
20
|
|
|
new New_( $this->file_path, $node->getLine(), $this->node_to_class_name( $node->class ) ) |
21
|
|
|
); |
22
|
|
|
} elseif ( $node instanceof Node\Expr\StaticCall ) { |
|
|
|
|
23
|
|
|
// TODO - args |
24
|
|
|
$this->invocations->add( |
25
|
|
|
new Static_Call( $this->file_path, $node->getLine(), $this->node_to_class_name( $node->class ), $node->name->name ) |
26
|
|
|
); |
27
|
|
|
} elseif ( $node instanceof Node\Expr\StaticPropertyFetch ) { |
|
|
|
|
28
|
|
|
$this->invocations->add( |
29
|
|
|
new Static_Property( $this->file_path, $node->getLine(), $this->node_to_class_name( $node->class ), $node->name->name ) |
30
|
|
|
); |
31
|
|
|
} elseif ( $node instanceof Node\Expr\FuncCall ) { |
|
|
|
|
32
|
|
|
// TODO - args |
33
|
|
|
if ( $node->name instanceof Node\Expr\Variable ) { |
|
|
|
|
34
|
|
|
$function_name = '$' . $this->maybe_stringify( $node->name->name ); |
35
|
|
|
} else { |
36
|
|
|
$function_name = implode( '\\', $node->name->parts ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->invocations->add( new Function_Call( $this->file_path, $node->getLine(), $function_name ) ); |
40
|
|
|
} else { |
41
|
|
|
// print_r( $node ); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function leaveNode( Node $node ) { |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function node_to_class_name( $node ) { |
49
|
|
|
if ( $node instanceof Node\Expr\Variable |
|
|
|
|
50
|
|
|
|| $node instanceof Node\Stmt\Class_ ) { |
|
|
|
|
51
|
|
|
$class_name = $node->name; |
52
|
|
|
} elseif ( $node instanceof Node\Name ) { |
|
|
|
|
53
|
|
|
$class_name = '\\' . implode( '\\', $node->parts ); |
54
|
|
|
} elseif ( $node instanceof Node\Expr\PropertyFetch ) { |
|
|
|
|
55
|
|
|
$class_name = |
56
|
|
|
'$' |
57
|
|
|
. $this->maybe_stringify( $node->var->name ) |
58
|
|
|
. '->' . $this->maybe_stringify( $node->name->name ); |
59
|
|
|
} elseif ( $node instanceof Node\Expr\ArrayDimFetch ) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$class_name = |
62
|
|
|
'$' |
63
|
|
|
. $this->maybe_stringify( $node->var->name ) |
64
|
|
|
. '[' . $this->maybe_stringify( $node->dim->value ) |
65
|
|
|
. ']'; |
66
|
|
|
} else { |
67
|
|
|
if ( method_exists( $node, 'toCodeString' ) ) { |
68
|
|
|
$class_name = $node->toCodeString(); |
69
|
|
|
} else { |
70
|
|
|
$class_name = get_class( $node ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $class_name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function maybe_stringify( $object ) { |
78
|
|
|
$is_stringifiable = ( |
79
|
|
|
is_string( $object ) |
80
|
|
|
|| ( |
81
|
|
|
is_object( $object ) |
82
|
|
|
&& method_exists( $object, '__toString' ) |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
if ( $is_stringifiable ) { |
87
|
|
|
return (string) $object; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Objects that need additional recursion to properly stringify |
91
|
|
|
// are of no interest to us because we won't know what classes |
92
|
|
|
// or methods they use without runtime analysis. |
93
|
|
|
return get_class( $object ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.