Module   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onBootstrap() 0 15 2
A getConfig() 0 4 1
A getAutoloaderConfig() 0 10 1
1
<?php
2
namespace T4web\DomainModule;
3
4
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
5
use Zend\ModuleManager\Feature\ConfigProviderInterface;
6
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
7
use Zend\EventManager\EventInterface;
8
use Zend\ModuleManager\ModuleManager;
9
use \RuntimeException;
10
11
class Module implements AutoloaderProviderInterface, ConfigProviderInterface, BootstrapListenerInterface
12
{
13
    public function onBootstrap(EventInterface $e)
14
    {
15
        /** @var ModuleManager $moduleManager */
16
        $moduleManager = $e->getApplication()
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...
17
            ->getServiceManager()
18
            ->get('ModuleManager');
19
20
        $baseModule = $moduleManager->getModule('T4webBase');
21
22
        if (!is_null($baseModule)) {
23
            throw new RuntimeException('T4web\DomainModule has conflict with T4webBase module,
24
                for use it you must disable T4webBase module.');
25
        }
26
27
    }
28
29
    public function getConfig($env = null)
30
    {
31
        return include dirname(__DIR__) . '/config/module.config.php';
32
    }
33
34
    public function getAutoloaderConfig()
35
    {
36
        return [
37
            'Zend\Loader\StandardAutoloader' => [
38
                'namespaces' => [
39
                    __NAMESPACE__ => dirname(__DIR__) . '/src',
40
                ],
41
            ],
42
        ];
43
    }
44
}
45