| Conditions | 24 |
| Paths | 1504 |
| Total Lines | 89 |
| Code Lines | 43 |
| Lines | 10 |
| Ratio | 11.24 % |
| 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 |
||
| 40 | public function leaveNode(Node $node) |
||
| 41 | { |
||
| 42 | |||
| 43 | if ($node instanceof Stmt\Namespace_) { |
||
| 44 | $this->uses = []; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($node instanceof Stmt\Use_) { |
||
| 48 | $this->uses = array_merge($this->uses, $node->uses); |
||
|
|
|||
| 49 | } |
||
| 50 | |||
| 51 | if ($node instanceof Stmt\Class_ |
||
| 52 | || $node instanceof Stmt\Interface_ |
||
| 53 | ) { |
||
| 54 | |||
| 55 | $class = $this->metrics->get($node->namespacedName->toString()); |
||
| 56 | $nodeClass = $node; |
||
| 57 | |||
| 58 | $dependencies = []; |
||
| 59 | |||
| 60 | // extends |
||
| 61 | if (isset($node->extends)) { |
||
| 62 | if (is_array($node->extends)) { |
||
| 63 | foreach ((array)$node->extends as $interface) { |
||
| 64 | array_push($dependencies, (string)$interface); |
||
| 65 | } |
||
| 66 | } else { |
||
| 67 | array_push($dependencies, (string)$node->extends); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | // implements |
||
| 72 | if (isset($node->implements)) { |
||
| 73 | foreach ($node->implements as $interface) { |
||
| 74 | array_push($dependencies, (string)$interface); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | foreach ($node->stmts as $stmt) { |
||
| 79 | if ($stmt instanceof Stmt\ClassMethod) { |
||
| 80 | |||
| 81 | |||
| 82 | // return |
||
| 83 | if (isset($stmt->returnType)) { |
||
| 84 | if ($stmt->returnType instanceof Node\Name\FullyQualified) { |
||
| 85 | array_push($dependencies, (string)$stmt->returnType); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // Type hint of method's parameters |
||
| 90 | foreach ($stmt->params as $param) { |
||
| 91 | if ($param->type) { |
||
| 92 | if ($param->type instanceof Node\Name\FullyQualified) { |
||
| 93 | array_push($dependencies, (string)$param->type); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // instanciations, static calls |
||
| 99 | \iterate_over_node($stmt, function ($node) use (&$dependencies) { |
||
| 100 | View Code Duplication | switch (true) { |
|
| 101 | case $node instanceof Node\Expr\New_: |
||
| 102 | // new MyClass |
||
| 103 | array_push($dependencies, getNameOfNode($node)); |
||
| 104 | break; |
||
| 105 | case $node instanceof Node\Expr\StaticCall: |
||
| 106 | // MyClass::Call |
||
| 107 | array_push($dependencies, getNameOfNode($node)); |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | }); |
||
| 111 | |||
| 112 | // annotations |
||
| 113 | $comments = $stmt->getDocComment(); |
||
| 114 | if ($comments && false !== preg_match_all('!\s+\*\s+@(\w+)!', $comments->getText(), $matches)) { |
||
| 115 | foreach ($matches[1] as $check) { |
||
| 116 | foreach ($this->uses as $use) { |
||
| 117 | if ($use->alias === $check) { |
||
| 118 | array_push($dependencies, (string)($use->name)); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $class->set('externals', $dependencies); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 131 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..