Module   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 53
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onBootstrap() 0 7 1
A getConfig() 0 4 1
A getAutoloaderConfig() 0 10 1
A getConsoleUsage() 0 8 1
1
<?php
2
3
namespace ZfSnapGeoip;
4
5
use Zend\Console\Adapter\AdapterInterface;
6
use Zend\EventManager\EventInterface;
7
use Zend\ModuleManager\Feature\AutoloaderProviderInterface as Autoloader;
8
use Zend\ModuleManager\Feature\BootstrapListenerInterface as BootstrapListener;
9
use Zend\ModuleManager\Feature\ConfigProviderInterface as Config;
10
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface as ConsoleUsage;
11
12
/**
13
 * Geoip module
14
 *
15
 * @author Witold Wasiczko <[email protected]>
16
 */
17
class Module implements ConsoleUsage, Config, Autoloader, BootstrapListener
18
{
19
20
    const CONSOLE_GEOIP_DOWNLOAD = 'geoip download [--no-clobber] [-q]';
21
22
    /**
23
     * @return array
24
     */
25
    public function getConfig()
26
    {
27
        return include __DIR__ . '/../../config/module.config.php';
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function getAutoloaderConfig()
34
    {
35
        return [
36
            'Zend\Loader\StandardAutoloader' => [
37
                'namespaces' => [
38
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
39
                ],
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @param AdapterInterface $console
46
     * @return array
47
     *
48
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
49
     */
50
    public function getConsoleUsage(AdapterInterface $console)
51
    {
52
        return ['Manage GeoIP database',
53
            self::CONSOLE_GEOIP_DOWNLOAD => 'Downloads the newest GeoIP db',
54
            ['--no-clobber', 'Don\'t overwrite an existing db file'],
55
            ['-q', 'Turn off output'],
56
        ];
57
    }
58
59
    /**
60
     * @param \Zend\Mvc\MvcEvent $event
61
     */
62
    public function onBootstrap(EventInterface $event)
63
    {
64
        $serviceManager = $event->getApplication()->getServiceManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\EventManager\EventInterface as the method getApplication() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
65
        $config = $serviceManager->get('config');
66
67
        require_once $config['maxmind']['timezone_function_path'];
68
    }
69
}
70