Test Failed
Push — master ( e4b8e4...20d51c )
by Julien
04:19
created

Module::getDirname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Loader;
16
use Phalcon\Mvc\View;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zemit\Mvc\View. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Phalcon\Mvc\ModuleDefinitionInterface;
18
use Zemit\Bootstrap\Config;
19
use Zemit\Di\Injectable;
20
use Zemit\Utils;
21
use Zemit\Url;
22
23
/**
24
 * {@inheritDoc}
25
 */
26
class Module extends Injectable implements ModuleDefinitionInterface
27
{
28
    public const NAME_FRONTEND = 'frontend';
29
    public const NAME_ADMIN = 'admin';
30
    public const NAME_API = 'api';
31
    public const NAME_OAUTH2 = 'oauth2';
32
    
33
    public string $name;
34
    
35
    public Config $config;
36
    
37
    public Dispatcher $dispatcher;
38
    
39
    public Loader $loader;
40
    
41
    public Router $router;
42
    
43
    public View $view;
44
    
45
    public Url $url;
46
    
47
    /**
48
     * Registers an autoloader related to the frontend module
49
     */
50
    public function registerAutoloaders(DiInterface $container = null): void
51
    {
52
        $this->getServices();
0 ignored issues
show
Bug introduced by
The call to Zemit\Mvc\Module::getServices() has too few arguments starting with container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->/** @scrutinizer ignore-call */ 
53
               getServices();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
        $this->loader->registerNamespaces($this->getNamespaces(), true);
54
        $this->loader->register();
55
    }
56
    
57
    /**
58
     * Registers services related to the module
59
     */
60
    public function registerServices(DiInterface $container): void
61
    {
62
        $this->getServices($container);
63
        
64
        $defaultNamespace = $this->getDefaultNamespace();
65
        $this->dispatcher->setDefaultNamespace($defaultNamespace);
66
        $this->dispatcher->setNamespaceName($defaultNamespace);
67
        $this->view->setViewsDir($this->getViewsDir());
68
        
69
        // url settings
70
        $this->url->setBasePath('/' . $this->name . '/');
71
        $this->url->setStaticBaseUri('/' . $this->name . '/');
72
        $this->router->setDefaults([
73
            'namespace' => $defaultNamespace,
74
            'module' => $this->name,
75
            'controller' => 'index',
76
            'action' => 'index',
77
        ]);
78
        
79
        // router settings
80
        $this->router->notFound([
81
            'controller' => 'error',
82
            'action' => 'notFound',
83
        ]);
84
        $this->router->removeExtraSlashes(true);
85
        
86
        $this->setServices($container);
87
    }
88
    
89
    public function getServices(DiInterface $container): void
90
    {
91
        $this->config ??= $container['config'] ??= new Config();
92
        $this->loader ??= $container['loader'] ??= new Loader();
93
        $this->router ??= $container['router'] ??= new Router();
94
        $this->dispatcher ??= $container['dispatcher'] ??= new Dispatcher();
95
        $this->view ??= $container['view'] ??= new View();
96
        $this->url ??= $container['url'] ??= new Url();
97
    }
98
    
99
    public function setServices(DiInterface $container): void
100
    {
101
        $container->set('config', $this->config);
102
        $container->set('dispatcher', $this->dispatcher);
103
        $container->set('loader', $this->loader);
104
        $container->set('router', $this->router);
105
        $container->set('view', $this->view);
106
        $container->set('url', $this->url);
107
    }
108
    
109
    public function getNamespaces(): array
110
    {
111
        // Caller namespace
112
        $namespace = $this->getNamespace();
113
        $dirname = $this->getDirname();
114
        
115
        // register the vendor module controllers
116
        $namespaces = [];
117
        $namespaces[$namespace . '\\Controllers'] = $dirname . '/Controllers/';
118
        $namespaces[$namespace . '\\Models'] = $dirname . '/Models/';
119
        
120
        // add zemit core models
121
        $corePath = dirname(__DIR__);
122
        $namespaces['Zemit\\Models'] = $corePath . '/Models/';
123
        
124
        return $namespaces;
125
    }
126
    
127
    public function getDefaultNamespace(): string
128
    {
129
        return $this->getNamespace() . '\\Controllers';
130
    }
131
    
132
    public function getViewsDir(): array
133
    {
134
        return [$this->getDirname() . '/Views/'];
135
    }
136
    
137
    public function getDirname(): string
138
    {
139
        return Utils::getDirname($this);
140
    }
141
    
142
    public function getNamespace(): string
143
    {
144
        return Utils::getNamespace($this);
145
    }
146
}
147