Completed
Push — master ( 3071ad...3723f6 )
by Maxence
03:35 queued 01:12
created

BaseCircle   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 364
Duplicated Lines 2.75 %

Coupling/Cohesion

Components 4
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 53
c 1
b 0
f 0
lcom 4
cbo 1
dl 10
loc 364
rs 7.4757

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A setId() 0 5 1
A getId() 0 3 1
A setUniqueId() 0 5 1
A getUniqueId() 0 7 2
A generateUniqueId() 0 4 1
A setName() 0 5 1
A getName() 0 3 1
A getOwner() 0 3 1
A setOwner() 0 5 1
A getViewer() 0 3 1
A setViewer() 0 5 1
A getGroupViewer() 0 3 1
A setGroupViewer() 0 5 1
A getHigherViewer() 0 18 4
A setDescription() 0 5 1
A getDescription() 0 3 1
A setSettings() 0 9 3
B getSettings() 0 20 5
A setSetting() 0 15 3
A getSetting() 0 10 3
A setType() 0 5 1
A getType() 0 3 1
A setCreation() 0 5 1
A getCreation() 0 3 1
A setMembers() 0 5 1
A getMembers() 0 3 1
A setGroups() 0 5 1
A getGroups() 0 3 1
A setLinks() 0 5 1
A getLinks() 0 3 1
B typeInt() 10 19 6

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 BaseCircle 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 BaseCircle, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Model;
28
29
use OC\L10N\L10N;
30
31
class BaseCircle {
32
33
	const CIRCLES_SETTINGS_DEFAULT = [
34
		'allow_links'       => 'false',
35
		'allow_links_auto'  => 'false',
36
		'allow_links_files' => 'false'
37
	];
38
39
	const CIRCLES_PERSONAL = 1;
40
	const CIRCLES_HIDDEN = 2;
41
	const CIRCLES_PRIVATE = 4;
42
	const CIRCLES_PUBLIC = 8;
43
44
	const CIRCLES_ALL = 15;
45
46
	/** @var int */
47
	private $id;
48
49
	/** @var L10N */
50
	protected $l10n;
51
52
	/** @var string */
53
	private $uniqueId;
54
55
	/** @var string */
56
	private $name;
57
58
	/** @var Member */
59
	private $owner;
60
61
	/** @var Member */
62
	private $viewer;
63
64
	/** @var Member */
65
	private $viewerGroup;
66
67
	/** @var string */
68
	private $description = '';
69
70
	/** @var array */
71
	private $settings = [];
72
73
	/** @var int */
74
	private $type;
75
76
	/** @var string */
77
	private $creation;
78
79
	/** @var Member[] */
80
	private $members;
81
82
	/** @var Member[] */
83
	private $groups;
84
85
	/** @var FederatedLink[] */
86
	private $links;
87
88
	public function __construct($l10n, $type = -1, $name = '') {
89
		$this->l10n = $l10n;
90
91
		if ($type > -1) {
92
			$this->type = $type;
93
		}
94
		if ($name !== '') {
95
			$this->name = $name;
96
		}
97
	}
98
99
100
	public function setId($id) {
101
		$this->id = $id;
102
103
		return $this;
104
	}
105
106
	public function getId() {
107
		return $this->id;
108
	}
109
110
111
	/**
112
	 * @param string $uniqueId
113
	 *
114
	 * @return $this
115
	 */
116
	public function setUniqueId($uniqueId) {
117
		$this->uniqueId = (string)$uniqueId;
118
119
		return $this;
120
	}
121
122
	/**
123
	 * @param bool $full
124
	 *
125
	 * @return string
126
	 */
127
	public function getUniqueId($full = false) {
128
		if ($full) {
129
			return $this->uniqueId;
130
		}
131
132
		return substr($this->uniqueId, 0, 14);
133
	}
134
135
136
	public function generateUniqueId() {
137
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
138
		$this->setUniqueId($uniqueId);
139
	}
140
141
	public function setName($name) {
142
		$this->name = $name;
143
144
		return $this;
145
	}
146
147
	public function getName() {
148
		return $this->name;
149
	}
150
151
152
	public function getOwner() {
153
		return $this->owner;
154
	}
155
156
	public function setOwner($owner) {
157
		$this->owner = $owner;
158
159
		return $this;
160
	}
161
162
163
	/**
164
	 * @return Member
165
	 */
166
	public function getViewer() {
167
		return $this->viewer;
168
	}
169
170
	public function setViewer($user) {
171
		$this->viewer = $user;
172
173
		return $this;
174
	}
175
176
177
	/**
178
	 * @return Member
179
	 */
180
	public function getGroupViewer() {
181
		return $this->viewerGroup;
182
	}
183
184
	public function setGroupViewer($group) {
185
		$this->viewerGroup = $group;
186
187
		return $this;
188
	}
189
190
191
	/**
192
	 * @return Member
193
	 */
194
	public function getHigherViewer() {
195
		if ($this->getGroupViewer() === null) {
196
			return $this->getViewer();
197
		}
198
199
		if ($this->getViewer() === null) {
200
			return $this->getGroupViewer();
201
		}
202
203
		if ($this->getGroupViewer()
204
				 ->getLevel() > $this->getViewer()
205
									 ->getLevel()
206
		) {
207
			return $this->getGroupViewer();
208
		}
209
210
		return $this->getViewer();
211
	}
212
213
214
	public function setDescription($description) {
215
		$this->description = $description;
216
217
		return $this;
218
	}
219
220
	public function getDescription() {
221
		return $this->description;
222
	}
223
224
225
	public function setSettings($settings) {
226
		if (is_array($settings)) {
227
			$this->settings = $settings;
228
		} else if (is_string($settings)) {
229
			$this->settings = (array)json_decode($settings, true);
230
		}
231
232
		return $this;
233
	}
234
235
	public function getSettings($json = false) {
236
237
		if ($json) {
238
			return json_encode($this->settings);
239
		}
240
241
		$settings = $this->settings;
242
		if ($settings === null) {
243
			$settings = [];
244
		}
245
246
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
247
		foreach ($ak AS $k) {
248
			if (!key_exists($k, $settings)) {
249
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
250
			}
251
		}
252
253
		return $settings;
254
	}
255
256
257
	public function setSetting($k, $v) {
258
		switch ($k) {
259
			case 'circle_name':
260
				$this->setName($v);
261
				break;
262
263
			case 'circle_desc':
264
				$this->setDescription($v);
265
				break;
266
267
			default:
268
				$this->settings[$k] = $v;
269
				break;
270
		}
271
	}
272
273
274
	/**
275
	 * @param string $k
276
	 *
277
	 * @return string|null
278
	 */
279
	public function getSetting($k) {
280
		if (key_exists($k, $this->settings)) {
281
			return $this->settings[$k];
282
		}
283
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
284
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
285
		}
286
287
		return null;
288
	}
289
290
291
	public function setType($type) {
292
		$this->type = self::typeInt($type);
293
294
		return $this;
295
	}
296
297
	public function getType() {
298
		return $this->type;
299
	}
300
301
302
	public function setCreation($creation) {
303
		$this->creation = $creation;
304
305
		return $this;
306
	}
307
308
	public function getCreation() {
309
		return $this->creation;
310
	}
311
312
313
	public function setMembers($members) {
314
		$this->members = $members;
315
316
		return $this;
317
	}
318
319
	public function getMembers() {
320
		return $this->members;
321
	}
322
323
324
	public function setGroups($groups) {
325
		$this->groups = $groups;
326
327
		return $this;
328
	}
329
330
	public function getGroups() {
331
		return $this->groups;
332
	}
333
334
335
	public function setLinks($links) {
336
		$this->links = $links;
337
338
		return $this;
339
	}
340
341
	public function getLinks() {
342
		return $this->links;
343
	}
344
345
346
//	public function getRemote() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
347
//		return $this->remote;
348
//	}
349
//
350
//	public function addRemote($link) {
351
//		array_push($this->remote, $link);
352
//	}
353
//
354
//	public function getRemoteFromToken($token) {
355
//		foreach ($this->links AS $link) {
356
//			if ($link->getToken() === $token) {
357
//				return $link;
358
//			}
359
//		}
360
//
361
//		return null;
362
//	}
363
//
364
//	public function getRemoteFromAddressAndId($address, $id) {
365
//		foreach ($this->links AS $link) {
366
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
367
//				return $link;
368
//			}
369
//		}
370
//
371
//		return null;
372
//	}
373
374
375
	public static function typeInt($type) {
376
377
		if (is_numeric($type)) {
378
			return (int)$type;
379
		}
380
381 View Code Duplication
		switch ($type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
382
			case 'Personal':
383
				return self::CIRCLES_PERSONAL;
384
			case 'Private':
385
				return self::CIRCLES_PRIVATE;
386
			case 'Hidden':
387
				return self::CIRCLES_HIDDEN;
388
			case 'Public':
389
				return self::CIRCLES_PUBLIC;
390
		}
391
392
		return 0;
393
	}
394
}