GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 290674...0f9d71 )
by Marius
14:11 queued 07:19
created

SiteMapA   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 295
Duplicated Lines 14.24 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.06%

Importance

Changes 0
Metric Value
dl 42
loc 295
ccs 89
cts 114
cp 0.7806
rs 8.3673
c 0
b 0
f 0
wmc 45
lcom 1
cbo 6

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A addClassMap() 20 20 4
A addStaticMap() 0 5 1
A getMaps() 0 3 1
A isValidStaticPath() 0 3 2
A getClassName() 0 8 1
A getCurrentModuleMap() 0 3 1
A getParentModuleMap() 0 10 3
A getAllModules() 0 15 3
A getAllControllers() 0 11 2
A findProcessorMap() 0 9 3
A getBaseRegex() 0 7 2
B addMap() 22 22 4
B addModuleMap() 0 27 3
C map() 0 32 8
A getControllerMappings() 0 7 2
A getModuleMappings() 0 7 2
A getProcessorMappings() 0 7 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like SiteMapA often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SiteMapA, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package application
4
 * @subpackage sitemaps
5
 * @author marius orcsik <[email protected]>
6
 * @date 09.09.24
7
 */
8
namespace vsc\application\sitemaps;
9
10
use vsc\application\processors\ProcessorA;
11
use vsc\infrastructure\BaseObject;
12
13
abstract class SiteMapA extends BaseObject {
14
	/**
15
	 * the base regex for the current map
16
	 * @todo this needs to be deprecated in favour of regexes of the parent module
17
	 * @var string
18
	 */
19
	private $aMaps = array();
20
	/**
21
	 * @var ModuleMap
22
	 */
23
	private $oCurrentModuleMap;
24
25
	public function __construct() {}
26
27
	/**
28
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
29
	 */
30 1
	public function getBaseRegex() {
31 1
		$oModuleMap = $this->getCurrentModuleMap();
32 1
		if (ModuleMap::isValid($oModuleMap)) {
33
			return (string)$oModuleMap->getRegex();
34
		}
35 1
		return null;
36
	}
37
38
	/**
39
	 *
40
	 * @param string $sRegex
41
	 * @param string $sPath
42
	 * @returns MappingA
43
	 */
44 2 View Code Duplication
	public function addMap($sRegex, $sPath) {
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...
45 2
		$oModuleMap = $this->getCurrentModuleMap();
46
47 2
		if (MappingA::isValid($oModuleMap)) {
48 1
			$sRegex = $oModuleMap->getRegex() . $sRegex;
49
		}
50
51 2
		if (!array_key_exists($sRegex, $this->aMaps)) {
52 2
			$oNewMap = new ClassMap($sPath, $sRegex);
53
54 2
			if (MappingA::isValid($oModuleMap)) {
55 1
				$oNewMap->merge($oModuleMap);
56 1
				$oNewMap->setModuleMap($oModuleMap);
57
			}
58
59 2
			$this->aMaps[$sRegex] = $oNewMap;
60
		} else {
61
			$oNewMap = $this->aMaps[$sRegex];
62
		}
63
64 2
		return $oNewMap;
65
	}
66
67
	/**
68
	 * @param string $sRegex
69
	 * @param string $sPath
70
	 * @throws ExceptionSitemap
71
	 * @returns MappingA
72
	 */
73 20 View Code Duplication
	protected function addClassMap($sRegex, $sPath) {
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 20
		$oModuleMap = $this->getCurrentModuleMap();
75 20
		$oNewMap = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
76
77 20
		if (MappingA::isValid($oModuleMap)) {
78 18
			$sRegex = $oModuleMap->getRegex() . $sRegex;
79
		}
80
81 20
		if (!array_key_exists($sRegex, $this->aMaps)) {
82 20
			$oNewMap = new ClassMap($sPath, $sRegex);
83
84 20
			if (MappingA::isValid($oModuleMap)) {
85 18
				$oNewMap->merge($oModuleMap);
86 18
				$oNewMap->setModuleMap($oModuleMap);
87
			}
88
89 20
			$this->aMaps[$sRegex] = $oNewMap;
90
		}
91 20
		return $oNewMap;
92
	}
93
94
	/**
95
	 *
96
	 * @param string $sRegex
97
	 * @param string $sPath
98
	 * @returns MappingA
99
	 */
100 19
	public function addModuleMap($sRegex, $sPath) {
101 19
		$oModuleMap = $this->getCurrentModuleMap();
102
103
		// setting the parent module map to the existing one
104 19
		if (MappingA::isValid($oModuleMap)) {
105
			$sRegex = $oModuleMap->getRegex() . $sRegex;
106
107
			$oNewModuleMap = new ModuleMap($sPath, $sRegex);
108
109
			$oNewModuleMap->setModuleMap($oModuleMap);
110
			$oNewModuleMap->merge($oModuleMap);
111
		} else {
112 19
			$oNewModuleMap = new RootMap($sPath, $sRegex);
113
		}
114
115
		// switching the current module map to the new one
116 19
		$this->oCurrentModuleMap = $oNewModuleMap;
117
118 19
		include ($sPath);
119
120 19
		if (ModuleMap::isValid($oNewModuleMap->getModuleMap())) {
121
			// 	after we finished parsing the new module, we set the previous module map as current
122 19
			$this->oCurrentModuleMap = $oNewModuleMap->getModuleMap();
123
		}
124
125 19
		return $oNewModuleMap;
126
	}
127
128
	/**
129
	 *
130
	 * @param string $sRegex
131
	 * @param string $sPath
132
	 * @returns MappingA
133
	 */
134 2
	public function addStaticMap($sRegex, $sPath) {
135 2
		$oStaticMap = $this->addMap($sRegex, $sPath);
136 2
		$oStaticMap->setIsStatic(true);
137 2
		return $oStaticMap;
138
	}
139
140
	/**
141
	 * @returns ClassMap[]
142
	 */
143 20
	public function getMaps() {
144 20
		return $this->aMaps;
145
	}
146
147
	/**
148
	 * This tells us if $sPath belongs to a file that can be used as a static resource
149
	 *  eg. Javascript, CSS, etc.
150
	 * @param string $sPath
151
	 * @return bool
152
	 */
153 3
	public static function isValidStaticPath($sPath) {
154 3
		return (!stristr($sPath, 'php') && is_file($sPath));
155
	}
156
157
	/**
158
	 * Gets the class name of based on the included path
159
	 * In order for it to work the file needs to be already include()-d
160
	 * @param string $sPath
161
	 * @return string
162
	 */
163 1
	public static function getClassName($sPath) {
164 1
		$sClassName = strtolower(basename($sPath, '.php'));
165
166 1
		$iKey = array_search($sClassName, array_map('strtolower', get_declared_classes()));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
167 1
		$aClasses = get_declared_classes();
168
169 1
		return  $aClasses[$iKey];
170
	}
171
172
	/**
173
	 * @returns ModuleMap
174
	 */
175 20
	public function getCurrentModuleMap() {
176 20
		return $this->oCurrentModuleMap;
177
	}
178
179
	/**
180
	 * @return MappingA|null
181
	 */
182 2
	public function getParentModuleMap() {
183 2
		if (MappingA::isValid($this->oCurrentModuleMap)) {
184 1
			$oParentModule = $this->oCurrentModuleMap->getModuleMap();
185 1
			if (ModuleMap::isValid($oParentModule)) {
186 1
				return $oParentModule;
187
			}
188
		}
189
		// return a default root node
190 1
		return new ModuleMap(VSC_SRC_PATH . 'config/map.php', '');
191
	}
192
193
	/**
194
	 *
195
	 * @param string $sRegex
196
	 * @param string $sPath
197
	 * @throws ExceptionSitemap
198
	 * @returns MappingA
199
	 */
200 19
	public function map($sRegex, $sPath) {
201 19
		if ($sRegex === null) {
202
			throw new ExceptionSitemap('A regex URI must be present.');
203
		}
204 19
		if (empty($sPath)) {
205
			throw new ExceptionSitemap('A path must be present.');
206
		}
207
208 19
		if (ClassMap::isValidMap($sPath)) {
209
			// instead of a path we have a namespace
210 19
			return $this->addClassMap($sRegex, $sPath);
211
		} else {
212 18
			if (!is_file($sPath)) {
213 1
				$sPath = $this->getCurrentModuleMap()->getModulePath() . $sPath;
214
			}
215
216 18
			if (!is_file($sPath)) {
217
				throw new ExceptionSitemap('The path associated with [' . $sRegex . '] can\'t be empty or an invalid file.');
218
			}
219
220 18
			$sPath = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $sPath);
221 18
			if (ModuleMap::isValidMap($sPath)) {
222
				// Valid site map
223 18
				return $this->addModuleMap($sRegex, $sPath);
224
			}
225 1
			if (self::isValidStaticPath($sPath)) {
226
				// Valid static file
227 1
				return $this->addStaticMap($sRegex, $sPath);
228
			}
229
			throw new ExceptionSitemap('[' . $sPath . '] could not be loaded.');
230
		}
231
	}
232
233
	/**
234
	 * @returns ModuleMap[]
235
	 */
236 2
	protected function getAllModules() {
237 2
		$aProcessorMaps = $this->getMaps();
238 2
		$aModuleMaps = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
239
240
		/* @var MappingA $oProcessor */
241 2
		foreach ($aProcessorMaps as $oProcessor) {
0 ignored issues
show
Bug introduced by
The expression $aProcessorMaps of type string is not traversable.
Loading history...
242 1
			$oModuleMap = $oProcessor->getModuleMap();
243 1
			if (!in_array($oModuleMap, $aModuleMaps, true)) {
244 1
				$aModuleMaps[$oModuleMap->getRegex()] = $oModuleMap;
245
			}
246
247
		}
248
249 2
		return $aModuleMaps;
250
	}
251
252
	/**
253
	 * @returns ClassMap[]
254
	 */
255 2
	protected function getAllControllers() {
256 2
		$aProcessorMaps = $this->getMaps();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
257 2
		$aControllerMaps = array();
258
259
		/* @var MappingA $oProcessor */
260 2
		foreach ($aProcessorMaps as $oProcessor) {
0 ignored issues
show
Bug introduced by
The expression $aProcessorMaps of type string is not traversable.
Loading history...
261 1
			$aControllerMaps = array_merge($aControllerMaps, $oProcessor->getModuleMap()->getControllerMaps());
262
		}
263
264 2
		return $aControllerMaps;
265
	}
266
267
	public function getControllerMappings() {
268
		$aC = false;
269
		foreach ($this->getAllControllers() as $sKey => $oController) {
270
			$aC[$sKey] = $oController->getPath();
271
		}
272
		return $aC;
273
	}
274
275
	public function getModuleMappings() {
276
		$aC = false;
277
		foreach ($this->getAllModules() as $sKey => $oModule) {
278
			$aC[$sKey] = $oModule->getPath();
279
		}
280
		return $aC;
281
	}
282
283
	/**
284
	 * @returns ClassMap[]
285
	 */
286
	public function getProcessorMappings() {
287
		$aC = false;
288
		foreach ($this->getMaps() as $sKey => $oProcessor) {
0 ignored issues
show
Bug introduced by
The expression $this->getMaps() of type string is not traversable.
Loading history...
289
			$aC[$sKey] = $oProcessor->getPath();
290
		}
291
		return $aC;
292
	}
293
294
	/**
295
	 * @param ProcessorA $oProcessor
296
	 * @return MappingA
297
	 */
298 2
	public function findProcessorMap(ProcessorA $oProcessor) {
299
		/* @var ClassMap $oProcessorMap */
300 2
		foreach ($this->getMaps() as $oProcessorMap) {
0 ignored issues
show
Bug introduced by
The expression $this->getMaps() of type string is not traversable.
Loading history...
301 1
			if ($oProcessorMap->maps($oProcessor)) {
302 1
				return $oProcessorMap;
303
			}
304
		}
305 1
		return null;
306
	}
307
}
308