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 $path |
||
14 | * |
||
15 | * @return static |
||
16 | */ |
||
17 | 8 | public static function create($app, $path) |
|
27 | |||
28 | /** |
||
29 | * @param string $path |
||
30 | * @param string $name |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | 8 | protected static function loadAddonConfig($path, $name) |
|
35 | { |
||
36 | 8 | if (file_exists($path.'/addon.php')) { |
|
37 | 8 | $config = require $path.'/addon.php'; |
|
38 | 8 | } else { |
|
39 | throw new RuntimeException("No such config file for addon '$name', need 'addon.php'."); |
||
40 | } |
||
41 | |||
42 | 8 | $version = array_get($config, 'version', 5); |
|
43 | 8 | if ($version != 5) { |
|
44 | throw new RuntimeException($version.': Illigal addon version.'); |
||
45 | } |
||
46 | |||
47 | 8 | return $config; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @var \Illuminate\Contracts\Foundation\Application |
||
52 | */ |
||
53 | protected $app; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $name; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $path; |
||
64 | |||
65 | /** |
||
66 | * @var \Illuminate\Contracts\Config\Repository |
||
67 | */ |
||
68 | protected $config; |
||
69 | |||
70 | /** |
||
71 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
72 | * @param string $name |
||
73 | * @param string $path |
||
74 | * @param array $config |
||
75 | */ |
||
76 | 16 | public function __construct($app, $name, $path, array $config) |
|
84 | |||
85 | /** |
||
86 | * get name. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 11 | public function name() |
|
94 | |||
95 | /** |
||
96 | * get fullpath. |
||
97 | * |
||
98 | * @param string $path |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 7 | public function path($path = null) |
|
110 | |||
111 | /** |
||
112 | * get relative path. |
||
113 | * |
||
114 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 8 | public function relativePath(Application $app) |
|
122 | |||
123 | /** |
||
124 | * get version. |
||
125 | * |
||
126 | * @return int |
||
127 | */ |
||
128 | 2 | public function version() |
|
132 | |||
133 | /** |
||
134 | * get PHP namespace. |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 8 | public function phpNamespace() |
|
142 | |||
143 | /** |
||
144 | * get config value. |
||
145 | * |
||
146 | * @param string $key |
||
147 | * @param mixed $default |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | 11 | public function config($key, $default = null) |
|
155 | |||
156 | /** |
||
157 | * set config value. |
||
158 | * |
||
159 | * @param string $key |
||
160 | * @param mixed $value |
||
161 | */ |
||
162 | public function setConfig($key, $value) |
||
166 | |||
167 | /** |
||
168 | * Get a lang resource name |
||
169 | * |
||
170 | * @param string $resource |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 1 | public function transName($resource) |
|
178 | |||
179 | /** |
||
180 | * Translate the given message. |
||
181 | * |
||
182 | * @param string $id |
||
|
|||
183 | * @param array $parameters |
||
184 | * @param string $domain |
||
185 | * @param string $locale |
||
186 | * @return string |
||
187 | */ |
||
188 | 1 | View Code Duplication | public function trans() |
195 | |||
196 | /** |
||
197 | * Translates the given message based on a count. |
||
198 | * |
||
199 | * @param string $id |
||
200 | * @param int $number |
||
201 | * @param array $parameters |
||
202 | * @param string $domain |
||
203 | * @param string $locale |
||
204 | * @return string |
||
205 | */ |
||
206 | 1 | View Code Duplication | public function transChoice() |
213 | |||
214 | /** |
||
215 | * Get a view resource name |
||
216 | * |
||
217 | * @param string $resource |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function viewName($resource) |
||
225 | |||
226 | /** |
||
227 | * @param string $view |
||
228 | * @param array $data |
||
229 | * @param array $mergeData |
||
230 | * |
||
231 | * @return \Illuminate\View\View |
||
232 | */ |
||
233 | public function view($view, $data = [], $mergeData = []) |
||
237 | |||
238 | /** |
||
239 | * Get a spec resource name |
||
240 | * |
||
241 | * @param string $resource |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | public function specName($resource) |
||
249 | |||
250 | /** |
||
251 | * Get spec. |
||
252 | * |
||
253 | * @param string $path |
||
254 | * |
||
255 | * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
||
256 | */ |
||
257 | public function spec($path) |
||
261 | } |
||
262 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.