Conditions | 10 |
Paths | 96 |
Total Lines | 89 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false) |
||
35 | { |
||
36 | parent::__construct(); |
||
37 | |||
38 | $this->blockManager = Injector::inst()->get('SheaDawson\\Blocks\\BlockManager'); |
||
39 | $controllerClass = Controller::curr()->class; |
||
40 | // Get available Areas (for page) or all in case of ModelAdmin |
||
41 | if ($controllerClass == 'CMSPageEditController') { |
||
42 | $currentPage = Controller::curr()->currentPage(); |
||
43 | $areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName); |
||
44 | } else { |
||
45 | $areasFieldSource = $this->blockManager->getAreas(); |
||
46 | } |
||
47 | |||
48 | // EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved |
||
49 | if ($editableRows) { |
||
50 | $this->addComponent($editable = new GridFieldEditableColumns()); |
||
51 | $displayfields = array( |
||
52 | 'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
||
53 | 'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'Silverstripe\\Forms\\ReadonlyField'), |
||
54 | 'BlockArea' => array( |
||
55 | 'title' => _t('Block.BlockArea', 'Block Area'), |
||
56 | 'callback' => function () use ($areasFieldSource) { |
||
57 | $areasField = DropdownField::create('BlockArea', 'Block Area', $areasFieldSource); |
||
58 | if (count($areasFieldSource) > 1) { |
||
59 | $areasField->setHasEmptyDefault(true); |
||
60 | } |
||
61 | return $areasField; |
||
62 | }, |
||
63 | ), |
||
64 | 'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
||
65 | 'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
||
66 | ); |
||
67 | |||
68 | if ($aboveOrBelow) { |
||
69 | $displayfields['AboveOrBelow'] = array( |
||
70 | 'title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), |
||
71 | 'callback' => function () { |
||
72 | return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options')); |
||
73 | }, |
||
74 | ); |
||
75 | } |
||
76 | $editable->setDisplayFields($displayfields); |
||
77 | } else { |
||
78 | $this->addComponent($dcols = new GridFieldDataColumns()); |
||
79 | |||
80 | $displayfields = array( |
||
81 | 'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
||
82 | 'Title' => _t('Block.Title', 'Title'), |
||
83 | 'BlockArea' => _t('Block.BlockArea', 'Block Area'), |
||
84 | 'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'SilverStripe\\Forms\\LiteralField'), |
||
85 | 'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'), |
||
86 | ); |
||
87 | $dcols->setDisplayFields($displayfields); |
||
88 | $dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw')); |
||
89 | } |
||
90 | |||
91 | |||
92 | $this->addComponent(new GridFieldButtonRow('before')); |
||
93 | $this->addComponent(new GridFieldToolbarHeader()); |
||
94 | $this->addComponent(new GridFieldDetailForm()); |
||
95 | $this->addComponent($sort = new GridFieldSortableHeader()); |
||
96 | $this->addComponent($filter = new GridFieldFilterHeader()); |
||
97 | $this->addComponent(new GridFieldDetailForm()); |
||
98 | if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) { |
||
99 | $this->addComponent(new GridFieldCopyButton()); |
||
100 | } |
||
101 | |||
102 | $filter->setThrowExceptionOnBadDataType(false); |
||
103 | $sort->setThrowExceptionOnBadDataType(false); |
||
104 | |||
105 | if ($canAdd) { |
||
106 | $multiClass = new GridFieldAddNewMultiClass(); |
||
107 | $classes = $this->blockManager->getBlockClasses(); |
||
108 | $multiClass->setClasses($classes); |
||
109 | $this->addComponent($multiClass); |
||
110 | //$this->addComponent(new GridFieldAddNewButton()); |
||
|
|||
111 | } |
||
112 | |||
113 | if ($canEdit) { |
||
114 | $this->addComponent(new GridFieldEditButton()); |
||
115 | } |
||
116 | |||
117 | if ($canDelete) { |
||
118 | $this->addComponent(new GridFieldDeleteAction(true)); |
||
119 | } |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
155 |
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.