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 |
||
10 | class Addon |
||
11 | { |
||
12 | /** |
||
13 | * @param string $path |
||
14 | * |
||
15 | * @return static |
||
16 | */ |
||
17 | 9 | public static function create($path) |
|
27 | |||
28 | /** |
||
29 | * @param string $path |
||
30 | * @param string $name |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | 9 | protected static function loadAddonConfig($path, $name) |
|
35 | { |
||
36 | 9 | if (file_exists($path.'/addon.php')) { |
|
37 | 8 | $config = require $path.'/addon.php'; |
|
38 | } else { |
||
39 | 1 | 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 string |
||
52 | */ |
||
53 | protected $name; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $path; |
||
59 | |||
60 | /** |
||
61 | * @var \Illuminate\Contracts\Config\Repository |
||
62 | */ |
||
63 | protected $config; |
||
64 | |||
65 | /** |
||
66 | * @var \Illuminate\Contracts\Foundation\Application |
||
67 | */ |
||
68 | protected $app; |
||
69 | |||
70 | /** |
||
71 | * @param string $name |
||
72 | * @param string $path |
||
73 | * @param array $config |
||
74 | */ |
||
75 | 16 | public function __construct($name, $path, array $config) |
|
82 | |||
83 | /** |
||
84 | * get name. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 11 | public function name() |
|
92 | |||
93 | /** |
||
94 | * get fullpath. |
||
95 | * |
||
96 | * @param string $path |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 8 | public function path($path = null) |
|
108 | |||
109 | /** |
||
110 | * get relative path. |
||
111 | * |
||
112 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 8 | public function relativePath(Application $app) |
|
120 | |||
121 | /** |
||
122 | * get version. |
||
123 | * |
||
124 | * @return int |
||
125 | */ |
||
126 | 2 | public function version() |
|
130 | |||
131 | /** |
||
132 | * get PHP namespace. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 8 | public function phpNamespace() |
|
140 | |||
141 | /** |
||
142 | * get config value. |
||
143 | * |
||
144 | * @param string $key |
||
145 | * @param mixed $default |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 11 | public function config($key, $default = null) |
|
153 | |||
154 | /** |
||
155 | * Get a lang resource name |
||
156 | * |
||
157 | * @param string $resource |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 1 | public function transName($resource) |
|
165 | |||
166 | /** |
||
167 | * Translate the given message. |
||
168 | * |
||
169 | * @param string $id |
||
|
|||
170 | * @param array $parameters |
||
171 | * @param string $domain |
||
172 | * @param string $locale |
||
173 | * @return string |
||
174 | */ |
||
175 | 1 | View Code Duplication | public function trans() |
182 | |||
183 | /** |
||
184 | * Translates the given message based on a count. |
||
185 | * |
||
186 | * @param string $id |
||
187 | * @param int $number |
||
188 | * @param array $parameters |
||
189 | * @param string $domain |
||
190 | * @param string $locale |
||
191 | * @return string |
||
192 | */ |
||
193 | 1 | View Code Duplication | public function transChoice() |
200 | |||
201 | /** |
||
202 | * Get a view resource name |
||
203 | * |
||
204 | * @param string $resource |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function viewName($resource) |
||
212 | |||
213 | /** |
||
214 | * @param string $view |
||
215 | * @param array $data |
||
216 | * @param array $mergeData |
||
217 | * |
||
218 | * @return \Illuminate\View\View |
||
219 | */ |
||
220 | public function view($view, $data = [], $mergeData = []) |
||
224 | |||
225 | /** |
||
226 | * Get a spec resource name |
||
227 | * |
||
228 | * @param string $resource |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function specName($resource) |
||
236 | |||
237 | /** |
||
238 | * Get spec. |
||
239 | * |
||
240 | * @param string $path |
||
241 | * |
||
242 | * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
||
243 | */ |
||
244 | public function spec($path) |
||
248 | |||
249 | /** |
||
250 | * register addon. |
||
251 | * |
||
252 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
253 | */ |
||
254 | 3 | public function register(Application $app) |
|
255 | { |
||
256 | 3 | $this->app = $app; |
|
257 | |||
258 | // prepare helper functions |
||
259 | 3 | $this->loadFiles($this->config('addon.files', [])); |
|
260 | |||
261 | // load config |
||
262 | 3 | $this->loadConfigurationFiles($this->path($this->config('addon.paths.config', 'config'))); |
|
263 | |||
264 | // regist service providers |
||
265 | 3 | $providers = $this->config('addon.providers', []); |
|
266 | 3 | foreach ($providers as $provider) { |
|
267 | 1 | $app->register($provider); |
|
268 | } |
||
269 | 3 | } |
|
270 | |||
271 | /** |
||
272 | * boot addon. |
||
273 | * |
||
274 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
275 | */ |
||
276 | 2 | public function boot(Application $app) |
|
280 | |||
281 | /** |
||
282 | * Load the configuration items from all of the files. |
||
283 | * |
||
284 | * @param string $directoryPath |
||
285 | */ |
||
286 | 3 | protected function loadConfigurationFiles($directoryPath) |
|
292 | |||
293 | /** |
||
294 | * Get all of the configuration files for the directory. |
||
295 | * |
||
296 | * @param string $directoryPath |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 3 | protected function getConfigurationFiles($directoryPath) |
|
313 | |||
314 | /** |
||
315 | * load addon initial script files. |
||
316 | * |
||
317 | * @param array $files |
||
318 | */ |
||
319 | 3 | protected function loadFiles(array $files) |
|
334 | /** |
||
335 | * Register the package's component namespaces. |
||
336 | * |
||
337 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
338 | */ |
||
339 | 2 | protected function registerPackage(Application $app) |
|
358 | } |
||
359 |
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.