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 Installer |
||
11 | { |
||
12 | /** |
||
13 | * The module name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * The version of module being installed. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $version; |
||
25 | |||
26 | /** |
||
27 | * The module repository instance. |
||
28 | * @var \Nwidart\Modules\Contracts\RepositoryInterface |
||
29 | */ |
||
30 | protected $repository; |
||
31 | |||
32 | /** |
||
33 | * The console command instance. |
||
34 | * |
||
35 | * @var \Illuminate\Console\Command |
||
36 | */ |
||
37 | protected $console; |
||
38 | |||
39 | /** |
||
40 | * The destionation path. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $path; |
||
45 | |||
46 | /** |
||
47 | * The process timeout. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $timeout = 3360; |
||
52 | /** |
||
53 | * @var null|string |
||
54 | */ |
||
55 | private $type; |
||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $tree; |
||
60 | |||
61 | /** |
||
62 | * The constructor. |
||
63 | * |
||
64 | * @param string $name |
||
65 | * @param string $version |
||
66 | * @param string $type |
||
67 | * @param bool $tree |
||
68 | */ |
||
69 | public function __construct($name, $version = null, $type = null, $tree = false) |
||
76 | |||
77 | /** |
||
78 | * Set destination path. |
||
79 | * |
||
80 | * @param string $path |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | public function setPath($path) |
||
90 | |||
91 | /** |
||
92 | * Set the module repository instance. |
||
93 | * @param \Nwidart\Modules\Contracts\RepositoryInterface $repository |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function setRepository(RepositoryInterface $repository) |
||
102 | |||
103 | /** |
||
104 | * Set console command instance. |
||
105 | * |
||
106 | * @param \Illuminate\Console\Command $console |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setConsole(Command $console) |
||
116 | |||
117 | /** |
||
118 | * Set process timeout. |
||
119 | * |
||
120 | * @param int $timeout |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setTimeout($timeout) |
||
130 | |||
131 | /** |
||
132 | * Run the installation process. |
||
133 | * |
||
134 | * @return \Symfony\Component\Process\Process |
||
135 | */ |
||
136 | public function run() |
||
150 | |||
151 | /** |
||
152 | * Get process instance. |
||
153 | * |
||
154 | * @return \Symfony\Component\Process\Process |
||
155 | */ |
||
156 | public function getProcess() |
||
168 | |||
169 | /** |
||
170 | * Get destination path. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getDestinationPath() |
||
182 | |||
183 | /** |
||
184 | * Get git repo url. |
||
185 | * |
||
186 | * @return string|null |
||
187 | */ |
||
188 | public function getRepoUrl() |
||
220 | |||
221 | /** |
||
222 | * Get branch name. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getBranch() |
||
230 | |||
231 | /** |
||
232 | * Get module name. |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getModuleName() |
||
242 | |||
243 | /** |
||
244 | * Get composer package name. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getPackageName() |
||
256 | |||
257 | /** |
||
258 | * Install the module via git. |
||
259 | * |
||
260 | * @return \Symfony\Component\Process\Process |
||
261 | */ |
||
262 | View Code Duplication | public function installViaGit() |
|
273 | |||
274 | /** |
||
275 | * Install the module via git subtree. |
||
276 | * |
||
277 | * @return \Symfony\Component\Process\Process |
||
278 | */ |
||
279 | View Code Duplication | public function installViaSubtree() |
|
291 | |||
292 | /** |
||
293 | * Install the module via composer. |
||
294 | * |
||
295 | * @return \Symfony\Component\Process\Process |
||
296 | */ |
||
297 | public function installViaComposer() |
||
305 | } |
||
306 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.