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:
| 1 | <?php |
||
| 43 | class ScramblePrivateMethod extends ScramblerVisitor |
||
| 44 | { |
||
| 45 | use TrackingRenamerTrait; |
||
| 46 | use SkipTrait; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Before node traversal |
||
| 50 | * |
||
| 51 | * @param Node[] $nodes |
||
| 52 | * @return array |
||
| 53 | **/ |
||
| 54 | public function beforeTraverse(array $nodes) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Check all variable nodes |
||
| 67 | * |
||
| 68 | * @param Node $node |
||
| 69 | * @return void |
||
| 70 | **/ |
||
| 71 | public function enterNode(Node $node) |
||
| 72 | { |
||
| 73 | if ($this->shouldSkip()) { |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | // Scramble calls |
||
| 78 | if ($node instanceof MethodCall || $node instanceof StaticCall) { |
||
| 79 | |||
| 80 | // Node wasn't renamed |
||
| 81 | if (!$this->isRenamed($node->name)) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Scramble usage |
||
| 86 | return $this->scramble($node); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Recursively scan for method calls and see if variables are used |
||
| 92 | * |
||
| 93 | * @param Node[] $nodes |
||
| 94 | * @return void |
||
| 95 | **/ |
||
| 96 | private function variableMethodCallsUsed(array $nodes) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Recursively scan for private method definitions and rename them |
||
| 119 | * |
||
| 120 | * @param Node[] $nodes |
||
| 121 | * @return void |
||
| 122 | **/ |
||
| 123 | View Code Duplication | private function scanMethodDefinitions(array $nodes) |
|
| 143 | } |
||
| 144 |
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: