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 |
||
28 | class UnusedFormalParameter extends AbstractLocalVariable implements FunctionAware, MethodAware |
||
29 | { |
||
30 | /** |
||
31 | * Collected ast nodes. |
||
32 | * |
||
33 | * @var \PHPMD\Node\ASTNode[] |
||
34 | */ |
||
35 | private $nodes = array(); |
||
36 | |||
37 | /** |
||
38 | * This method checks that all parameters of a given function or method are |
||
39 | * used at least one time within the artifacts body. |
||
40 | * |
||
41 | * @param \PHPMD\AbstractNode $node |
||
42 | * @return void |
||
43 | */ |
||
44 | 33 | public function apply(AbstractNode $node) |
|
45 | { |
||
46 | 33 | if ($this->isAbstractMethod($node)) { |
|
47 | 2 | return; |
|
48 | } |
||
49 | |||
50 | // Magic methods should be ignored as invalid declarations are picked up by PHP. |
||
51 | 31 | if ($this->isMagicMethod($node)) { |
|
|
|||
52 | 1 | return; |
|
53 | } |
||
54 | |||
55 | 30 | if ($this->isInheritedSignature($node)) { |
|
56 | 2 | return; |
|
57 | } |
||
58 | |||
59 | 28 | if ($this->isNotDeclaration($node)) { |
|
60 | 3 | return; |
|
61 | } |
||
62 | |||
63 | 25 | $this->nodes = array(); |
|
64 | |||
65 | 25 | $this->collectParameters($node); |
|
66 | 25 | $this->removeUsedParameters($node); |
|
67 | |||
68 | 25 | $exceptions = $this->getExceptionsList(); |
|
69 | |||
70 | 25 | View Code Duplication | foreach ($this->nodes as $node) { |
71 | 7 | if (in_array(substr($node->getImage(), 1), $exceptions)) { |
|
72 | continue; |
||
73 | } |
||
74 | 7 | $this->addViolation($node, array($node->getImage())); |
|
75 | } |
||
76 | 25 | } |
|
77 | |||
78 | /** |
||
79 | * Gets array of exceptions from property |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | 25 | private function getExceptionsList() |
|
84 | { |
||
85 | try { |
||
86 | 25 | $exceptions = $this->getStringProperty('exceptions'); |
|
87 | 23 | } catch (\OutOfBoundsException $e) { |
|
88 | 23 | $exceptions = ''; |
|
89 | } |
||
90 | |||
91 | 25 | return explode(',', $exceptions); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns <b>true</b> when the given node is an abstract method. |
||
96 | * |
||
97 | * @param \PHPMD\AbstractNode $node |
||
98 | * @return boolean |
||
99 | */ |
||
100 | 33 | private function isAbstractMethod(AbstractNode $node) |
|
101 | { |
||
102 | 33 | if ($node instanceof MethodNode) { |
|
103 | 30 | return $node->isAbstract(); |
|
104 | } |
||
105 | 3 | return false; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Returns <b>true</b> when the given node is method with signature declared as inherited using |
||
110 | * {@inheritdoc} annotation. |
||
111 | * |
||
112 | * @param \PHPMD\AbstractNode $node |
||
113 | * @return boolean |
||
114 | */ |
||
115 | 30 | private function isInheritedSignature(AbstractNode $node) |
|
116 | { |
||
117 | 30 | if ($node instanceof MethodNode) { |
|
118 | 27 | return preg_match('/\@inheritdoc/i', $node->getDocComment()); |
|
119 | } |
||
120 | 3 | return false; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns <b>true</b> when the given node is a magic method signature |
||
125 | * @param AbstractNode $node |
||
126 | * @return boolean |
||
127 | */ |
||
128 | 31 | private function isMagicMethod(AbstractNode $node) |
|
129 | { |
||
130 | 31 | static $names = array( |
|
131 | 'call', |
||
132 | 'callStatic', |
||
133 | 'get', |
||
134 | 'set', |
||
135 | 'isset', |
||
136 | 'unset', |
||
137 | 'set_state' |
||
138 | ); |
||
139 | |||
140 | 31 | if ($node instanceof MethodNode) { |
|
141 | 28 | return preg_match('/\__(?:' . implode("|", $names) . ')/i', $node->getName()); |
|
142 | } |
||
143 | 3 | return false; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Tests if the given <b>$node</b> is a method and if this method is also |
||
148 | * the initial declaration. |
||
149 | * |
||
150 | * @param \PHPMD\AbstractNode $node |
||
151 | * @return boolean |
||
152 | * @since 1.2.1 |
||
153 | */ |
||
154 | 28 | private function isNotDeclaration(AbstractNode $node) |
|
161 | |||
162 | /** |
||
163 | * This method extracts all parameters for the given function or method node |
||
164 | * and it stores the parameter images in the <b>$_images</b> property. |
||
165 | * |
||
166 | * @param \PHPMD\AbstractNode $node |
||
167 | * @return void |
||
168 | */ |
||
169 | 25 | private function collectParameters(AbstractNode $node) |
|
181 | |||
182 | /** |
||
183 | * This method collects all local variables in the body of the currently |
||
184 | * analyzed method or function and removes those parameters that are |
||
185 | * referenced by one of the collected variables. |
||
186 | * |
||
187 | * @param \PHPMD\AbstractNode $node |
||
188 | * @return void |
||
189 | */ |
||
190 | 25 | private function removeUsedParameters(AbstractNode $node) |
|
228 | } |
||
229 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: