Config::_recursiveGet()   B
last analyzed

Complexity

Conditions 10
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 7.2765
c 0
b 0
f 0
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
        $aDirectories = [];
53
        $sJsonFile='';
54
55
        if ($bNoDoRedirect === true) { $sNameCache = $sName.'_true'; } else { $sNameCache = $sName; }
56
	    
57
		if ($sPortal === null || !is_string($sPortal)) {
58
		    
59
		    if (defined('PORTAL')) {
60
61
				$sPortal = PORTAL;
62
				$aDirectories = array($sPortal);
63
			} else {
64
65
				$sPortal = '';
66
				$aDirectories = scandir(str_replace('core', 'src', __DIR__));
67
			}
68
		}
69
70
		if (!isset(self::$_aConfCache[$sNameCache])) {
71
72
			$base = new \stdClass;
73
74
            if (count($aDirectories) < 1) { $aDirectories = [$sPortal]; }
75
76
			foreach ($aDirectories as $sPortal) {
77
			
78
			    if ($sPortal != '..' && $sPortal != '.') {
79
80 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...
81
        
82
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
83
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
84
        			}
85
86
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) {
87
        				
88
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
89
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
90
        			}
91
92 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...
93
        
94
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev';
95
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
96
        			}
97
98 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...
99
        
100
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-dev';
101
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
102
        			}
103
104 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...
105
        
106
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod';
107
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
108
        			}
109
110 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...
111
        
112
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-prod';
113
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
114
        			}
115
116 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...
117
        
118
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod';
119
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
120
        			}
121
122 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...
123
        
124
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-pprod';
125
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
126
        			}
127
128 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...
129
        
130
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec';
131
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
132
        			}
133
134 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...
135
        
136
        				$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-rec';
137
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
138
        			}
139
140
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local')) {
141
        
142
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf-local';
143
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
144
        			}
145
146
        			if (file_exists(str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf')) {
147
        
148
        				$sJsonFile = str_replace('core', 'src'.DIRECTORY_SEPARATOR.$sPortal.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf';
149
        				$base = self::_mergeAndGetConf($sJsonFile, $base);
150
        			}
151
152
        			$sJsonFile = str_replace('core', 'conf', __DIR__).DIRECTORY_SEPARATOR.$sName.'.conf';
153
        			$base = self::_mergeAndGetConf($sJsonFile, $base);
154
			    }
155
		    }
156
157
            if ($base === '') {
158
				
159
				trigger_error("Error in your Json format in this file : ".$sJsonFile, E_USER_NOTICE);
160
			}
161
162
			if (isset($base->redirect) && $bNoDoRedirect === false) {
163
			
164
                $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...
165
			}
166
			
167
			self::$_aConfCache[$sNameCache] = $base;
168
		}
169
170
		if (!self::$_aConfCache[$sNameCache]) {
171
			
172
			$oDebug = Debug::getInstance();
173
			$oDebug->error('The configuration file '.$sName.' is in error!');
174
		}
175
		
176
		return self::$_aConfCache[$sNameCache];
177
	}
178
	
179
	/**
180
	 * get the bundle name location or the actualy bundle name if they isn't location
181
	 *
182
	 * @access public
183
	 * @param  string $sName name of the configuration
184
	 * @return string
185
	 */
186
	public static function getBundleLocationName(string $sName): string
187
	{
188
	    $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...
189
190
	    if (isset($oConfig->redirect)) { return $oConfig->redirect; } else { return PORTAL; }
191
	}
192
193
	/**
194
	 * get file content and merge if not exists
195
	 *
196
	 * @access private
197
	 * @param  string $sFileToMerge file to get
198
	 * @param  \stdClass $base base
199
	 * @return \stdClass
200
	 */
201
	private static function  _mergeAndGetConf(string $sFileToMerge, \stdClass $base) : \stdClass
202
	{
203
		$oConfFiles = json_decode(file_get_contents($sFileToMerge));
204
205
		if (is_object($oConfFiles)) {
206
207
			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...
208
			return $base;
209
		} else {
210
211
			echo "The Json ".$sFileToMerge." has an error! Please verify!\n";
212
			$oDebug = Debug::getInstance();
213
			$oDebug->error("The Json ".$sFileToMerge." has an error! Please verify!\n");
214
            new \Exception("The Json ".$sFileToMerge." has an error! Please verify!\n");
215
		}
216
	}
217
218
	/**
219
	 * recursive merge
220
	 *
221
	 * @access private
222
	 * @param  $oConfFiles
223
	 * @param  \stdClass $base
224
	 * @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...
225
	 */
226
	private static function _recursiveGet($oConfFiles, \stdClass $base) : array
227
	{
228
		foreach ($oConfFiles as $sKey => $mOne) {
229
230
			if (is_object($oConfFiles) && is_object($base) && !isset($base->$sKey)) {
231
232
				$base->$sKey = $oConfFiles->$sKey;
233
			} else if (is_array($oConfFiles) && is_array($base) && !isset($base[$sKey])) {
234
235
				$base[$sKey] = $oConfFiles[$sKey];
236
            } else if (!isset($base->$sKey) && is_array($mOne)) {
237
238
				$base->$sKey = new \StdClass;
239
				list($oConfFiles, $base) = self::_recursiveGet($mOne, $base->$sKey);
240
			}
241
		}
242
243
		return array($oConfFiles, $base);
244
	}
245
}
246