| Conditions | 17 |
| Paths | 13 |
| Total Lines | 51 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 86 | function getNameOfNode($node) |
||
| 87 | { |
||
| 88 | if (is_string($node)) { |
||
| 89 | return $node; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($node instanceof \PhpParser\Node\Name\FullyQualified) { |
||
| 93 | return (string)$node; |
||
| 94 | } |
||
| 95 | if ($node instanceof \PhpParser\Node\Expr\New_) { |
||
| 96 | return getNameOfNode($node->class); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (isset($node->class)) { |
||
| 100 | return getNameOfNode($node->class); |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($node instanceof \PhpParser\Node\Name) { |
||
| 104 | return (string)implode($node->parts); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) { |
||
| 108 | return getNameOfNode($node->name); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) { |
||
| 112 | return getNameOfNode($node->name); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) { |
||
| 116 | return getNameOfNode($node->var); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) { |
||
| 120 | return get_class($node->name); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) { |
||
| 124 | return getNameOfNode($node->var); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (isset($node->name) && !is_string($node->name)) { |
||
| 128 | return getNameOfNode($node->name); |
||
| 129 | } |
||
| 130 | |||
| 131 | if (isset($node->name)) { |
||
| 132 | return (string)$node->name; |
||
| 133 | } |
||
| 134 | |||
| 135 | return null; |
||
| 136 | } |
||
| 137 | |||
| 166 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.