| Conditions | 27 |
| Paths | 2080 |
| Total Lines | 97 |
| 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 |
||
| 39 | public function leaveNode(Node $node) |
||
| 40 | { |
||
| 41 | |||
| 42 | if ($node instanceof Stmt\Namespace_) { |
||
| 43 | $this->uses = []; |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($node instanceof Stmt\Use_) { |
||
| 47 | $this->uses = array_merge($this->uses, $node->uses); |
||
|
|
|||
| 48 | } |
||
| 49 | |||
| 50 | if ($node instanceof Stmt\Class_ |
||
| 51 | || $node instanceof Stmt\Interface_ |
||
| 52 | || $node instanceof Stmt\Trait_ |
||
| 53 | ) { |
||
| 54 | |||
| 55 | $class = $this->metrics->get(MetricClassNameGenerator::getName($node)); |
||
| 56 | $parents = []; |
||
| 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 | $this->pushToDependencies($dependencies, (string)$interface); |
||
| 65 | array_push($parents, (string)$interface); |
||
| 66 | } |
||
| 67 | } else { |
||
| 68 | $this->pushToDependencies($dependencies, (string)$node->extends); |
||
| 69 | array_push($parents, (string)$node->extends); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // implements |
||
| 74 | if (isset($node->implements)) { |
||
| 75 | foreach ($node->implements as $interface) { |
||
| 76 | $this->pushToDependencies($dependencies, (string)$interface); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | foreach ($node->stmts as $stmt) { |
||
| 81 | if ($stmt instanceof Stmt\ClassMethod) { |
||
| 82 | // return |
||
| 83 | if (isset($stmt->returnType)) { |
||
| 84 | if ($stmt->returnType instanceof Node\Name\FullyQualified) { |
||
| 85 | $this->pushToDependencies($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 | $this->pushToDependencies($dependencies, (string)$param->type); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // instantiations, static calls |
||
| 99 | \iterate_over_node($stmt, function ($node) use (&$dependencies) { |
||
| 100 | switch (true) { |
||
| 101 | case $node instanceof Node\Expr\New_: |
||
| 102 | // new MyClass |
||
| 103 | $this->pushToDependencies($dependencies, getNameOfNode($node)); |
||
| 104 | break; |
||
| 105 | case $node instanceof Node\Expr\StaticCall: |
||
| 106 | // MyClass::Call |
||
| 107 | $this->pushToDependencies($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 (method_exists($use, 'getAlias')) { |
||
| 118 | if (((string) $use->getAlias()) === $check) { |
||
| 119 | $this->pushToDependencies($dependencies, (string)($use->name)); |
||
| 120 | } |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | if ($use->alias === $check) { |
||
| 124 | $this->pushToDependencies($dependencies, (string)($use->name)); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $class->set('externals', $dependencies); |
||
| 133 | $class->set('parents', $parents); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 147 |
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..