DbMock   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
dl 0
loc 216
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllRoles() 0 7 2
A getRoleByName() 0 14 2
A getUserRole() 0 3 1
A setUserRole() 0 4 1
A unsetUserRole() 0 4 1
1
<?php
2
3
namespace Elgg\Roles;
4
5
use ElggRole;
6
7
class DbMock implements DbInterface {
8
9
	private $conf = array(
10
		'default' => array(
11
			'title' => 'roles:role:DEFAULT_ROLE',
12
			'extends' => array(),
13
			'permissions' => array(
14
				'actions' => array(
15
					'bar/foo' => 'allow',
16
					'baz' => array('rule' => 'deny', 'redirect' => 'bar/foo'),
17
				),
18
			),
19
		),
20
		'tester1' => array(
21
			'title' => 'tester1',
22
			'extends' => array('default'),
23
			'permissions' => array(
24
				'actions' => array(
25
					'foo/bar' => 'allow',
26
					'foo/bar/baz' => 'deny',
27
					'bar/foo' => ['rule' => 'deny']
28
				),
29
			),
30
		),
31
		'tester2' => array(
32
			'title' => 'tester2',
33
			'extends' => array('default'),
34
			'permissions' => array(
35
				'actions' => array(
36
					'foo/foo/bar' => 'allow',
37
					'bar/baz' => 'deny',
38
					'bar/foo' => 'deny',
39
				),
40
			),
41
		),
42
		'deny' => array(
43
			'title' => 'deny',
44
			'extends' => array(),
45
			'permissions' => array(
46
				'views' => array(
47
					'foo/bar' => 'deny',
48
				),
49
				'actions' => array(
50
					'foo/bar' => 'deny',
51
				),
52
				'pages' => array(
53
					'foo/bar' => 'deny',
54
				),
55
				'hooks' => array(
56
					'foo::bar' => 'deny',
57
				),
58
				'events' => array(
59
					'foo::bar' => 'deny',
60
				),
61
				'menus' => array(
62
					'foo' => 'deny',
63
					'bar::baz' => 'deny',
64
				)
65
			)
66
		),
67
		'allow' => array(
68
			'title' => 'deny',
69
			'extends' => array('deny'),
70
			'permissions' => array(
71
				'views' => array(
72
					'foo/bar' => 'allow',
73
				),
74
				'actions' => array(
75
					'foo/bar' => 'allow',
76
				),
77
				'pages' => array(
78
					'foo/bar' => 'allow',
79
				),
80
				'hooks' => array(
81
					'foo::bar' => 'allow',
82
				),
83
				'events' => array(
84
					'foo::bar' => 'allow',
85
				),
86
				'menus' => array(
87
					'foo' => 'allow',
88
					'bar::baz' => 'allow',
89
				)
90
			),
91
		),
92
		'extend' => array(
93
			'title' => 'extend',
94
			'extends' => array('deny'),
95
			'permissions' => array(
96
				'views' => array(
97
					'foo/bar' => array(
98
						'rule' => 'extend',
99
						'view_extension' => array(
100
							'view' => 'foo/baz',
101
							'priority' => 400,
102
						),
103
					),
104
				),
105
				'hooks' => array(
106
					'foo::bar' => array(
107
						'rule' => 'extend',
108
						'hook' => array(
109
							'handler' => '\Elgg\Values::getFalse',
110
							'priority' => 600,
111
						)
112
					),
113
				),
114
				'events' => array(
115
					'foo::bar' => array(
116
						'rule' => 'extend',
117
						'event' => array(
118
							'handler' => '\Elgg\Values::getFalse',
119
							'priority' => 600,
120
						)
121
					),
122
				),
123
				'menus' => array(
124
					'foo' => array(
125
						'rule' => 'extend',
126
						'menu_item' => array(
127
							'name' => 'baz2',
128
							'href' => 'baz2',
129
							'text' => 'baz2',
130
						),
131
					),
132
				),
133
			)
134
		),
135
		'replace' => array(
136
			'title' => 'replace',
137
			'extends' => array('deny'),
138
			'permissions' => array(
139
				'views' => array(
140
					'foo/baz' => array(
141
						'rule' => 'replace',
142
						'view_replacement' => array(
143
							'location' => '/mod/roles/tests/phpunit/test_files/views2/',
144
						),
145
					),
146
				),
147
				'pages' => array(
148
					'foo/bar' => array(
149
						'rule' => 'redirect',
150
						'forward' => 'foo/baz',
151
					),
152
				),
153
				'hooks' => array(
154
					'foo::bar' => array(
155
						'rule' => 'replace',
156
						'hook' => array(
157
							'old_handler' => '\Elgg\Values::getTrue',
158
							'new_handler' => '\Elgg\Values::getFalse',
159
						)
160
					),
161
				),
162
				'events' => array(
163
					'foo::bar' => array(
164
						'rule' => 'replace',
165
						'event' => array(
166
							'old_handler' => '\Elgg\Values::getTrue',
167
							'new_handler' => '\Elgg\Values::getFalse',
168
						)
169
					),
170
				),
171
				'menus' => array(
172
					'bar::baz' => array(
173
						'rule' => 'replace',
174
						'menu_item' => array(
175
							'name' => 'baz2',
176
							'href' => 'baz2',
177
							'text' => 'baz2',
178
						),
179
					),
180
				),
181
			)
182
		),
183
	);
184
185 27
	public function getAllRoles() {
186 27
		$roles = array();
187 27
		foreach (array_keys($this->conf) as $name) {
188 27
			$roles[] = $this->getRoleByName($name);
189 27
		}
190 27
		return $roles;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $roles; (array) is incompatible with the return type declared by the interface Elgg\Roles\DbInterface::getAllRoles of type ElggBatch.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
191
	}
192
193 27
	public function getRoleByName($role_name = '') {
194
195 27
		if (!isset($this->conf[$role_name])) {
196
			return false;
197
		}
198 27
		$conf = $this->conf[$role_name];
199 27
		$role = new ElggRole();
200 27
		$role->name = $role_name;
201 27
		$role->title = $conf['title'];
202 27
		$role->setExtends($conf['extends']);
203 27
		$role->setPermissions($conf['permissions']);
204
205 27
		return $role;
206
	}
207
208 1
	public function getUserRole(\ElggUser $user) {
209 1
		return $user->getVolatileData('role');
210
	}
211
212 1
	public function setUserRole(\ElggUser $user, ElggRole $role) {
213 1
		$user->setVolatileData('role', $role);
214 1
		return true;
215
	}
216
217 1
	public function unsetUserRole(\ElggUser $user) {
218 1
		$user->setVolatileData('role', null);
219 1
		return true;
220
	}
221
222
}
223