| Conditions | 6 | 
| Paths | 8 | 
| Total Lines | 122 | 
| Code Lines | 82 | 
| Lines | 13 | 
| Ratio | 10.66 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 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  | 
            ||
| 19 | 	public function AddForm() { | 
            ||
| 20 | $pageTypes = array();  | 
            ||
| 21 | 		foreach($this->PageTypes() as $type) { | 
            ||
| 22 | 			$html = sprintf('<span class="page-icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>', | 
            ||
| 23 | 				$type->getField('ClassName'), | 
            ||
| 24 | 				$type->getField('AddAction'), | 
            ||
| 25 | 				$type->getField('Description') | 
            ||
| 26 | );  | 
            ||
| 27 | 			$pageTypes[$type->getField('ClassName')] = $html; | 
            ||
| 28 | }  | 
            ||
| 29 | // Ensure generic page type shows on top  | 
            ||
| 30 | 		if(isset($pageTypes['Page'])) { | 
            ||
| 31 | $pageTitle = $pageTypes['Page'];  | 
            ||
| 32 | 			$pageTypes = array_merge(array('Page' => $pageTitle), $pageTypes); | 
            ||
| 33 | }  | 
            ||
| 34 | |||
| 35 | $numericLabelTmpl = '<span class="step-label"><span class="flyout">%d</span><span class="arrow"></span><span class="title">%s</span></span>';  | 
            ||
| 36 | |||
| 37 | 		$topTitle = _t('CMSPageAddController.ParentMode_top', 'Top level'); | 
            ||
| 38 | 		$childTitle = _t('CMSPageAddController.ParentMode_child', 'Under another page'); | 
            ||
| 39 | |||
| 40 | $fields = new FieldList(  | 
            ||
| 41 | 			new LiteralField('PageModeHeader', sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page'))), | 
            ||
| 42 | $parentModeField = new SelectionGroup(  | 
            ||
| 43 | "ParentModeField",  | 
            ||
| 44 | array(  | 
            ||
| 45 | new SelectionGroup_Item(  | 
            ||
| 46 | "top",  | 
            ||
| 47 | null,  | 
            ||
| 48 | $topTitle  | 
            ||
| 49 | ),  | 
            ||
| 50 | new SelectionGroup_Item(  | 
            ||
| 51 | 'child',  | 
            ||
| 52 | $parentField = new TreeDropdownField(  | 
            ||
| 53 | "ParentID",  | 
            ||
| 54 | "",  | 
            ||
| 55 | 'SiteTree',  | 
            ||
| 56 | 'ID',  | 
            ||
| 57 | 'TreeTitle'  | 
            ||
| 58 | ),  | 
            ||
| 59 | $childTitle  | 
            ||
| 60 | )  | 
            ||
| 61 | )  | 
            ||
| 62 | ),  | 
            ||
| 63 | $typeField = new OptionsetField(  | 
            ||
| 64 | "PageType",  | 
            ||
| 65 | 				sprintf($numericLabelTmpl, 2, _t('CMSMain.ChoosePageType', 'Choose page type')), | 
            ||
| 66 | $pageTypes,  | 
            ||
| 67 | 'Page'  | 
            ||
| 68 | ),  | 
            ||
| 69 | new LiteralField(  | 
            ||
| 70 | 'RestrictedNote',  | 
            ||
| 71 | sprintf(  | 
            ||
| 72 | '<p class="message notice message-restricted">%s</p>',  | 
            ||
| 73 | _t(  | 
            ||
| 74 | 'CMSMain.AddPageRestriction',  | 
            ||
| 75 | 'Note: Some page types are not allowed for this selection'  | 
            ||
| 76 | )  | 
            ||
| 77 | )  | 
            ||
| 78 | )  | 
            ||
| 79 | );  | 
            ||
| 80 | 		$parentField->setSearchFunction(function ($sourceObject, $labelField, $search) { | 
            ||
| 81 | return DataObject::get(  | 
            ||
| 82 | $sourceObject,  | 
            ||
| 83 | sprintf(  | 
            ||
| 84 | "\"MenuTitle\" LIKE '%%%s%%' OR \"Title\" LIKE '%%%s%%'",  | 
            ||
| 85 | Convert::raw2sql($search),  | 
            ||
| 86 | Convert::raw2sql($search)  | 
            ||
| 87 | )  | 
            ||
| 88 | );  | 
            ||
| 89 | });  | 
            ||
| 90 | |||
| 91 | // TODO Re-enable search once it allows for HTML title display,  | 
            ||
| 92 | // see http://open.silverstripe.org/ticket/7455  | 
            ||
| 93 | // $parentField->setShowSearch(true);  | 
            ||
| 94 | |||
| 95 | 		$parentModeField->addExtraClass('parent-mode'); | 
            ||
| 96 | |||
| 97 | // CMSMain->currentPageID() automatically sets the homepage,  | 
            ||
| 98 | // which we need to counteract in the default selection (which should default to root, ID=0)  | 
            ||
| 99 | 		if($parentID = $this->getRequest()->getVar('ParentID')) { | 
            ||
| 100 | 			$parentModeField->setValue('child'); | 
            ||
| 101 | $parentField->setValue((int)$parentID);  | 
            ||
| 102 | 		} else { | 
            ||
| 103 | 			$parentModeField->setValue('top'); | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | $actions = new FieldList(  | 
            ||
| 107 | 			FormAction::create("doAdd", _t('CMSMain.Create',"Create")) | 
            ||
| 108 | 				->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept') | 
            ||
| 109 | ->setUseButtonTag(true),  | 
            ||
| 110 | 			FormAction::create("doCancel", _t('CMSMain.Cancel',"Cancel")) | 
            ||
| 111 | 				->addExtraClass('ss-ui-action-destructive ss-ui-action-cancel') | 
            ||
| 112 | ->setUseButtonTag(true)  | 
            ||
| 113 | );  | 
            ||
| 114 | |||
| 115 | 		$this->extend('updatePageOptions', $fields); | 
            ||
| 116 | |||
| 117 | $negotiator = $this->getResponseNegotiator();  | 
            ||
| 118 | $form = Form::create(  | 
            ||
| 119 | $this, "AddForm", $fields, $actions  | 
            ||
| 120 | 		)->setHTMLID('Form_AddForm'); | 
            ||
| 121 | 		$form->setAttribute('data-hints', $this->SiteTreeHints()); | 
            ||
| 122 | 		$form->setAttribute('data-childfilter', $this->Link('childfilter')); | 
            ||
| 123 | View Code Duplication | 		$form->setValidationResponseCallback(function() use ($negotiator, $form) { | 
            |
| 124 | $request = $this->getRequest();  | 
            ||
| 125 | 			if($request->isAjax() && $negotiator) { | 
            ||
| 126 | $this->setupFormErrors();  | 
            ||
| 127 | $result = $this->forTemplate();  | 
            ||
| 128 | |||
| 129 | return $negotiator->respond($request, array(  | 
            ||
| 130 | 					'CurrentForm' => function() use($result) { | 
            ||
| 131 | return $result;  | 
            ||
| 132 | }  | 
            ||
| 133 | ));  | 
            ||
| 134 | }  | 
            ||
| 135 | });  | 
            ||
| 136 | 		$form->addExtraClass('cms-add-form stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses()); | 
            ||
| 137 | 		$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); | 
            ||
| 138 | |||
| 139 | return $form;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 191 |