Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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 ) { |
||
33 | |||
34 | static function node_to_class_name( $node, $class_for_self = null ) { |
||
35 | if ( $node instanceof Node\Expr\Variable |
||
36 | || $node instanceof Node\Stmt\Class_ ) { |
||
37 | $class_name = $node->name; |
||
38 | } elseif ( $node instanceof Node\Name ) { |
||
39 | $class_name = '\\' . implode( '\\', $node->parts ); |
||
40 | } elseif ( $node instanceof Node\Expr\PropertyFetch ) { |
||
41 | $class_name = |
||
42 | '$' |
||
43 | . self::maybe_stringify( $node->var->name ) |
||
44 | . '->' . self::maybe_stringify( $node->name->name ); |
||
45 | View Code Duplication | } elseif ( $node instanceof Node\Expr\ArrayDimFetch ) { |
|
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 ) { |
||
89 | |||
90 | /** |
||
91 | * Check if a node has a docblock containing a specific comment string. |
||
92 | * |
||
93 | * @param PhpParser/Node $node Current node we are parsing. |
||
94 | * @param string $comment Comment to match. |
||
95 | * @return boolean |
||
96 | */ |
||
97 | public static function has_doc_comment( $node, $comment ) { |
||
104 | |||
105 | /** |
||
106 | * Check if a node contains a call to a function name. |
||
107 | * Any part of the function name will be matched. |
||
108 | * |
||
109 | * @param PhpParser/Node $node Current node we are parsing. |
||
110 | * @param string $name Function name to match. |
||
111 | * @return boolean |
||
112 | */ |
||
113 | public static function has_function_call( $node, $name ) { |
||
129 | |||
130 | } |
||
131 |
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.