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 |
||
30 | class Manager |
||
31 | { |
||
32 | use \Jaxon\Utils\Traits\Manager; |
||
33 | use \Jaxon\Utils\Traits\Config; |
||
34 | use \Jaxon\Utils\Traits\Cache; |
||
35 | use \Jaxon\Utils\Traits\Event; |
||
36 | use \Jaxon\Utils\Traits\Translator; |
||
37 | |||
38 | /** |
||
39 | * All plugins, indexed by priority |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $aPlugins = []; |
||
44 | |||
45 | /** |
||
46 | * Request plugins, indexed by name |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | private $aRequestPlugins = []; |
||
51 | |||
52 | /** |
||
53 | * Response plugins, indexed by name |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $aResponsePlugins = []; |
||
58 | |||
59 | /** |
||
60 | * An array of package names |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | private $aPackages = []; |
||
65 | |||
66 | /** |
||
67 | * Get the request plugins |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getRequestPlugins() |
||
75 | |||
76 | /** |
||
77 | * Get the response plugins |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getResponsePlugins() |
||
85 | |||
86 | /** |
||
87 | * Get the package plugins |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getPackages() |
||
95 | |||
96 | /** |
||
97 | * Inserts an entry into an array given the specified priority number |
||
98 | * |
||
99 | * If a plugin already exists with the given priority, the priority is automatically incremented until a free spot is found. |
||
100 | * The plugin is then inserted into the empty spot in the array. |
||
101 | * |
||
102 | * @param Plugin $xPlugin An instance of a plugin |
||
103 | * @param integer $nPriority The desired priority, used to order the plugins |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | private function setPluginPriority(Plugin $xPlugin, $nPriority) |
||
117 | |||
118 | /** |
||
119 | * Register a plugin |
||
120 | * |
||
121 | * Below is a table for priorities and their description: |
||
122 | * - 0 thru 999: Plugins that are part of or extensions to the jaxon core |
||
123 | * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
||
124 | * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
||
125 | * |
||
126 | * @param Plugin $xPlugin An instance of a plugin |
||
127 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | public function registerPlugin(Plugin $xPlugin, $nPriority = 1000) |
||
167 | |||
168 | /** |
||
169 | * Register a package |
||
170 | * |
||
171 | * @param string $sPackageClass The package class name |
||
172 | * @param Closure $xClosure A closure to create package instance |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function registerPackage(string $sPackageClass, Closure $xClosure) |
||
181 | |||
182 | /** |
||
183 | * Register a function, event or callable object |
||
184 | * |
||
185 | * Call the request plugin with the $sType defined as name. |
||
186 | * |
||
187 | * @param string $sType The type of request handler being registered |
||
188 | * @param string $sCallable The callable entity being registered |
||
189 | * @param array|string $aOptions The associated options |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function register($sType, $sCallable, $aOptions = []) |
||
211 | |||
212 | /** |
||
213 | * Read and set Jaxon options from a JSON config file |
||
214 | * |
||
215 | * @param Config $xAppConfig The config options |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public function registerFromConfig($xAppConfig) |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Find the specified response plugin by name and return a reference to it if one exists |
||
301 | * |
||
302 | * @param string $sName The name of the plugin |
||
303 | * |
||
304 | * @return \Jaxon\Plugin\Response |
||
305 | */ |
||
306 | public function getResponsePlugin($sName) |
||
314 | |||
315 | /** |
||
316 | * Find the specified request plugin by name and return a reference to it if one exists |
||
317 | * |
||
318 | * @param string $sName The name of the plugin |
||
319 | * |
||
320 | * @return \Jaxon\Plugin\Request |
||
321 | */ |
||
322 | public function getRequestPlugin($sName) |
||
330 | } |
||
331 |
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.