Completed
Push — master ( 0648f2...737095 )
by Jean-Christophe
01:38
created

StartupConfigTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 83
Duplicated Lines 62.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 52
loc 83
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 3 1
A setConfig() 0 3 1
A getModelsDir() 0 3 1
A getModelsCompletePath() 0 3 1
A needsKeyInConfigArray() 7 7 4
A getNS() 8 8 3
A setCtrlNS() 0 3 1
A checkDbConfig() 11 11 2
A checkModelsConfig() 10 10 2
A reloadConfig() 7 7 1
A saveConfig() 9 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\utils\base\UFileSystem;
7
8
trait StartupConfigTrait {
9
	protected static $config;
10
	protected static $ctrlNS;
11
	
12
	public static function getConfig() {
13
		return self::$config;
14
	}
15
	
16
	public static function setConfig($config) {
17
		self::$config = $config;
18
	}
19
	
20
	public static function getModelsDir() {
21
		return self::$config ["mvcNS"] ["models"];
22
	}
23
	
24
	public static function getModelsCompletePath() {
25
		return ROOT . DS . self::getModelsDir ();
26
	}
27
	
28 View Code Duplication
	protected static function needsKeyInConfigArray(&$result, $array, $needs) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
29
		foreach ( $needs as $need ) {
30
			if (! isset ( $array [$need] ) || UString::isNull ( $array [$need] )) {
31
				$result [] = $need;
32
			}
33
		}
34
	}
35
	
36 View Code Duplication
	public static function getNS($part = "controllers") {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
		$config = self::$config;
38
		$ns = $config ["mvcNS"] [$part];
39
		if ($ns !== "" && $ns !== null) {
40
			$ns .= "\\";
41
		}
42
		return $ns;
43
	}
44
	
45
	protected static function setCtrlNS() {
46
		self::$ctrlNS = self::getNS ();
47
	}
48
	
49 View Code Duplication
	public static function checkDbConfig() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
		$config = self::$config;
51
		$result = [ ];
52
		$needs = [ "type","dbName","serverName" ];
53
		if (! isset ( $config ["database"] )) {
54
			$result [] = "database";
55
		} else {
56
			self::needsKeyInConfigArray ( $result, $config ["database"], $needs );
57
		}
58
		return $result;
59
	}
60
	
61 View Code Duplication
	public static function checkModelsConfig() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
62
		$config = self::$config;
63
		$result = [ ];
64
		if (! isset ( $config ["mvcNS"] )) {
65
			$result [] = "mvcNS";
66
		} else {
67
			self::needsKeyInConfigArray ( $result, $config ["mvcNS"], [ "models" ] );
68
		}
69
		return $result;
70
	}
71
	
72
	
73 View Code Duplication
	public static function reloadConfig(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
		$appDir=\dirname ( ROOT );
75
		$filename=$appDir."/app/config/config.php";
76
		self::$config=include($filename);
77
		self::startTemplateEngine(self::$config);
78
		return self::$config;
79
	}
80
	
81 View Code Duplication
	public static function saveConfig($content){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
82
		$appDir=\dirname ( ROOT );
83
		$filename=$appDir."/app/config/config.php";
84
		$oldFilename=$appDir."/app/config/config.old.php";
85
		if (!file_exists($filename) || copy($filename, $oldFilename)) {
86
			return UFileSystem::save($filename,$content);
87
		}
88
		return false;
89
	}
90
}
91
92