Passed
Pull Request — master (#19)
by Anton
03:33
created

Addons   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 79
Duplicated Lines 10.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 79
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadItemsAll() 4 13 3
A loadItemsInstalled() 4 13 3
A __construct() 0 6 2
B install() 0 21 9

How to fix   Duplicated Code   

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:

1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Extend
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Extend\Utils\Loader {
11
12
	use Modules\Extend, Utils\Schema;
13
14
	abstract class Addons extends Extend\Utils\Loader {
15
16
		/**
17
		 * Load the list of all items
18
		 */
19
20
		protected function loadItemsAll() : array {
21
22
			$items = $this->loadItems(['installed' => false]);
23
24 View Code Duplication
			foreach ((Schema::get(static::$schema)->load() ?? []) as $item) {
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...
25
26
				if (isset($items[$item['name']])) $items[$item['name']]['installed'] = true;
27
			}
28
29
			# ------------------------
30
31
			return $items;
32
		}
33
34
		/**
35
		 * Load the list of installed items
36
		 */
37
38
		protected function loadItemsInstalled() : array {
39
40
			$items = [];
41
42 View Code Duplication
			foreach ((Schema::get(static::$schema)->load() ?? []) as $item) {
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...
43
44
				if (static::$extension_class::isValid($item['name'])) $items[$item['name']] = $item;
45
			}
46
47
			# ------------------------
48
49
			return $items;
50
		}
51
52
		/**
53
		 * Constructor
54
		 */
55
56
		public function __construct(bool $load_all = true) {
57
58
			$this->dir_name = static::$root_dir;
59
60
			$this->items = ($load_all ? $this->loadItemsAll() : $this->loadItemsInstalled());
61
		}
62
63
		/**
64
		 * Install or uninstall an item
65
		 *
66
		 * @param $value : if set to true the item will be installed, otherwise uninstalled
67
		 *
68
		 * @return bool : true on success or false on failure
69
		 */
70
71
		public function install(string $name, bool $value = true) : bool {
72
73
			if (!isset($this->items[$name])) return false;
74
75
			$items = [];
76
77
			foreach ($this->items as $item) {
78
79
				if ($value) { if ($item['installed'] || ($item['name'] === $name)) $items[] = $item; }
80
81
				else { if ($item['installed'] && ($item['name'] !== $name)) $items[] = $item; }
82
			}
83
84
			if (!Schema::get(static::$schema)->save($items)) return false;
85
86
			$this->items[$name]['installed'] = $value;
87
88
			# ------------------------
89
90
			return true;
91
		}
92
	}
93
}
94