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