Completed
Push — master ( b58116...9afd96 )
by Cheren
08:23
created

Cms::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core;
17
18
use Core\Path\Path;
19
use JBZoo\Utils\FS;
20
use Pimple\Container;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Core\Container.

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...
21
use Cake\Utility\Hash;
22
use Cake\Core\Configure;
23
24
/**
25
 * Class Cms
26
 *
27
 * @package Core
28
 */
29
class Cms extends Container
30
{
31
32
    /**
33
     * Get cms instance.
34
     *
35
     * @return Cms
36
     */
37
    public static function getInstance()
38
    {
39
        static $instance;
40
        if (null === $instance) {
41
            $instance = new self();
42
            $instance->_initialize();
0 ignored issues
show
Unused Code introduced by
The call to the method Core\Cms::_initialize() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
43
        }
44
45
        return $instance;
46
    }
47
48
    /**
49
     * Merge configure values by key.
50
     *
51
     * @param string $key
52
     * @param array|string $config
53
     * @return array|mixed
54
     */
55
    public static function mergeConfig($key, $config)
56
    {
57
        $values = Hash::merge((array) Configure::read($key), $config);
58
        Configure::write($key, $values);
59
60
        return $values;
61
    }
62
63
    /**
64
     * On initialize application.
65
     *
66
     * @return void
67
     */
68
    protected function _initialize()
69
    {
70
        $this['path'] = function () {
71
            return $this->_initPaths();
72
        };
73
    }
74
75
    /**
76
     * Init base paths.
77
     *
78
     * @return Path
79
     * @throws \JBZoo\Path\Exception
80
     */
81
    protected function _initPaths()
82
    {
83
        $path = Path::getInstance();
84
        $path->setRoot(ROOT);
85
86
        //  Setup all webroot paths
87
        $path->set('webroot', Configure::read('App.wwwRoot'));
88
        foreach ((array) Plugin::loaded() as $name) {
89
            $plgPath = Plugin::path($name) . '/' . Configure::read('App.webroot') . '/';
90
            $path->set('webroot', FS::clean($plgPath), Path::MOD_APPEND);
91
        }
92
93
        //  Setup applicatiojn paths
94
        $paths = Configure::read('App.paths');
95
        foreach ($paths as $alias => $_paths) {
96
            $path->set($alias, $_paths);
97
        }
98
99
        return $path;
100
    }
101
}
102