Completed
Pull Request — master (#49)
by Abdul Malik
02:01
created

Module   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 6
c 6
b 1
f 1
lcom 0
cbo 2
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 4 1
A getModuleDependencies() 0 4 1
B onBootstrap() 0 22 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 39 and the first side effect is on line 31.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace SanSessionToolbar;
20
21
use Zend\EventManager\EventInterface;
22
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
23
use Zend\ModuleManager\Feature\ConfigProviderInterface;
24
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
25
use Zend\Session\Container;
26
use Zend\Stdlib\SplQueue;
27
28
/**
29
 * @author Abdul Malik Ikhsan <[email protected]>
30
 */
31
class Module implements
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
32
    BootstrapListenerInterface,
33
    ConfigProviderInterface,
34
    DependencyIndicatorInterface
35
{
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function onBootstrap(EventInterface $e)
40
    {
41
        $container = new Container('FlashMessenger');
42
        // need to be saved first, as when session will be read on next process, session already lost
43
        $reCreateFlash = $container->getArrayCopy();
44
45
        $app = $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: ZendDeveloperTools\ProfilerEvent, 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...
46
        $services = $app->getServiceManager();
47
48
        $flash = $services->get('ControllerPluginManager')->get('flashMessenger');
49
        foreach ($reCreateFlash as $key => $row) {
50
            if ($row instanceof SplQueue) {
51
                $flashPerNameSpace = $flash->setNamespace($key);
52
                $valuesMessage = array();
53
                foreach ($row->toArray() as $keyArray => $rowArray) {
54
                    $flashPerNameSpace->addMessage($rowArray);
55
                    $valuesMessage[] = $rowArray;
56
                }
57
                $container->offsetSet($key, $valuesMessage);
58
            }
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getConfig()
66
    {
67
        return include __DIR__.'/../config/module.config.php';
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getModuleDependencies()
74
    {
75
        return array('ZendDeveloperTools');
76
    }
77
}
78