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:
Complex classes like NameResolver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NameResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class NameResolver extends NodeVisitorAbstract |
||
| 14 | { |
||
| 15 | /** @var null|Name Current namespace */ |
||
| 16 | protected $namespace; |
||
| 17 | |||
| 18 | /** @var array Map of format [aliasType => [aliasName => originalName]] */ |
||
| 19 | protected $aliases; |
||
| 20 | |||
| 21 | public function beforeTraverse(array $nodes) { |
||
| 24 | |||
| 25 | public function enterNode(Node $node) { |
||
| 26 | if ($node instanceof Stmt\Namespace_) { |
||
| 27 | $this->resetState($node->name); |
||
|
|
|||
| 28 | } elseif ($node instanceof Stmt\Use_) { |
||
| 29 | foreach ($node->uses as $use) { |
||
| 30 | $this->addAlias($use, $node->type, null); |
||
| 31 | } |
||
| 32 | } elseif ($node instanceof Stmt\GroupUse) { |
||
| 33 | foreach ($node->uses as $use) { |
||
| 34 | $this->addAlias($use, $node->type, $node->prefix); |
||
| 35 | } |
||
| 36 | } elseif ($node instanceof Stmt\Class_) { |
||
| 37 | if (null !== $node->extends) { |
||
| 38 | $node->extends = $this->resolveClassName($node->extends); |
||
| 39 | } |
||
| 40 | |||
| 41 | foreach ($node->implements as &$interface) { |
||
| 42 | $interface = $this->resolveClassName($interface); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (null !== $node->name) { |
||
| 46 | $this->addNamespacedName($node); |
||
| 47 | } |
||
| 48 | } elseif ($node instanceof Stmt\Interface_) { |
||
| 49 | foreach ($node->extends as &$interface) { |
||
| 50 | $interface = $this->resolveClassName($interface); |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->addNamespacedName($node); |
||
| 54 | } elseif ($node instanceof Stmt\Trait_) { |
||
| 55 | $this->addNamespacedName($node); |
||
| 56 | } elseif ($node instanceof Stmt\Function_) { |
||
| 57 | $this->addNamespacedName($node); |
||
| 58 | $this->resolveSignature($node); |
||
| 59 | } elseif ($node instanceof Stmt\ClassMethod |
||
| 60 | || $node instanceof Expr\Closure |
||
| 61 | ) { |
||
| 62 | $this->resolveSignature($node); |
||
| 63 | } elseif ($node instanceof Stmt\Const_) { |
||
| 64 | foreach ($node->consts as $const) { |
||
| 65 | $this->addNamespacedName($const); |
||
| 66 | } |
||
| 67 | } elseif ($node instanceof Expr\StaticCall |
||
| 68 | || $node instanceof Expr\StaticPropertyFetch |
||
| 69 | || $node instanceof Expr\ClassConstFetch |
||
| 70 | || $node instanceof Expr\New_ |
||
| 71 | || $node instanceof Expr\Instanceof_ |
||
| 72 | ) { |
||
| 73 | if ($node->class instanceof Name) { |
||
| 74 | $node->class = $this->resolveClassName($node->class); |
||
| 75 | } |
||
| 76 | } elseif ($node instanceof Stmt\Catch_) { |
||
| 77 | $node->type = $this->resolveClassName($node->type); |
||
| 78 | } elseif ($node instanceof Expr\FuncCall) { |
||
| 79 | if ($node->name instanceof Name) { |
||
| 80 | $node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION); |
||
| 81 | } |
||
| 82 | } elseif ($node instanceof Expr\ConstFetch) { |
||
| 83 | $node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_CONSTANT); |
||
| 84 | } elseif ($node instanceof Stmt\TraitUse) { |
||
| 85 | foreach ($node->traits as &$trait) { |
||
| 86 | $trait = $this->resolveClassName($trait); |
||
| 87 | } |
||
| 88 | |||
| 89 | foreach ($node->adaptations as $adaptation) { |
||
| 90 | if (null !== $adaptation->trait) { |
||
| 91 | $adaptation->trait = $this->resolveClassName($adaptation->trait); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) { |
||
| 95 | foreach ($adaptation->insteadof as &$insteadof) { |
||
| 96 | $insteadof = $this->resolveClassName($insteadof); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function resetState(Name $namespace = null) { |
||
| 105 | $this->namespace = $namespace; |
||
| 106 | $this->aliases = array( |
||
| 107 | Stmt\Use_::TYPE_NORMAL => array(), |
||
| 108 | Stmt\Use_::TYPE_FUNCTION => array(), |
||
| 109 | Stmt\Use_::TYPE_CONSTANT => array(), |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) { |
||
| 114 | // Add prefix for group uses |
||
| 115 | $name = $prefix ? Name::concat($prefix, $use->name) : $use->name; |
||
| 116 | // Type is determined either by individual element or whole use declaration |
||
| 117 | $type |= $use->type; |
||
| 118 | |||
| 119 | // Constant names are case sensitive, everything else case insensitive |
||
| 120 | if ($type === Stmt\Use_::TYPE_CONSTANT) { |
||
| 121 | $aliasName = $use->alias; |
||
| 122 | } else { |
||
| 123 | $aliasName = strtolower($use->alias); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (isset($this->aliases[$type][$aliasName])) { |
||
| 127 | $typeStringMap = array( |
||
| 128 | Stmt\Use_::TYPE_NORMAL => '', |
||
| 129 | Stmt\Use_::TYPE_FUNCTION => 'function ', |
||
| 130 | Stmt\Use_::TYPE_CONSTANT => 'const ', |
||
| 131 | ); |
||
| 132 | |||
| 133 | throw new Error( |
||
| 134 | sprintf( |
||
| 135 | 'Cannot use %s%s as %s because the name is already in use', |
||
| 136 | $typeStringMap[$type], $name, $use->alias |
||
| 137 | ), |
||
| 138 | $use->getLine() |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->aliases[$type][$aliasName] = $name; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */ |
||
| 146 | private function resolveSignature($node) { |
||
| 156 | |||
| 157 | protected function resolveClassName(Name $name) { |
||
| 158 | // don't resolve special class names |
||
| 159 | if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) { |
||
| 189 | |||
| 190 | protected function resolveOtherName(Name $name, $type) { |
||
| 225 | |||
| 226 | protected function addNamespacedName(Node $node) { |
||
| 233 | } |
||
| 234 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: