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 |
||
32 | abstract class AbstractModule implements ModuleInterface, ConfigProviderInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var string The Module name. |
||
36 | */ |
||
37 | protected $name; |
||
38 | |||
39 | /** |
||
40 | * @var \ReflectionObject |
||
41 | */ |
||
42 | protected $reflected; |
||
43 | |||
44 | /** |
||
45 | * @todo Add inline documentation. |
||
46 | * |
||
47 | * @var null |
||
48 | */ |
||
49 | protected $config; |
||
50 | |||
51 | /** |
||
52 | * Configuration loader. |
||
53 | * |
||
54 | * @var null|\PPI\Framework\Config\ConfigLoader |
||
55 | */ |
||
56 | protected $configLoader; |
||
57 | |||
58 | /** |
||
59 | * @todo Add inline documentation. |
||
60 | * |
||
61 | * @var null |
||
62 | */ |
||
63 | protected $routes; |
||
64 | |||
65 | /** |
||
66 | * @todo Add inline documentation. |
||
67 | * |
||
68 | * @var null |
||
69 | */ |
||
70 | protected $services; |
||
71 | |||
72 | /** |
||
73 | * Load up our routes. |
||
74 | * |
||
75 | * @param string $path |
||
76 | * |
||
77 | * @return \Symfony\Component\Routing\RouteCollection |
||
78 | */ |
||
79 | protected function loadSymfonyRoutes($path) |
||
91 | |||
92 | /** |
||
93 | * Load up our routes. |
||
94 | * |
||
95 | * @deprecated Please use loadSymfonyRoutes instead |
||
96 | * |
||
97 | * @param string $path |
||
98 | * |
||
99 | * @return \Symfony\Component\Routing\RouteCollection |
||
100 | */ |
||
101 | protected function loadYamlRoutes($path) |
||
105 | |||
106 | /** |
||
107 | * @param string $path |
||
108 | * |
||
109 | * @throws \Exception when the included routes file doesn't return an AuraRouter back |
||
110 | * |
||
111 | * @return AuraRouter |
||
112 | */ |
||
113 | protected function loadLaravelRoutes($path) |
||
122 | |||
123 | /** |
||
124 | * @param $path |
||
125 | * |
||
126 | * @return FastRouteWrapper |
||
127 | */ |
||
128 | protected function loadFastRouteRoutes($path) |
||
162 | |||
163 | /** |
||
164 | * @todo - move part of this into AuraRouterWrapper->load() |
||
165 | * @todo - consider adding a setModuleName() to the AuraRouterWrapper instead of _module to each route. |
||
166 | * |
||
167 | * @param string $path |
||
168 | * |
||
169 | * @throws \Exception when the included routes file doesn't return an AuraRouter back |
||
170 | * |
||
171 | * @return AuraRouter |
||
172 | */ |
||
173 | protected function loadAuraRoutes($path) |
||
197 | |||
198 | /** |
||
199 | * Load up our config results from the specific yaml file. |
||
200 | * |
||
201 | * @param string $path |
||
202 | * |
||
203 | * @return array |
||
204 | * |
||
205 | * @deprecated since version 2.1, to be removed in 2.2. Use "loadConfig()" instead. |
||
206 | */ |
||
207 | protected function loadYamlConfig($path) |
||
211 | |||
212 | /** |
||
213 | * Set services for our module. |
||
214 | * |
||
215 | * @param string $services |
||
216 | * |
||
217 | * @return Module |
||
218 | */ |
||
219 | public function setServices($services) |
||
225 | |||
226 | /** |
||
227 | * Get the services. |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | public function getServices() |
||
235 | |||
236 | /** |
||
237 | * Get a particular service. |
||
238 | * |
||
239 | * @param string $serviceName |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public function getService($serviceName) |
||
247 | |||
248 | /** |
||
249 | * Loads a configuration file (PHP, YAML) or PHP array. |
||
250 | * |
||
251 | * @param string $resource The resource |
||
252 | * @param null|string $type The resource type |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | public function loadConfig($resource, $type = null) |
||
260 | |||
261 | /** |
||
262 | * Loads and merges the configuration. |
||
263 | * |
||
264 | * @param mixed $resources |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function mergeConfig($resources) |
||
277 | |||
278 | /** |
||
279 | * Set the module name. |
||
280 | * |
||
281 | * @param string $Name |
||
282 | * |
||
283 | * @return $this |
||
284 | */ |
||
285 | public function setName($Name) |
||
291 | |||
292 | /** |
||
293 | * Returns the module name. Defaults to the module namespace stripped of backslashes. |
||
294 | * |
||
295 | * @return string The Module name |
||
296 | */ |
||
297 | public function getName() |
||
307 | |||
308 | /** |
||
309 | * Gets the Module namespace. |
||
310 | * |
||
311 | * @return string The Module namespace |
||
312 | * |
||
313 | * @api |
||
314 | */ |
||
315 | public function getNamespace() |
||
323 | |||
324 | /** |
||
325 | * Gets the Module directory path. |
||
326 | * |
||
327 | * @return string The Module absolute path |
||
328 | * |
||
329 | * @api |
||
330 | */ |
||
331 | public function getPath() |
||
339 | |||
340 | /** |
||
341 | * Returns configuration to merge with application configuration. |
||
342 | * |
||
343 | * @return array|\Traversable |
||
344 | */ |
||
345 | public function getConfig() |
||
349 | |||
350 | /** |
||
351 | * Returns the default location of where Command classes are registered. |
||
352 | * Override this in your child module if it differs from this default convention. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getCommandsPath() |
||
360 | |||
361 | /** |
||
362 | * Finds and registers Commands. |
||
363 | * |
||
364 | * Override this method if your module commands do not follow the conventions: |
||
365 | * |
||
366 | * * Commands are in the 'Command' sub-directory |
||
367 | * * Commands extend PPI\Framework\Console\Command\AbstractCommand |
||
368 | * |
||
369 | * @param Application $application An Application instance |
||
370 | */ |
||
371 | public function registerCommands(Application $application) |
||
372 | { |
||
373 | if (!is_dir($dir = $this->getCommandsPath())) { |
||
374 | return; |
||
375 | } |
||
376 | |||
377 | $finder = new Finder(); |
||
378 | $finder->files()->name('*Command.php')->in($dir); |
||
379 | |||
380 | $prefix = $this->getNamespace() . '\\Command'; |
||
381 | foreach ($finder as $file) { |
||
382 | $ns = $prefix; |
||
383 | if ($relativePath = $file->getRelativePath()) { |
||
384 | $ns .= '\\' . strtr($relativePath, '/', '\\'); |
||
385 | } |
||
386 | $r = new \ReflectionClass($ns . '\\' . $file->getBasename('.php')); |
||
387 | if ($r->isSubclassOf('PPI\Framework\\Console\\Command\\AbstractCommand') && !$r->isAbstract()) { |
||
388 | $application->add($r->newInstance()); |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns a ConfigLoader instance. |
||
395 | * |
||
396 | * @return \PPI\Framework\Config\ConfigLoader |
||
397 | */ |
||
398 | protected function getConfigLoader() |
||
406 | } |
||
407 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..