Conditions | 13 |
Paths | 130 |
Total Lines | 142 |
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 |
||
58 | public function getEditForm($id = null, $fields = null) { |
||
59 | // TODO Duplicate record fetching (see parent implementation) |
||
60 | if(!$id) $id = $this->currentPageID(); |
||
61 | $form = parent::getEditForm($id); |
||
62 | |||
63 | // TODO Duplicate record fetching (see parent implementation) |
||
64 | $record = $this->getRecord($id); |
||
65 | |||
66 | if($record && !$record->canView()) { |
||
67 | return Security::permissionFailure($this); |
||
68 | } |
||
69 | |||
70 | $memberList = GridField::create( |
||
71 | 'Members', |
||
72 | false, |
||
73 | Member::get(), |
||
74 | $memberListConfig = GridFieldConfig_RecordEditor::create() |
||
75 | ->addComponent(new GridFieldButtonRow('after')) |
||
76 | ->addComponent(new GridFieldExportButton('buttons-after-left')) |
||
77 | )->addExtraClass("members_grid"); |
||
78 | |||
79 | if($record && method_exists($record, 'getValidator')) { |
||
80 | $validator = $record->getValidator(); |
||
81 | } else { |
||
82 | $validator = Injector::inst()->get('Member')->getValidator(); |
||
83 | } |
||
84 | |||
85 | $memberListConfig |
||
|
|||
86 | ->getComponentByType('GridFieldDetailForm') |
||
87 | ->setValidator($validator); |
||
88 | |||
89 | $groupList = GridField::create( |
||
90 | 'Groups', |
||
91 | false, |
||
92 | Group::get(), |
||
93 | GridFieldConfig_RecordEditor::create() |
||
94 | ); |
||
95 | $columns = $groupList->getConfig()->getComponentByType('GridFieldDataColumns'); |
||
96 | $columns->setDisplayFields(array( |
||
97 | 'Title' => singleton('Group')->fieldLabel('Title') |
||
98 | )); |
||
99 | $columns->setFieldFormatting(array( |
||
100 | 'Title' => function($val, $item) { |
||
101 | return Convert::raw2xml($item->getBreadcrumbs(' > ')); |
||
102 | } |
||
103 | )); |
||
104 | |||
105 | $fields = new FieldList( |
||
106 | $root = new TabSet( |
||
107 | 'Root', |
||
108 | $usersTab = new Tab('Users', _t('SecurityAdmin.Users', 'Users'), |
||
109 | $memberList, |
||
110 | new LiteralField('MembersCautionText', |
||
111 | sprintf('<p class="caution-remove"><strong>%s</strong></p>', |
||
112 | _t( |
||
113 | 'SecurityAdmin.MemberListCaution', |
||
114 | 'Caution: Removing members from this list will remove them from all groups and the' |
||
115 | . ' database' |
||
116 | ) |
||
117 | ) |
||
118 | ) |
||
119 | ), |
||
120 | $groupsTab = new Tab('Groups', singleton('Group')->i18n_plural_name(), |
||
121 | $groupList |
||
122 | ) |
||
123 | ), |
||
124 | // necessary for tree node selection in LeftAndMain.EditForm.js |
||
125 | new HiddenField('ID', false, 0) |
||
126 | ); |
||
127 | |||
128 | // Add import capabilities. Limit to admin since the import logic can affect assigned permissions |
||
129 | if(Permission::check('ADMIN')) { |
||
130 | $fields->addFieldsToTab('Root.Users', array( |
||
131 | new HeaderField(_t('SecurityAdmin.IMPORTUSERS', 'Import users'), 3), |
||
132 | new LiteralField( |
||
133 | 'MemberImportFormIframe', |
||
134 | sprintf( |
||
135 | '<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
136 | . '</iframe>', |
||
137 | $this->Link('memberimport') |
||
138 | ) |
||
139 | ) |
||
140 | )); |
||
141 | $fields->addFieldsToTab('Root.Groups', array( |
||
142 | new HeaderField(_t('SecurityAdmin.IMPORTGROUPS', 'Import groups'), 3), |
||
143 | new LiteralField( |
||
144 | 'GroupImportFormIframe', |
||
145 | sprintf( |
||
146 | '<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
147 | . '</iframe>', |
||
148 | $this->Link('groupimport') |
||
149 | ) |
||
150 | ) |
||
151 | )); |
||
152 | } |
||
153 | |||
154 | // Tab nav in CMS is rendered through separate template |
||
155 | $root->setTemplate('CMSTabSet'); |
||
156 | |||
157 | // Add roles editing interface |
||
158 | if(Permission::check('APPLY_ROLES')) { |
||
159 | $rolesField = GridField::create('Roles', |
||
160 | false, |
||
161 | PermissionRole::get(), |
||
162 | GridFieldConfig_RecordEditor::create() |
||
163 | ); |
||
164 | |||
165 | $rolesTab = $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.TABROLES', 'Roles')); |
||
166 | $rolesTab->push($rolesField); |
||
167 | } |
||
168 | |||
169 | $actionParam = $this->getRequest()->param('Action'); |
||
170 | if($actionParam == 'groups') { |
||
171 | $groupsTab->addExtraClass('ui-state-active'); |
||
172 | } elseif($actionParam == 'users') { |
||
173 | $usersTab->addExtraClass('ui-state-active'); |
||
174 | } elseif($actionParam == 'roles' && isset($rolesTab)) { |
||
175 | $rolesTab->addExtraClass('ui-state-active'); |
||
176 | } |
||
177 | |||
178 | $actions = new FieldList(); |
||
179 | |||
180 | $form = CMSForm::create( |
||
181 | $this, |
||
182 | 'EditForm', |
||
183 | $fields, |
||
184 | $actions |
||
185 | )->setHTMLID('Form_EditForm'); |
||
186 | $form->setResponseNegotiator($this->getResponseNegotiator()); |
||
187 | $form->addExtraClass('cms-edit-form'); |
||
188 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
189 | // Tab nav in CMS is rendered through separate template |
||
190 | if($form->Fields()->hasTabset()) { |
||
191 | $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); |
||
192 | } |
||
193 | $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses()); |
||
194 | $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
||
195 | |||
196 | $this->extend('updateEditForm', $form); |
||
197 | |||
198 | return $form; |
||
199 | } |
||
200 | |||
375 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: