Completed
Push — update/slideshow-block-image-s... ( 1406a4...8d463f )
by
unknown
08:16
created

packages/analyzer/src/Invocations/Visitor.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Automattic\Jetpack\Analyzer\Invocations;
4
5
use PhpParser\NodeVisitorAbstract;
6
use PhpParser\Node;
7
use Automattic\Jetpack\Analyzer\Utils;
8
9
class Visitor extends NodeVisitorAbstract {
10
	private $invocations;
11
	private $file_path;
12
13
	public function __construct( $file_path, $invocations ) {
14
		$this->file_path   = $file_path;
15
		$this->invocations = $invocations;
16
	}
17
18
	public function enterNode( Node $node ) {
19
		if ( $node instanceof Node\Expr\New_ ) {
20
			$this->invocations->add(
21
				new New_( $this->file_path, $node->getLine(), Utils::node_to_class_name( $node->class ) )
22
			);
23
		} elseif ( $node instanceof Node\Expr\StaticCall ) {
24
			// TODO - args
25
			$this->invocations->add(
26
				new Static_Call( $this->file_path, $node->getLine(), Utils::node_to_class_name( $node->class ), $node->name->name )
27
			);
28
		} elseif ( $node instanceof Node\Expr\StaticPropertyFetch ) {
29
			$this->invocations->add(
30
				new Static_Property( $this->file_path, $node->getLine(), Utils::node_to_class_name( $node->class ), $node->name->name )
31
			);
32
		} elseif ( $node instanceof Node\Expr\FuncCall ) {
33
			// TODO - args
34
			if ( $node->name instanceof Node\Expr\Variable ) {
35
				$function_name = '$' . Utils::maybe_stringify( $node->name->name );
36 View Code Duplication
			} elseif ( $node->name instanceof Node\Expr\ArrayDimFetch ) {
0 ignored issues
show
The class PhpParser\Node\Expr\ArrayDimFetch does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
				$function_name = '$' . Utils::maybe_stringify( $node->name->var->name ) . '[' . Utils::maybe_stringify( $node->name->dim->value ) . ']';
38
			} else {
39
				$function_name = implode( '\\', $node->name->parts );
40
			}
41
42
			$this->invocations->add( new Function_Call( $this->file_path, $node->getLine(), $function_name ) );
43
		} elseif ( $node instanceof Node\Expr\ClassConstFetch ) {
44
			$this->invocations->add(
45
				new Static_Const( $this->file_path, $node->getLine(), Utils::node_to_class_name( $node->class ), $node->name->name )
46
			);
47
		} else {
48
			// print_r( $node );
49
		}
50
	}
51
52
	public function leaveNode( Node $node ) {
53
	}
54
}
55