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.

getNamespaceNumberFromDefinedConstantName()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 20
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * File containing the PermissionsHelper class
4
 *
5
 * This file is part of the MediaWiki skin Chameleon.
6
 *
7
 * @copyright 2013 - 2014, Stephan Gambke
8
 * @license   GNU General Public License, version 3 (or any later version)
9
 *
10
 * The Chameleon skin is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by the Free
12
 * Software Foundation, either version 3 of the License, or (at your option) any
13
 * later version.
14
 *
15
 * The Chameleon skin is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18
 * details.
19
 *
20
 * You should have received a copy of the GNU General Public License along
21
 * with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 * @file
24
 * @ingroup   Skins
25
 */
26
27
namespace Skins\Chameleon;
28
29
use DOMElement;
30
31
/**
32
 * PermissionsHelper class
33
 *
34
 * @author  Stephan Gambke
35
 * @since   1.1
36
 * @ingroup Skins
37
 */
38
class PermissionsHelper {
39
40
	private $domElement;
41
	private $skin;
42
	private $default;
43
44
	/**
45
	 * @param \SkinChameleon $skin
46
	 * @param DOMElement     $domElement
47
	 * @param bool           $default
48
	 */
49
	public function __construct( \SkinChameleon $skin, DOMElement $domElement = null, $default = false ) {
50
		$this->skin = $skin;
51
		$this->domElement = $domElement;
52
		$this->default = $default;
53
	}
54
55
	/**
56
	 * @since 1.1
57
	 *
58
	 * @param string $attributeNameInDomElement
59
	 *
60
	 * @return bool
61
	 */
62
	public function userHasGroup( $attributeNameInDomElement ) {
63
64
		return $this->userHas( 'group', $attributeNameInDomElement );
65
	}
66
67
	/**
68
	 * @param string $attributeOfUser
69
	 * @param string $attributeNameInDomElement
70
	 *
71
	 * @throws \MWException
72
	 * @return bool
73
	 */
74
	protected function userHas( $attributeOfUser, $attributeNameInDomElement ) {
75
76
		$user = $this->skin->getUser();
77
		$attributeAccessors = array(
78
			'group'      => array( $user, 'getEffectiveGroups' ),
79
			'permission' => array( $user, 'getRights' ),
80
		);
81
82
		if ( !array_key_exists( $attributeOfUser, $attributeAccessors ) ) {
83
			throw new \MWException( sprintf( 'Unknown permission: %s', $attributeOfUser ) );
84
		}
85
86
		if ( !$this->hasAttribute( $attributeNameInDomElement ) ) {
87
			return $this->default;
88
		}
89
90
		$expectedValues = $this->getValueListFromAttribute( $attributeNameInDomElement );
91
		$observedValues = call_user_func( $attributeAccessors[ $attributeOfUser ] );
92
		$effectiveValues = array_intersect( $expectedValues, $observedValues );
93
94
		return !empty( $effectiveValues );
95
	}
96
97
	/**
98
	 * @since 1.1
99
	 *
100
	 * @param string $attributeNameInDomElement
101
	 *
102
	 * @return bool
103
	 */
104
	public function hasAttribute( $attributeNameInDomElement ) {
105
		return $this->domElement !== null && $this->domElement->hasAttribute( $attributeNameInDomElement );
106
	}
107
108
	/**
109
	 * @param string $attributeName
110
	 *
111
	 * @return string[]
112
	 */
113
	protected function getValueListFromAttribute( $attributeName ) {
114
		return $this->domElement === null ? array() : array_map( 'trim', explode( ',', $this->domElement->getAttribute( $attributeName ) ) );
115
116
	}
117
118
	/**
119
	 * @since 1.1
120
	 *
121
	 * @param string $attributeNameInDomElement
122
	 *
123
	 * @return bool
124
	 */
125
	public function userHasPermission( $attributeNameInDomElement ) {
126
127
		return $this->userHas( 'permission', $attributeNameInDomElement );
128
	}
129
130
	/**
131
	 * @since 1.1
132
	 *
133
	 * @param string $attributeNameInDomElement
134
	 *
135
	 * @return bool
136
	 */
137
	public function pageIsInNamespace( $attributeNameInDomElement ) {
138
139
		if ( !$this->hasAttribute( $attributeNameInDomElement ) ) {
140
			return $this->default;
141
		}
142
143
		$expectedNamespaces = array_map( array( $this, 'getNamespaceNumberFromDefinedConstantName' ), $this->getValueListFromAttribute( $attributeNameInDomElement ) );
144
		$pageNamespace = $this->skin->getTitle()->getNamespace();
145
146
		return in_array( $pageNamespace, $expectedNamespaces );
147
	}
148
149
	/**
150
	 * @param null|string $value
151
	 *
152
	 * @return int
153
	 */
154
	protected function getNamespaceNumberFromDefinedConstantName( $value ) {
155
		$constants = get_defined_constants();
156
		if ( !is_null( $value ) && array_key_exists( $value, $constants ) ) {
157
			$value = $constants[ $value ];
158
		}
159
160
		return is_int( $value ) ? $value : -1;
161
	}
162
}
163