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 ( 7b42e1...c93f43 )
by Marius
10:41 queued 04:09
created

ResourceMapTrait::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 20
	protected function mergeResources($oMap) {
56 20
		$aResources = $this->getResources();
57 20
		if ($oMap instanceof self) {
58 2
			$aParentResources = $oMap->getResources();
59 2
			$aResources = array_merge($aResources, $aParentResources);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
60
			// maybe I should merge the regexes too like processor_regex . '.*' . controller_regex
61
		}
62 20
		$this->aResources = $aResources;
63 20
	}
64
65
	/**
66
	 * @param $sVar
67
	 * @return void
68
	 */
69 1
	public function removeHeader($sVar) {
70 1
		$this->aResources['headers'][$sVar] = null;
71 1
	}
72
73
	/**
74
	 * @param $sVar
75
	 * @param $sVal
76
	 * @return void
77
	 */
78 1
	public function addHeader($sVar, $sVal) {
79 1
		$this->aResources['headers'][$sVar] = $sVal;
80 1
	}
81
82
	/**
83
	 * @param $sVar
84
	 * @param $sVal
85
	 * @return void
86
	 */
87 1
	public function addSetting($sVar, $sVal) {
88 1
		$this->aResources['settings'][$sVar] = $sVal;
89 1
	}
90
91
	/**
92
	 * @param string $sPath
93
	 * @return string
94
	 */
95
	private function getResourcePath($sPath) {
96
		if (is_file($this->getModulePath() . $sPath)) {
97
			$sPath = $this->getModulePath() . $sPath;
98
		}
99
		$oUrl = UrlParserA::url($sPath);
100
		if ($oUrl->isLocal()) {
101
			// I had a bad habit of correcting external URL's
102
			$sPath = $oUrl->getPath();
103
		}
104
		return $sPath;
105
	}
106
107 1
	public function addStyle($sPath, $sMedia = 'screen') {
108 1
		$this->aResources['styles'][$sMedia][] = $this->getResourcePath($sPath);
109 1
	}
110
111
	/**
112
	 *
113
	 * Adds a path for a JavaScript resource
114
	 * @param string $sPath
115
	 * @param bool $bInHead
116
	 */
117 2
	public function addScript($sPath, $bInHead = false) {
118 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...
119 2
		$this->aResources['scripts'][$iMainKey][] = $this->getResourcePath($sPath);
120 2
	}
121
122
	/**
123
	 * @param string $sType The type of the link element (eg, application/rss+xml or image/png)
124
	 * @param string $aData The rest of the link's attributes (href, rel, s/a)
125
	 * @return void
126
	 */
127 1
	public function addLink($sType, $aData) {
128 1
		if (array_key_exists('href', $aData)) {
129
			$aData['href'] = $this->getResourcePath($aData['href']);
130
		}
131 1
		if (array_key_exists('src', $aData)) {
132
			$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...
133
		}
134 1
		$this->aResources['links'][$sType][] = $aData;
135 1
	}
136
137 2
	public function addMeta($sName, $sValue) {
138 2
		$this->aResources['meta'][$sName] = $sValue;
139 2
	}
140
141
	/**
142
	 * @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...
143
	 * @return array
144
	 */
145 20
	public function getResources($sType = null) {
146 20
		if (!is_null($sType)) {
147
			if (array_key_exists($sType, $this->aResources)) {
148
				$aResources = $this->aResources[$sType];
149
			} else {
150
				$aResources = array();
151
			}
152
153
			return $aResources;
154
		} else {
155 20
			return $this->aResources;
156
		}
157
	}
158
159 1
	public function getStyles($sMedia = null) {
160 1
		$aStyles = $this->getResources('styles');
161 1
		if (!is_null($sMedia)) {
162
			$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...
163
			return array_key_exists($sMedia, $aStyles) ? $aMediaStyles : null;
164
		} else {
165 1
			return $aStyles;
166
		}
167
	}
168
169 3
	public function getMetas($sName = null) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
170 3
		$aMetas = $this->getResources('meta');
171 3
		if (!is_null($sName)) {
172 1
			return array_key_exists($sName, $aMetas) ? $aMetas[$sName] : '';
173
		} else {
174 2
			return $aMetas;
175
		}
176
	}
177
178
	/**
179
	 * @param bool $bInHead
180
	 * @return array
181
	 */
182 1
	public function getScripts($bInHead = false) {
183 1
		$aAllScripts = $this->getResources('scripts');
184 1
		if ($bInHead && array_key_exists(1, $aAllScripts)) {
185
			return $aAllScripts[1];
186
		}
187
188 1
		if (!$bInHead && array_key_exists(0, $aAllScripts)) {
189
			return $aAllScripts[0]; // [1] -> script goes in the <head> [0] - script is loaded at the end of the source
190
		}
191 1
		return [];
192
	}
193
194 1
	public function getSettings() {
195 1
		return $this->getResources('settings');
196
	}
197
198
	/**
199
	 * @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...
200
	 * @return array
201
	 */
202 2
	public function getLinks($sType = null) {
203 2
		$aLinks = $this->getResources('links');
204
205 2
		if (!is_null($sType)) {
206 1
			if (array_key_exists($sType, $aLinks)) {
207 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...
208 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...
209
			} else {
210
				$aLinks = array($sType => array()); // kinda hackish, but needed to have a uniform structure
211
			}
212
		}
213 2
		return $aLinks;
214
	}
215
216
	/**
217
	 * @param string $sVar
218
	 * @return array|string
219
	 */
220 1
	public function getSetting($sVar) {
221 1
		$aSettings = $this->getResources('settings');
222
223 1
		if (array_key_exists($sVar, $aSettings)) {
224
			return $aSettings[$sVar];
225
		} else {
226 1
			return '';
227
		}
228
	}
229
230 2
	public function getHeaders() {
231 2
		return $this->getResources('headers');
232
	}
233
234
}
235