Completed
Push — try/static-code-analysis-featu... ( 0afe5b...f5e415 )
by
unknown
08:20
created

Utils   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 9.76
c 0
b 0
f 0
wmc 33
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B get_param_default_as_string() 0 19 9
B node_to_class_name() 0 32 9
B maybe_stringify() 0 23 6
A has_doc_comment() 0 7 3
A has_function_call() 0 16 6
1
<?php
2
3
namespace Automattic\Jetpack\Analyzer;
4
5
use PhpParser\Node;
6
7
/**
8
 * Shared code that probably should be a trait
9
 */
10
class Utils {
11
	/**
12
	 * parses the node used to describe parameter defaults into a string for easy comparison
13
	 */
14
	static function get_param_default_as_string( $default, $current_class ) {
15
		if ( $default instanceof Node\Expr\Array_ ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\Array_ 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...
16
			return 'array()';
17
		} elseif ( $default instanceof Node\Expr\ConstFetch ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\ConstFetch 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...
18
			return $default->name->toCodeString();
19
		} elseif ( $default instanceof Node\Scalar\LNumber || $default instanceof Node\Scalar\DNumber ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\LNumber 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...
Bug introduced by
The class PhpParser\Node\Scalar\DNumber 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...
20
			return $default->value;
21
		} elseif ( $default instanceof Node\Scalar\String_ ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Scalar\String_ 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...
22
			return '\'' . $default->value . '\'';
23
		} elseif ( $default instanceof Node\Expr\UnaryMinus ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\UnaryMinus 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...
24
			return '-' . self::get_param_default_as_string( $default->expr, $current_class );
25
		} elseif ( $default instanceof Node\Expr\UnaryPlus ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\UnaryPlus 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...
26
			return '+' . self::get_param_default_as_string( $default->expr, $current_class );
27
		} elseif ( $default instanceof Node\Expr\ClassConstFetch ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Expr\ClassConstFetch 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
			return self::node_to_class_name( $default->class, $current_class ) . '::' . $default->name->name;
29
		} else {
30
			return $default;
31
		}
32
	}
33
34
	static function node_to_class_name( $node, $class_for_self = null ) {
35
		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...
36
			|| $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...
37
			$class_name = $node->name;
38
		} 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...
39
			$class_name = '\\' . implode( '\\', $node->parts );
40
		} 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...
41
			$class_name =
42
						'$'
43
						. self::maybe_stringify( $node->var->name )
44
						. '->' . self::maybe_stringify( $node->name->name );
45
		} 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...
46
47
			$class_name =
48
						'$'
49
						. self::maybe_stringify( $node->var->name )
50
						. '[' . self::maybe_stringify( $node->dim->value )
51
						. ']';
52
		} else {
53
			if ( method_exists( $node, 'toCodeString' ) ) {
54
				$class_name = $node->toCodeString();
55
			} else {
56
				$class_name = get_class( $node );
57
			}
58
		}
59
60
		if ( $class_name === '\\self' && ! is_null( $class_for_self ) ) {
61
			$class_name = $class_for_self;
62
		}
63
64
		return $class_name;
65
	}
66
67
	static function maybe_stringify( $object ) {
68
		$is_stringifiable = (
69
			is_numeric( $object )
70
			|| is_string( $object )
71
			|| (
72
				is_object( $object )
73
				&& method_exists( $object, '__toString' )
74
			)
75
		);
76
77
		if ( $is_stringifiable ) {
78
			return (string) $object;
79
		}
80
81
		if ( is_null( $object ) ) {
82
			return '';
83
		}
84
85
		// Objects that need additional recursion to properly stringify
86
		// are of no interest to us because we won't know what classes
87
		// or methods they use without runtime analysis.
88
		return get_class( $object );
89
	}
90
91
	/**
92
	 * Check if a node has a docblock containing a specific comment string.
93
	 *
94
	 * @param PhpParser/Node $node    Current node we are parsing.
0 ignored issues
show
Documentation introduced by
The doc-type PhpParser/Node could not be parsed: Unknown type name "PhpParser/Node" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
95
	 * @param string         $comment Comment to match.
96
	 * @return boolean
97
	 */
98
	public static function has_doc_comment( $node, $comment ) {
99
		if ( $node->getDocComment() && false !== strpos( $node->getDocComment(), $comment ) ) {
100
			return true;
101
		}
102
103
		return false;
104
	}
105
106
	/**
107
	 * Check if a node contains a call to a function name.
108
	 * Any part of the function name will be matched.
109
	 *
110
	 * @param PhpParser/Node $node Current node we are parsing.
0 ignored issues
show
Documentation introduced by
The doc-type PhpParser/Node could not be parsed: Unknown type name "PhpParser/Node" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
111
	 * @param string         $name Function name to match.
112
	 * @return boolean
113
	 */
114
	public static function has_function_call( $node, $name ) {
115
		if ( empty( $node->getStmts() ) ) {
116
			return false;
117
		}
118
119
		foreach ( $node->getStmts() as $stmt ) {
120
			if ( ! $stmt instanceof Node\Stmt\Expression || ! $stmt->expr instanceof Node\Expr\FuncCall ) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Expression 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...
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...
121
				continue;
122
			}
123
			if ( false !== strpos( $stmt->expr->name->toCodeString(), $name ) ) {
124
				return true;
125
			}
126
		}
127
128
		return false;
129
	}
130
131
}
132