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.
Completed
Push — master ( 9df84b...567f57 )
by Shea
02:05
created

BlockManager::getWhiteListedPageTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * BlockManager.
4
 *
5
 * @author Shea Dawson <[email protected]>
6
 */
7
class BlockManager extends Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
	/**
10
	 * Define areas and config on a per theme basis.
11
	 *
12
	 * @var array
13
	 **/
14
	private static $themes = array();
0 ignored issues
show
Unused Code introduced by
The property $themes 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...
15
16
	/**
17
	 * Use default ContentBlock class.
18
	 *
19
	 * @var bool
20
	 **/
21
	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...
22
23
	/**
24
	 * Show a block area preview button in CMS
25
	 *
26
	 * @var bool
27
	 **/
28
	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...
29
30
	public function __construct()
31
	{
32
		parent::__construct();
33
	}
34
35
	/**
36
	 * Gets an array of all areas defined for the current theme.
37
	 *
38
	 * @param string $theme
39
	 * @param bool   $keyAsValue
40
	 *
41
	 * @return array $areas
42
	 **/
43
	public function getAreasForTheme($theme = null, $keyAsValue = true)
44
	{
45
		$theme = $theme ? $theme : $this->getTheme();
46
		if (!$theme) {
47
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by BlockManager::getAreasForTheme of type array.

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...
48
		}
49
		$config = $this->config()->get('themes');
50
		if (!isset($config[$theme]['areas'])) {
51
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by BlockManager::getAreasForTheme of type array.

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...
52
		}
53
		$areas = $config[$theme]['areas'];
54
		$areas = $keyAsValue ? ArrayLib::valuekey(array_keys($areas)) : $areas;
55
		if (count($areas)) {
56
			foreach ($areas as $k => $v) {
57
				$areas[$k] = $keyAsValue ? FormField::name_to_label($k) : $v;
58
			}
59
		}
60
61
		return $areas;
62
	}
63
64
	/**
65
	 * Gets an array of all areas defined for the current theme that are compatible
66
	 * with pages of type $class.
67
	 *
68
	 * @param string $class
69
	 *
70
	 * @return array $areas
71
	 **/
72
	public function getAreasForPageType($class)
73
	{
74
		$areas = $this->getAreasForTheme(null, false);
75
76
		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...
77
			return false;
78
		}
79
80
		foreach ($areas as $area => $config) {
81
			if (!is_array($config)) {
82
				continue;
83
			}
84
85 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...
86
				$except = $config['except'];
87
				if (is_array($except)
88
					? in_array($class, $except)
89
					: $except == $class
90
				) {
91
					unset($areas[$area]);
92
					continue;
93
				}
94
			}
95
96 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...
97
				$only = $config['only'];
98
				if (is_array($only)
99
					? !in_array($class, $only)
100
					: $only != $class
101
				) {
102
					unset($areas[$area]);
103
					continue;
104
				}
105
			}
106
		}
107
108
		if (count($areas)) {
109
			foreach ($areas as $k => $v) {
110
				$areas[$k] = _t('Block.BlockAreaName.'.$k, FormField::name_to_label($k));
111
			}
112
113
			return $areas;
114
		} else {
115
			return $areas;
116
		}
117
	}
118
119
	public function getBlockClasses()
120
	{
121
		$classes = ArrayLib::valuekey(ClassInfo::subclassesFor('Block'));
122
		array_shift($classes);
123
		foreach ($classes as $k => $v) {
124
			$classes[$k] = singleton($k)->singular_name();
125
		}
126
127
		if (!Config::inst()->get('BlockManager', 'use_default_blocks')) {
128
			unset($classes['ContentBlock']);
129
		}
130
131
		if ($disabledArr = Config::inst()->get('BlockManager', 'disabled_blocks')) {
132
			foreach ($disabledArr as $k => $v) {
133
				unset($classes[$v]);
134
			}
135
		}
136
137
		return $classes;
138
	}
139
140
	/*
141
	 * Get the current/active theme or 'default' to support theme-less sites
142
	 */
143
	private function getTheme()
144
	{
145
		$currentTheme = Config::inst()->get('SSViewer', 'theme');
146
147
		// check directly on SiteConfig incase ContentController hasn't set
148
		// the theme yet in ContentController->init()
149
		if (!$currentTheme && class_exists('SiteConfig')) {
150
			$currentTheme = SiteConfig::current_site_config()->Theme;
151
		}
152
153
		return $currentTheme ? $currentTheme : 'default';
154
	}
155
156
	/*
157
	 * Get the block config for the current theme
158
	 */
159
	private function getThemeConfig()
160
	{
161
		$theme = $this->getTheme();
162
		$config = $this->config()->get('themes');
163
164
		return $theme && isset($config[$theme]) ? $config[$theme] : null;
165
	}
166
167
	/*
168
	 * Usage of BlockSets configurable from yaml
169
	 */
170
	public function getUseBlockSets()
171
	{
172
		$config = $this->getThemeConfig();
173
174
		return isset($config['use_blocksets']) ? $config['use_blocksets'] : true;
175
	}
176
177
	/*
178
	 * Exclusion of blocks from page types defined in yaml
179
	 */
180
	public function getExcludeFromPageTypes()
181
	{
182
		$config = $this->getThemeConfig();
183
184
		return isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : array();
185
	}
186
187
	/*
188
	 * getWhiteListedPageTypes optionally configured by the developer
189
	 */
190
	public function getWhiteListedPageTypes()
191
	{
192
		$config = $this->getThemeConfig();
193
		return isset($config['pagetype_whitelist']) ? $config['pagetype_whitelist'] : array();
194
	}
195
196
	/*
197
	 * getBlackListedPageTypes optionally configured by the developer
198
	 * Includes blacklisted page types defined in the old exclude_from_page_types array
199
	 */
200
	public function getBlackListedPageTypes()
201
	{
202
		$config = $this->getThemeConfig();
203
		$legacy = isset($config['exclude_from_page_types']) ? $config['exclude_from_page_types'] : array();
204
		$current = isset($config['pagetype_blacklist']) ? $config['pagetype_blacklist'] : array();
205
		return array_merge($legacy, $current);
206
	}
207
208
	/*
209
	 * Usage of extra css classes configurable from yaml
210
	 */
211
	public function getUseExtraCSSClasses()
212
	{
213
		$config = $this->getThemeConfig();
214
215
		return isset($config['use_extra_css_classes']) ? $config['use_extra_css_classes'] : false;
216
	}
217
218
	/*
219
	 * Prefix for the default CSSClasses
220
	 */
221
	public function getPrefixDefaultCSSClasses()
222
	{
223
		$config = $this->getThemeConfig();
224
225
		return isset($config['prefix_default_css_classes']) ? $config['prefix_default_css_classes'] : false;
226
	}
227
}
228