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 ( eaf6b4...19d49d )
by Marius
03:14
created

ResourceMapTrait::mergeResources()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 2
nop 1
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 9
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package application
4
 * @subpackage sitemaps
5
 * @author marius orcisk <[email protected]>
6
 * @date 17.01.25
7
 */
8
namespace vsc\application\sitemaps;
9
10
use vsc\infrastructure\urls\UrlParserA;
11
12
trait ResourceMapTrait
13
{
14
	/**
15
	 * @var string
16
	 */
17
	private $sTitle;
18
19
	/**
20
	 * @var array
21
	 */
22
	private $aResources = [];
23
24
	/**
25
	 * @return string
26
	 * @throws ExceptionSitemap
27
	 */
28
	abstract public function getModulePath();
29
30
	/**
31
	 * @param string $sTitle
32
	 */
33 1
	public function setTitle($sTitle) {
34 1
		$this->sTitle = $sTitle;
35 1
	}
36
37
	/**
38
	 * @return string
39
	 */
40 20
	public function getTitle() {
41 20
		return $this->sTitle;
42
	}
43
44
45
	/**
46
	 * @param array $aResources
47
	 */
48 1
	public function setResources($aResources) {
49 1
		$this->aResources = $aResources;
50 1
	}
51
52
	/**
53
	 * @param ResourceMapTrait $oMap
0 ignored issues
show
introduced by
The type ResourceMapTrait for parameter $oMap is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
54
	 */
55 22
	protected function mergeResources($oMap) {
56 22
		$aResources = $this->getResources();
57 22
		if ($oMap instanceof ResourceMapInterface) {
58 22
			$aParentResources = $oMap->getResources();
59
			// iterate over our resources and merging with matching existing parent resources
60 22
			foreach ($aResources as $resType => $resources) {
61 2
				if (!isset($aParentResources[$resType])) { continue; }
62
63 2
				foreach ($resources as $key => $path) {
64 2
					if (!isset($aParentResources[$resType][$key])) {  continue; }
65 2
					$aResources[$resType][$key] = array_merge($aResources[$resType][$key], $aParentResources[$resType][$key]);
66 2
					unset ($aParentResources[$resType][$key]);
67
				}
68
			}
69
			// adding elements that exist just in parent resources
70 22
			foreach ($aParentResources as $resType => $resources) {
71 2
				if (!isset($aParentResources[$resType])) { $aResources[$resType] = []; }
72
73 2
				foreach ($resources as $key => $path) {
74 2
					$aResources[$resType][$key] = $aParentResources[$resType][$key];
75
				}
76
			}
77
		}
78 22
		$this->aResources = $aResources;
79 22
	}
80
81
	/**
82
	 * @param $sVar
83
	 * @return void
84
	 */
85 1
	public function removeHeader($sVar) {
86 1
		$this->aResources['headers'][$sVar] = null;
87 1
	}
88
89
	/**
90
	 * @param $sVar
91
	 * @param $sVal
92
	 * @return void
93
	 */
94 1
	public function addHeader($sVar, $sVal) {
95 1
		$this->aResources['headers'][$sVar] = $sVal;
96 1
	}
97
98
	/**
99
	 * @param $sVar
100
	 * @param $sVal
101
	 * @return void
102
	 */
103 1
	public function addSetting($sVar, $sVal) {
104 1
		$this->aResources['settings'][$sVar] = $sVal;
105 1
	}
106
107
	/**
108
	 * @param string $sPath
109
	 * @return string
110
	 */
111
	private function getResourcePath($sPath) {
112
		if (is_file($this->getModulePath() . $sPath)) {
113
			$sPath = $this->getModulePath() . $sPath;
114
		}
115
		$oUrl = UrlParserA::url($sPath);
116
		if ($oUrl->isLocal()) {
117
			// I had a bad habit of correcting external URL's
118
			$sPath = $oUrl->getPath();
119
		}
120
		return $sPath;
121
	}
122
123
	/**
124
	 * @param string $sPath
125
	 * @param string $sMedia
126
	 */
127 1
	public function addStyle($sPath, $sMedia = 'screen') {
128 1
		$this->aResources['styles'][$sMedia][] = $this->getResourcePath($sPath);
129 1
	}
130
131
	/**
132
	 * Adds a path for a JavaScript resource
133
	 * @param string $sPath
134
	 * @param bool $bInHead
135
	 */
136 2
	public function addScript($sPath, $bInHead = false) {
137 2
		$iMainKey = (int)$bInHead; // [1] in the <head> section; [0] at the end of the *HTML document
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 33 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...
138 2
		$this->aResources['scripts'][$iMainKey][] = $this->getResourcePath($sPath);
139 2
	}
140
141
	/**
142
	 * @param string $sType The type of the link element (eg, application/rss+xml or image/png)
143
	 * @param string $aData The rest of the link's attributes (href, rel, s/a)
144
	 * @return void
145
	 */
146 1
	public function addLink($sType, $aData) {
147 1
		if (array_key_exists('href', $aData)) {
148
			$aData['href'] = $this->getResourcePath($aData['href']);
149
		}
150 1
		if (array_key_exists('src', $aData)) {
151
			$aData['src'] = $this->getResourcePath($aData['src']); ;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
152
		}
153 1
		$this->aResources['links'][$sType][] = $aData;
154 1
	}
155
156
	/**
157
	 * @param string $sName
158
	 * @param string $sValue
159
	 * @return void
160
	 */
161 2
	public function addMeta($sName, $sValue) {
162 2
		$this->aResources['meta'][$sName] = $sValue;
163 2
	}
164
165
	/**
166
	 * @param string $sType
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sType not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
167
	 * @return array
168
	 */
169 20
	public function getResources($sType = null) {
170 20
		if (!is_null($sType)) {
171
			if (array_key_exists($sType, $this->aResources)) {
172
				$aResources = $this->aResources[$sType];
173
			} else {
174
				$aResources = array();
175
			}
176
177
			return $aResources;
178
		} else {
179 20
			return $this->aResources;
180
		}
181
	}
182
183
	/**
184
	 * @param null $sMedia
185
	 * @return array|null
186
	 */
187 1
	public function getStyles($sMedia = null) {
188 1
		$aStyles = $this->getResources('styles');
189 1
		if (!is_null($sMedia)) {
190
			$aMediaStyles[$sMedia] = $aStyles[$sMedia];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aMediaStyles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aMediaStyles = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
191
			return array_key_exists($sMedia, $aStyles) ? $aMediaStyles : null;
192
		} else {
193 1
			return $aStyles;
194
		}
195
	}
196
197
	/**
198
	 * @param null $sName
199
	 * @return array|string
200
	 */
201 3
	public function getMetas($sName = null) {
202 3
		$aMetas = $this->getResources('meta');
203 3
		if (!is_null($sName)) {
204 1
			return array_key_exists($sName, $aMetas) ? $aMetas[$sName] : '';
205
		} else {
206 2
			return $aMetas;
207
		}
208
	}
209
210
	/**
211
	 * @param bool $bInHead
212
	 * @return array
213
	 */
214 1
	public function getScripts($bInHead = false) {
215 1
		$aAllScripts = $this->getResources('scripts');
216 1
		if ($bInHead && array_key_exists(1, $aAllScripts)) {
217
			return $aAllScripts[1];
218
		}
219
220 1
		if (!$bInHead && array_key_exists(0, $aAllScripts)) {
221
			return $aAllScripts[0]; // [1] -> script goes in the <head> [0] - script is loaded at the end of the source
222
		}
223 1
		return [];
224
	}
225
226
	/**
227
	 * @return array
228
	 */
229 1
	public function getSettings() {
230 1
		return $this->getResources('settings');
231
	}
232
233
	/**
234
	 * @param string $sType
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sType not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
235
	 * @return array
236
	 */
237 2
	public function getLinks($sType = null) {
238 2
		$aLinks = $this->getResources('links');
239
240 2
		if (!is_null($sType)) {
241 1
			if (array_key_exists($sType, $aLinks)) {
242 1
				$aTLinks[$sType] = $aLinks[$sType];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aTLinks was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aTLinks = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
243 1
				$aLinks = $aTLinks;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
244
			} else {
245
				$aLinks = array($sType => array()); // kinda hackish, but needed to have a uniform structure
246
			}
247
		}
248 2
		return $aLinks;
249
	}
250
251
	/**
252
	 * @param string $sVar
253
	 * @return array|string
254
	 */
255 1
	public function getSetting($sVar) {
256 1
		$aSettings = $this->getResources('settings');
257
258 1
		if (array_key_exists($sVar, $aSettings)) {
259
			return $aSettings[$sVar];
260
		} else {
261 1
			return '';
262
		}
263
	}
264
265
	/**
266
	 * @return array
267
	 */
268 2
	public function getHeaders() {
269 2
		return $this->getResources('headers');
270
	}
271
272
}
273