Module::loadTheme()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
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 LearnZF2Themes;
20
21
use LearnZF2Themes\Service\ReloadService;
22
use Zend\EventManager\EventInterface;
23
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
24
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
25
use Zend\ModuleManager\Feature\ConfigProviderInterface;
26
27
class Module implements AutoloaderProviderInterface, BootstrapListenerInterface, ConfigProviderInterface
28
{
29
    /**
30
     * @var \Zend\getServiceManager\ServiceManager
31
     */
32
    private $service = null;
33
34
    /**
35
     * Listen to the bootstrap event.
36
     *
37
     * @param EventInterface $event
38
     */
39
    public function onBootstrap(EventInterface $event)
40
    {
41
        $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...
42
        $this->service = $app->getServiceManager();
43
        $eventManager = $app->getEventManager();
44
        $sharedEventManager = $eventManager->getSharedManager();
45
46
        $eventManager->attach('render', [$this, 'loadTheme'], 100);
47
        $sharedEventManager->attach(ReloadService::class, 'reload', [$this, 'reloadConfig'], 100);
48
    }
49
50
    /**
51
     * Listen for theme change and override Config.
52
     */
53
    public function reloadConfig()
54
    {
55
        $request = $this->service->get('Request');
56
57
        $config = $this->service->get('Config');
58
        $this->service->setAllowOverride(true);
59
        $config['theme']['name'] = $request->getPost()['themeName'];
60
        $this->service->setService('Config', $config);
61
        $this->service->setAllowOverride(false);
62
    }
63
64
    /**
65
     * Setup theme.
66
     */
67
    public function loadTheme()
68
    {
69
        return $this->service->get('initThemes');
70
    }
71
72
    /**
73
     * @return array|\Traversable
74
     */
75
    public function getConfig()
76
    {
77
        return include __DIR__.'/config/module.config.php';
78
    }
79
80
    public function getAutoloaderConfig()
81
    {
82
        return [
83
            'Zend\Loader\StandardAutoloader' => [
84
                'namespaces' => [
85
                    __NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
86
                ],
87
            ],
88
        ];
89
    }
90
}
91