BlocksAdmin::getList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php use SaltedHerring\Debugger as Debugger;
2
/**
3
 * @file BlocksAdmin.php
4
 *
5
 * Left-hand-side tab : Admin Blocks
6
 * */
7
class BlocksAdmin extends ModelAdmin {
8
	private static $managed_models = array('Block');
9
	private static $url_segment = 'blocks';
10
	private static $menu_title = 'Blocks';
11
	private static $menu_priority = 10;
12
	private static $menu_icon = 'silverstripe-block/images/icon-block.png';
13
		
14
	public function getEditForm($id = null, $fields = null) {
15
		$form = parent::getEditForm($id, $fields);
16
		
17
		$grid = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
18
		
19
		$grid->getConfig()
20
			->removeComponentsByType('GridFieldPaginator')
21
			->removeComponentsByType('GridFieldAddNewButton')
22
			->removeComponentsByType('GridFieldPrintButton')
23
			->removeComponentsByType('GridFieldExportButton')
24
			->removeComponentsByType('GridFieldDetailForm')
25
			->addComponents(
26
				new VersionedForm(),
27
				new GridFieldPaginatorWithShowAll(30),
28
				$multiClass = new MultiClassSelector(),
29
				$sortable = new GridFieldOrderableRows('SortOrder')
30
			);
31
					
32
		$subBlocks = self::getAvaiableTypes();
33
		$multiClass->setClasses($subBlocks);
34
		$grid->setTitle('All Blcoks');
35
		return $form;
36
	}
37
	
38
	public function getList() {
39
		$list = Versioned::get_by_stage('Block', 'Stage');
40
		
41
		return $list;
42
    }
43
	
44
	public static function getAvaiableTypes() {
45
		$subBlocks = ClassInfo::subclassesFor('Block');
46
		if (is_null($subBlocks)) {
47
			$subBlocks = array('Block');
48
		}else{
49
			$disabledTypes = Config::inst()->get('Block','DisabledBlocks');
50
			if (!empty($disabledTypes)) {
51
				foreach ($disabledTypes as $disabledType) {
0 ignored issues
show
Bug introduced by
The expression $disabledTypes of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
					unset($subBlocks[$disabledType]);
53
				}
54
			}
55
			foreach ($subBlocks as $key => &$value) {
56
				$value = empty($key::$singular_name) ? ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $value)))) : $key::$singular_name;
57
			}
58
		}
59
		
60
		return $subBlocks;
61
	}
62
	
63
	
64
}
65