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 |
||
9 | class Addon |
||
10 | { |
||
11 | /** |
||
12 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
13 | * @param string $name |
||
14 | * @param string $path |
||
15 | * |
||
16 | * @return static |
||
17 | */ |
||
18 | 6 | public static function create($app, $name, $path) |
|
26 | |||
27 | /** |
||
28 | * @param string $path |
||
29 | * @param string $name |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | 6 | protected static function loadAddonConfig($path, $name) |
|
48 | |||
49 | /** |
||
50 | * @var \Illuminate\Contracts\Foundation\Application |
||
51 | */ |
||
52 | protected $app; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $name; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $path; |
||
63 | |||
64 | /** |
||
65 | * @var \Illuminate\Contracts\Config\Repository |
||
66 | */ |
||
67 | protected $config; |
||
68 | |||
69 | /** |
||
70 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
71 | * @param string $name |
||
72 | * @param string $path |
||
73 | * @param array $config |
||
74 | */ |
||
75 | 12 | public function __construct($app, $name, $path, array $config) |
|
83 | |||
84 | /** |
||
85 | * get name. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 7 | public function name() |
|
93 | |||
94 | /** |
||
95 | * get fullpath. |
||
96 | * |
||
97 | * @param string $path |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 5 | public function path($path = null) |
|
109 | |||
110 | /** |
||
111 | * get relative path. |
||
112 | * |
||
113 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 5 | public function relativePath(Application $app) |
|
121 | |||
122 | /** |
||
123 | * get version. |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | 2 | public function version() |
|
131 | |||
132 | /** |
||
133 | * get PHP namespace. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 7 | public function phpNamespace() |
|
141 | |||
142 | /** |
||
143 | * get config value. |
||
144 | * |
||
145 | * @param string $key |
||
146 | * @param mixed $default |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | 9 | public function config($key, $default = null) |
|
154 | |||
155 | /** |
||
156 | * set config value. |
||
157 | * |
||
158 | * @param string $key |
||
159 | * @param mixed $value |
||
160 | */ |
||
161 | public function setConfig($key, $value) |
||
165 | |||
166 | /** |
||
167 | * Get a lang resource name |
||
168 | * |
||
169 | * @param string $resource |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | 1 | public function transName($resource) |
|
177 | |||
178 | /** |
||
179 | * Translate the given message. |
||
180 | * |
||
181 | * @param string $id |
||
182 | * @param array $parameters |
||
183 | * @param string $domain |
||
184 | * @param string $locale |
||
185 | * @return string |
||
186 | */ |
||
187 | 1 | View Code Duplication | public function trans() |
194 | |||
195 | /** |
||
196 | * Translates the given message based on a count. |
||
197 | * |
||
198 | * @param string $id |
||
199 | * @param int $number |
||
200 | * @param array $parameters |
||
201 | * @param string $domain |
||
202 | * @param string $locale |
||
203 | * @return string |
||
204 | */ |
||
205 | 1 | View Code Duplication | public function transChoice() |
212 | |||
213 | /** |
||
214 | * Get a view resource name |
||
215 | * |
||
216 | * @param string $resource |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public function viewName($resource) |
||
224 | |||
225 | /** |
||
226 | * @param string $view |
||
227 | * @param array $data |
||
228 | * @param array $mergeData |
||
229 | * |
||
230 | * @return \Illuminate\View\View |
||
231 | */ |
||
232 | public function view($view, $data = [], $mergeData = []) |
||
236 | |||
237 | /** |
||
238 | * Get a spec resource name |
||
239 | * |
||
240 | * @param string $resource |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function specName($resource) |
||
248 | |||
249 | /** |
||
250 | * Get spec. |
||
251 | * |
||
252 | * @param string $path |
||
253 | * |
||
254 | * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
||
255 | */ |
||
256 | public function spec($path) |
||
260 | } |
||
261 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.