| Conditions | 6 | 
| Paths | 8 | 
| Total Lines | 131 | 
| Code Lines | 88 | 
| Lines | 12 | 
| Ratio | 9.16 % | 
| 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  | 
            ||
| 41 | public function AddForm()  | 
            ||
| 42 |     { | 
            ||
| 43 | $pageTypes = array();  | 
            ||
| 44 |         foreach ($this->PageTypes() as $type) { | 
            ||
| 45 | $html = sprintf(  | 
            ||
| 46 | '<span class="page-icon class-%s"></span><span class="title">%s</span><span class="form__field-description">%s</span>',  | 
            ||
| 47 |                 $type->getField('ClassName'), | 
            ||
| 48 |                 $type->getField('AddAction'), | 
            ||
| 49 |                 $type->getField('Description') | 
            ||
| 50 | );  | 
            ||
| 51 |             $pageTypes[$type->getField('ClassName')] = DBField::create_field('HTMLFragment', $html); | 
            ||
| 52 | }  | 
            ||
| 53 | // Ensure generic page type shows on top  | 
            ||
| 54 |         if (isset($pageTypes['Page'])) { | 
            ||
| 55 | $pageTitle = $pageTypes['Page'];  | 
            ||
| 56 |             $pageTypes = array_merge(array('Page' => $pageTitle), $pageTypes); | 
            ||
| 57 | }  | 
            ||
| 58 | |||
| 59 | $numericLabelTmpl = '<span class="step-label"><span class="flyout">Step %d. </span><span class="title">%s</span></span>';  | 
            ||
| 60 | |||
| 61 |         $topTitle = _t('SilverStripe\\CMS\\Controllers\\CMSPageAddController.ParentMode_top', 'Top level'); | 
            ||
| 62 |         $childTitle = _t('SilverStripe\\CMS\\Controllers\\CMSPageAddController.ParentMode_child', 'Under another page'); | 
            ||
| 63 | |||
| 64 | $fields = new FieldList(  | 
            ||
| 65 | $parentModeField = new SelectionGroup(  | 
            ||
| 66 | "ParentModeField",  | 
            ||
| 67 | array(  | 
            ||
| 68 | new SelectionGroup_Item(  | 
            ||
| 69 | "top",  | 
            ||
| 70 | null,  | 
            ||
| 71 | $topTitle  | 
            ||
| 72 | ),  | 
            ||
| 73 | new SelectionGroup_Item(  | 
            ||
| 74 | 'child',  | 
            ||
| 75 | $parentField = new TreeDropdownField(  | 
            ||
| 76 | "ParentID",  | 
            ||
| 77 | "",  | 
            ||
| 78 | 'SilverStripe\\CMS\\Model\\SiteTree',  | 
            ||
| 79 | 'ID',  | 
            ||
| 80 | 'TreeTitle'  | 
            ||
| 81 | ),  | 
            ||
| 82 | $childTitle  | 
            ||
| 83 | )  | 
            ||
| 84 | )  | 
            ||
| 85 | ),  | 
            ||
| 86 | new LiteralField(  | 
            ||
| 87 | 'RestrictedNote',  | 
            ||
| 88 | sprintf(  | 
            ||
| 89 | '<p class="message notice message-restricted">%s</p>',  | 
            ||
| 90 | _t(  | 
            ||
| 91 | 'SilverStripe\\CMS\\Controllers\\CMSMain.AddPageRestriction',  | 
            ||
| 92 | 'Note: Some page types are not allowed for this selection'  | 
            ||
| 93 | )  | 
            ||
| 94 | )  | 
            ||
| 95 | ),  | 
            ||
| 96 | $typeField = new OptionsetField(  | 
            ||
| 97 | "PageType",  | 
            ||
| 98 | DBField::create_field(  | 
            ||
| 99 | 'HTMLFragment',  | 
            ||
| 100 |                     sprintf($numericLabelTmpl, 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type')) | 
            ||
| 101 | ),  | 
            ||
| 102 | $pageTypes,  | 
            ||
| 103 | 'Page'  | 
            ||
| 104 | )  | 
            ||
| 105 | );  | 
            ||
| 106 | |||
| 107 | $parentModeField->setTitle(DBField::create_field(  | 
            ||
| 108 | 'HTMLFragment',  | 
            ||
| 109 |             sprintf($numericLabelTmpl, 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page')) | 
            ||
| 110 | ));  | 
            ||
| 111 | |||
| 112 |         $parentField->setSearchFunction(function ($sourceObject, $labelField, $search) { | 
            ||
| 113 | return DataObject::get($sourceObject)  | 
            ||
| 114 | ->filterAny([  | 
            ||
| 115 | 'MenuTitle:PartialMatch' => $search,  | 
            ||
| 116 | 'Title:PartialMatch' => $search,  | 
            ||
| 117 | ]);  | 
            ||
| 118 | });  | 
            ||
| 119 | |||
| 120 | // TODO Re-enable search once it allows for HTML title display,  | 
            ||
| 121 | // see http://open.silverstripe.org/ticket/7455  | 
            ||
| 122 | // $parentField->setShowSearch(true);  | 
            ||
| 123 | |||
| 124 |         $parentModeField->addExtraClass('parent-mode'); | 
            ||
| 125 | |||
| 126 | // CMSMain->currentPageID() automatically sets the homepage,  | 
            ||
| 127 | // which we need to counteract in the default selection (which should default to root, ID=0)  | 
            ||
| 128 |         if ($parentID = $this->getRequest()->getVar('ParentID')) { | 
            ||
| 129 |             $parentModeField->setValue('child'); | 
            ||
| 130 | $parentField->setValue((int)$parentID);  | 
            ||
| 131 |         } else { | 
            ||
| 132 |             $parentModeField->setValue('top'); | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | $actions = new FieldList(  | 
            ||
| 136 |             FormAction::create("doAdd", _t('SilverStripe\\CMS\\Controllers\\CMSMain.Create', "Create")) | 
            ||
| 137 |                 ->addExtraClass('btn-primary font-icon-plus-circled') | 
            ||
| 138 | ->setUseButtonTag(true),  | 
            ||
| 139 |             FormAction::create("doCancel", _t('SilverStripe\\CMS\\Controllers\\CMSMain.Cancel', "Cancel")) | 
            ||
| 140 |                 ->addExtraClass('btn-secondary') | 
            ||
| 141 | ->setUseButtonTag(true)  | 
            ||
| 142 | );  | 
            ||
| 143 | |||
| 144 |         $this->extend('updatePageOptions', $fields); | 
            ||
| 145 | |||
| 146 | $negotiator = $this->getResponseNegotiator();  | 
            ||
| 147 | $form = Form::create(  | 
            ||
| 148 | $this,  | 
            ||
| 149 | "AddForm",  | 
            ||
| 150 | $fields,  | 
            ||
| 151 | $actions  | 
            ||
| 152 |         )->setHTMLID('Form_AddForm')->setStrictFormMethodCheck(false); | 
            ||
| 153 |         $form->setAttribute('data-hints', $this->SiteTreeHints()); | 
            ||
| 154 |         $form->setAttribute('data-childfilter', $this->Link('childfilter')); | 
            ||
| 155 | View Code Duplication |         $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) { | 
            |
| 156 | $request = $this->getRequest();  | 
            ||
| 157 |             if ($request->isAjax() && $negotiator) { | 
            ||
| 158 | $result = $form->forTemplate();  | 
            ||
| 159 | return $negotiator->respond($request, array(  | 
            ||
| 160 |                     'CurrentForm' => function () use ($result) { | 
            ||
| 161 | return $result;  | 
            ||
| 162 | }  | 
            ||
| 163 | ));  | 
            ||
| 164 | }  | 
            ||
| 165 | return null;  | 
            ||
| 166 | });  | 
            ||
| 167 |         $form->addExtraClass('flexbox-area-grow fill-height cms-add-form cms-content cms-edit-form ' . $this->BaseCSSClasses()); | 
            ||
| 168 |         $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); | 
            ||
| 169 | |||
| 170 | return $form;  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 227 |