| Conditions | 19 |
| Paths | 14 |
| Total Lines | 55 |
| 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 |
||
| 87 | function getNameOfNode($node) |
||
| 88 | { |
||
| 89 | if (is_string($node)) { |
||
| 90 | return $node; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($node instanceof \PhpParser\Node\Name\FullyQualified) { |
||
| 94 | return (string)$node; |
||
| 95 | } |
||
| 96 | if ($node instanceof \PhpParser\Node\Expr\New_) { |
||
| 97 | return getNameOfNode($node->class); |
||
| 98 | } |
||
| 99 | |||
| 100 | if (isset($node->class)) { |
||
|
|
|||
| 101 | return getNameOfNode($node->class); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($node instanceof \PhpParser\Node\Name) { |
||
| 105 | return (string)implode($node->parts); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) { |
||
| 109 | return getNameOfNode($node->name); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) { |
||
| 113 | return getNameOfNode($node->name); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) { |
||
| 117 | return getNameOfNode($node->var); |
||
| 118 | } |
||
| 119 | |||
| 120 | if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) { |
||
| 121 | return get_class($node->name); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) { |
||
| 125 | return getNameOfNode($node->var); |
||
| 126 | } |
||
| 127 | |||
| 128 | if (isset($node->name) && !is_string($node->name)) { |
||
| 129 | return getNameOfNode($node->name); |
||
| 130 | } |
||
| 131 | |||
| 132 | if (isset($node->name) && null === $node->name) { |
||
| 133 | return 'anonymous@' . spl_object_hash($node); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (isset($node->name)) { |
||
| 137 | return (string)$node->name; |
||
| 138 | } |
||
| 139 | |||
| 140 | return null; |
||
| 141 | } |
||
| 142 | |||
| 241 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: