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.

BlockManager   B
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 186
Duplicated Lines 10.75 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 7
dl 20
loc 186
rs 8.2608
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getAreas() 0 13 5
C getAreasForPageType() 20 46 12
D getBlockClasses() 0 28 9
A getUseBlockSets() 0 6 2
A getExcludeFromPageTypes() 0 6 2
A getWhiteListedPageTypes() 0 5 2
A getBlackListedPageTypes() 0 7 3
A getUseExtraCSSClasses() 0 6 2
A getPrefixDefaultCSSClasses() 0 6 2

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

1
<?php
2
3
namespace SheaDawson\Blocks;
4
5
use SilverStripe\ORM\ArrayLib;
6
use SilverStripe\SiteConfig\SiteConfig;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\ClassInfo;
9
use SilverStripe\Forms\FormField;
10
use SilverStripe\View\SSViewer;
11
use SilverStripe\View\ViewableData;
12
13
/**
14
 * BlockManager.
15
 *
16
 * @author Shea Dawson <[email protected]>
17
 */
18
class BlockManager extends ViewableData
19
{
20
	/**
21
	 * Use default ContentBlock class.
22
	 *
23
	 * @var bool
24
	 **/
25
	private static $use_default_blocks = true;
0 ignored issues
show
Unused Code introduced by
The property $use_default_blocks is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
	/**
28
	 * Show a block area preview button in CMS
29
	 *
30
	 * @var bool
31
	 **/
32
	private static $block_area_preview = true;
0 ignored issues
show
Unused Code introduced by
The property $block_area_preview is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
33
34
	public function __construct()
35
	{
36
		parent::__construct();
37
	}
38
39
	/**
40
	 * Gets an array of all areas defined for blocks.
41
	 *
42
	 * @param bool   $keyAsValue
43
	 *
44
	 * @return array $areas
45
	 **/
46
	public function getAreas($keyAsValue = true)
47
	{
48
		$areas = $this->config()->get('areas');
49
50
		$areas = $keyAsValue ? ArrayLib::valuekey(array_keys($areas)) : $areas;
51
		if (count($areas)) {
52
			foreach ($areas as $k => $v) {
53
				$areas[$k] = $keyAsValue ? FormField::name_to_label($k) : $v;
54
			}
55
		}
56
57
		return $areas;
58
	}
59
60
	/**
61
	 * Gets an array of all areas defined that are compatible with pages of type $class.
62
	 *
63
	 * @param string $class
64
	 *
65
	 * @return array $areas
66
	 **/
67
	public function getAreasForPageType($class)
68
	{
69
		$areas = $this->getAreas(false);
70
71
		if (!$areas) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $areas of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
72
			return false;
73
		}
74
75
		foreach ($areas as $area => $config) {
76
			if (!is_array($config)) {
77
				continue;
78
			}
79
80 View Code Duplication
			if (isset($config['except'])) {
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...
81
				$except = $config['except'];
82
				if (is_array($except)
83
					? in_array($class, $except)
84
					: $except == $class
85
				) {
86
					unset($areas[$area]);
87
					continue;
88
				}
89
			}
90
91 View Code Duplication
			if (isset($config['only'])) {
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...
92
				$only = $config['only'];
93
				if (is_array($only)
94
					? !in_array($class, $only)
95
					: $only != $class
96
				) {
97
					unset($areas[$area]);
98
					continue;
99
				}
100
			}
101
		}
102
103
		if (count($areas)) {
104
			foreach ($areas as $k => $v) {
105
				$areas[$k] = _t('Block.BlockAreaName.'.$k, FormField::name_to_label($k));
106
			}
107
108
			return $areas;
109
		} else {
110
			return $areas;
111
		}
112
	}
113
114
	public function getBlockClasses()
115
	{
116
		$classes = ArrayLib::valuekey(ClassInfo::subclassesFor("SheaDawson\Blocks\model\Block"));
117
		array_shift($classes);
118
		foreach ($classes as $k => $v) {
119
			$classes[$k] = singleton($k)->singular_name();
120
		}
121
122
		$config = $this->config()->get('options');
123
124
		if (isset($config['use_default_blocks']) && !$config['use_default_blocks']) {
125
	        unset($classes['ContentBlock']);
126
	    } else if (!$config['use_default_blocks']) {
127
	        unset($classes['ContentBlock']);
128
	    }
129
130
		$disabledArr = Config::inst()->get("BlockManager", 'disabled_blocks') ? Config::inst()->get("BlockManager", 'disabled_blocks') : [];
131
		if (isset($config['disabled_blocks'])) {
132
		    $disabledArr = array_merge($disabledArr, $config['disabled_blocks']);
133
		}
134
		if (count($disabledArr)) {
135
			foreach ($disabledArr as $k => $v) {
136
				unset($classes[$v]);
137
			}
138
		}
139
140
		return $classes;
141
	}
142
143
	/*
144
	 * Usage of BlockSets configurable from yaml
145
	 */
146
	public function getUseBlockSets()
147
	{
148
		$config = $this->config()->get('options');
149
150
		return isset($config['use_blocksets']) ? $config['use_blocksets'] : true;
151
	}
152
153
	/*
154
	 * Exclusion of blocks from page types defined in yaml
155
	 */
156
	public function getExcludeFromPageTypes()
157
	{
158
		$config = $this->config()->get('options');
159
160
		return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : [];
161
	}
162
163
	/*
164
	 * getWhiteListedPageTypes optionally configured by the developer
165
	 */
166
	public function getWhiteListedPageTypes()
167
	{
168
		$config = $this->config()->get('options');
169
		return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : [];
170
	}
171
172
	/*
173
	 * getBlackListedPageTypes optionally configured by the developer
174
	 * Includes blacklisted page types defined in the old exclude_from_page_types array
175
	 */
176
	public function getBlackListedPageTypes()
177
	{
178
		$config = $this->config()->get('options');
179
		$legacy = isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : [];
180
		$current = isset($config['pagetype_blacklist']) ? $config['pagetype_blacklist'] : [];
181
		return array_merge($legacy, $current);
182
	}
183
184
	/*
185
	 * Usage of extra css classes configurable from yaml
186
	 */
187
	public function getUseExtraCSSClasses()
188
	{
189
		$config = $this->config()->get('options');
190
191
		return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false;
192
	}
193
194
	/*
195
	 * Prefix for the default CSSClasses
196
	 */
197
	public function getPrefixDefaultCSSClasses()
198
	{
199
		$config = $this->config()->get('options');
200
201
		return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false;
202
	}
203
}
204