Completed
Push — add/legacy-files ( 4cc789...bef25d )
by
unknown
157:43 queued 148:20
created

Visitor::node_to_class_name()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 1
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
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_ ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\New_ 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...
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 ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\StaticCall 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...
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 ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\StaticPropertyFetch 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...
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 ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\FuncCall 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...
32
			// TODO - args
33
			if ( $node->name instanceof Node\Expr\Variable ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable 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...
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
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Variable 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...
50
			|| $node instanceof Node\Stmt\Class_ ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Class_ 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...
51
			$class_name = $node->name;
52
		} elseif ( $node instanceof Node\Name ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Name 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...
53
			$class_name = '\\' . implode( '\\', $node->parts );
54
		} elseif ( $node instanceof Node\Expr\PropertyFetch ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\PropertyFetch 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...
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 ) {
0 ignored issues
show
Bug introduced by
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...
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