Module   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 4 1
B onBootstrap() 0 37 4
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoCanonicalRedirect for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoCanonicalRedirect;
11
12
use WebinoCanonicalRedirect\Options\ModuleOptions;
13
use WebinoCanonicalRedirect\Uri\Canonicalizer;
14
use Zend\Http\Request as HttpRequest;
15
use Zend\EventManager\EventInterface;
16
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
17
use Zend\ModuleManager\Feature\ConfigProviderInterface;
18
use Zend\Mvc\MvcEvent;
19
20
/**
21
 * WebinoCanonicalRedirect module
22
 */
23
class Module implements
24
    BootstrapListenerInterface,
25
    ConfigProviderInterface
26
{
27
    /**
28
     * Redirect to the canonicalized URI path
29
     *
30
     * @param EventInterface|MvcEvent $event
31
     * @return void
32
     */
33
    public function onBootstrap(EventInterface $event)
34
    {
35
        $app      = $event->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...
36
        $services = $app->getServiceManager();
37
        $options  = $services->get(ModuleOptions::class);
38
39
        if (!$options->isEnabled()
40
            || !($event->getRequest() instanceof HttpRequest)
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 getRequest() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent, Zend\View\ViewEvent.

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...
41
        ) {
42
            return;
43
        }
44
45
        $uri = $services->get(Canonicalizer::class);
46
        $uri
47
            ->www($options->useWww())
48
            ->trailingSlash($options->useSlash());
49
50
        if (!$uri->isCanonicalized()) {
51
            return;
52
        }
53
54
        $event->stopPropagation();
55
56
        $response = $event->getResponse();
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 getResponse() does only exist in the following implementations of said interface: Zend\Mvc\MvcEvent, Zend\Mvc\ResponseSender\SendResponseEvent, Zend\View\ViewEvent.

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...
57
        /** @var \Zend\Http\Response $response */
58
        $response
59
            ->setStatusCode(301)
60
            ->getHeaders()
61
            ->addHeaderLine('Location', $uri);
62
63
        unset($uri);
64
65
        $app->getEventManager()->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $event) {
66
            $event->stopPropagation();
67
            return $event->getResponse();
68
        }, 9999999);
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getConfig()
75
    {
76
        return include __DIR__ . '/../../config/module.config.php';
77
    }
78
}
79