Completed
Push — master ( 50d24f...812ecb )
by judicael
03:47
created

Config::_recursiveGet()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 7.2765
cc 10
eloc 10
nc 5
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Config Manager
5
 *
6
 * @category  	core
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\core;
16
17
use \Venus\lib\Debug as Debug;
18
19
/**
20
 * Config Manager
21
 *
22
 * @category  	core
23
 * @author    	Judicaël Paquet <[email protected]>
24
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
25
 * @license   	https://github.com/las93/venus3/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
26
 * @version   	Release: 3.0.0
27
 * @filesource	https://github.com/las93/venus3
28
 * @link      	https://github.com/las93
29
 * @since     	3.0
30
 */
31
class Config
32
{
33
	/**
34
	 * conf in a cache array
35
	 *
36
	 * @access private
37
	 * @var    array
38
	 */
39
	private static $_aConfCache = array();
40
41
	/**
42
	 * get a configuration
43
	 *
44
	 * @access public
45
	 * @param  string $sName name of the configuration
46
	 * @param  string $sPortal portal name if you specify it
47
	 * @param  bool $bNoDoRedirect not allowed the redirect parameter
48
	 * @return void
49
	 */
50
	public static function get(string $sName, string $sPortal = null, bool $bNoDoRedirect = false)
51
	{
52
	    if ($bNoDoRedirect === true) { $sNameCache = $sName.'_true'; } else { $sNameCache = $sName; }
53
	    
54
		if ($sPortal === null || !is_string($sPortal)) {
55
		    
56
		    if (defined('PORTAL')) {
57
58
				$sPortal = PORTAL;
59
				$aDirectories = array($sPortal);
60
			} else {
61
62
				$sPortal = '';
63
				$aDirectories = scandir(str_replace('core', 'src', __DIR__));
64
			}
65
		}
66
67
		if (!isset(self::$_aConfCache[$sNameCache])) {
68
69
			$base = new \StdClass;
70
71
			foreach ($aDirectories as $sPortal) {
0 ignored issues
show
Bug introduced by
The variable $aDirectories does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
72
			
73
			    if ($sPortal != '..' && $sPortal != '.') {
74
75 View Code Duplication
        			if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
        
77
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
78
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
79
        			}
80
81
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) {
82
        				
83
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
84
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
85
        			}
86
87 View Code Duplication
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('DEV') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
        
89
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev';
90
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
91
        			}
92
93 View Code Duplication
        			if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('DEV') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
        
95
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev';
96
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
97
        			}
98
99 View Code Duplication
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PROD') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
        
101
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod';
102
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
103
        			}
104
105 View Code Duplication
        			if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PROD') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
        
107
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod';
108
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
109
        			}
110
111 View Code Duplication
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PREPROD') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
        
113
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod';
114
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
115
        			}
116
117 View Code Duplication
        			if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('PREPROD') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
        
119
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod';
120
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
121
        			}
122
123 View Code Duplication
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('RECETTE') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
        
125
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec';
126
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
127
        			}
128
129 View Code Duplication
        			if (file_exists(str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev') && getenv('RECETTE') == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
        
131
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec';
132
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
133
        			}
134
135
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) {
136
        
137
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
138
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
139
        			}
140
141
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf')) {
142
        
143
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf';
144
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
145
        			}
146
        
147
        			$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf';
148
        			$base = self::_mergeAndGetConf($sJsonFile, $base);
149
			    }
150
		    }
151
152
            if ($base === '') {
153
				
154
				trigger_error("Error in your Json format in this file : ".$sJsonFile, E_USER_NOTICE);
0 ignored issues
show
Bug introduced by
The variable $sJsonFile does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
155
			}
156
157
			if (isset($base->redirect) && $bNoDoRedirect === false) {
158
			
159
                $base = self::get($sName, $base->redirect);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $base is correct as self::get($sName, $base->redirect) (which targets Venus\core\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
160
			}
161
			
162
			self::$_aConfCache[$sNameCache] = $base;
163
		}
164
165
		if (!self::$_aConfCache[$sNameCache]) {
166
			
167
			$oDebug = Debug::getInstance();
168
			$oDebug->error('The configuration file '.$sName.' is in error!');
169
		}
170
		
171
		return self::$_aConfCache[$sNameCache];
172
	}
173
	
174
	/**
175
	 * get the bundle name location or the actualy bundle name if they isn't location
176
	 *
177
	 * @access public
178
	 * @param  string $sName name of the configuration
179
	 * @return string
180
	 */
181
	public static function getBundleLocationName(string $sName): string
182
	{
183
	    $oConfig = self::get($sName, null, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $oConfig is correct as self::get($sName, null, true) (which targets Venus\core\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
184
185
	    if (isset($oConfig->redirect)) { return $oConfig->redirect; } else { return PORTAL; }
186
	}
187
188
	/**
189
	 * get file content and merge if not exists
190
	 *
191
	 * @access private
192
	 * @param  string $sFileToMerge file to get
193
	 * @param  \stdClass $base base
194
	 * @return \stdClass
195
	 */
196
	private static function  _mergeAndGetConf(string $sFileToMerge, \stdClass $base) : \stdClass
197
	{
198
		$oConfFiles = json_decode(file_get_contents($sFileToMerge));
199
200
		if (is_object($oConfFiles)) {
201
202
			list($oConfFiles, $base) = self::_recursiveGet($oConfFiles, $base);
0 ignored issues
show
Unused Code introduced by
The assignment to $oConfFiles is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
203
			return $base;
204
		} else {
205
206
			echo "The Json ".$sFileToMerge." has an error! Please verify!\n";
207
			$oDebug = Debug::getInstance();
208
			$oDebug->error("The Json ".$sFileToMerge." has an error! Please verify!\n");
209
            new \Exception("The Json ".$sFileToMerge." has an error! Please verify!\n");
210
		}
211
	}
212
213
	/**
214
	 * recursive merge
215
	 *
216
	 * @access private
217
	 * @param  $oConfFiles
218
	 * @param  \stdClass $base
219
	 * @return multitype:array multitype:array
0 ignored issues
show
Documentation introduced by
The doc-type multitype:array could not be parsed: Unknown type name "multitype:array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
220
	 */
221
	private static function _recursiveGet($oConfFiles, \stdClass $base) : array
222
	{
223
		foreach ($oConfFiles as $sKey => $mOne) {
224
225
			if (is_object($oConfFiles) && is_object($base) && !isset($base->$sKey)) {
226
227
				$base->$sKey = $oConfFiles->$sKey;
228
			} else if (is_array($oConfFiles) && is_array($base) && !isset($base[$sKey])) {
229
230
				$base[$sKey] = $oConfFiles[$sKey];
231
            } else if (!isset($base->$sKey) && is_array($mOne)) {
232
233
				$base->$sKey = new \StdClass;
234
				list($oConfFiles, $base) = self::_recursiveGet($mOne, $base->$sKey);
235
			}
236
		}
237
238
		return array($oConfFiles, $base);
239
	}
240
}
241