| Conditions | 12 |
| Paths | 130 |
| Total Lines | 141 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Shortcut action for setting the correct active tab. |
||
| 77 | * |
||
| 78 | * @param SS_HTTPRequest $request |
||
| 79 | * @return SS_HTTPResponse |
||
| 80 | */ |
||
| 81 | public function groups($request) { |
||
| 82 | return $this->index($request); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Shortcut action for setting the correct active tab. |
||
| 87 | * |
||
| 88 | * @param SS_HTTPRequest $request |
||
| 89 | * @return SS_HTTPResponse |
||
| 90 | */ |
||
| 91 | public function roles($request) { |
||
| 92 | return $this->index($request); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getEditForm($id = null, $fields = null) { |
||
| 96 | // TODO Duplicate record fetching (see parent implementation) |
||
| 97 | if(!$id) $id = $this->currentPageID(); |
||
| 98 | $form = parent::getEditForm($id); |
||
| 99 | |||
| 100 | // TODO Duplicate record fetching (see parent implementation) |
||
| 101 | $record = $this->getRecord($id); |
||
| 102 | |||
| 103 | if($record && !$record->canView()) { |
||
| 104 | return Security::permissionFailure($this); |
||
| 105 | } |
||
| 106 | |||
| 107 | $memberList = GridField::create( |
||
| 108 | 'Members', |
||
| 109 | false, |
||
| 110 | Member::get(), |
||
| 111 | $memberListConfig = GridFieldConfig_RecordEditor::create() |
||
| 112 | ->addComponent(new GridFieldButtonRow('after')) |
||
| 113 | ->addComponent(new GridFieldExportButton('buttons-after-left')) |
||
| 114 | )->addExtraClass("members_grid"); |
||
| 115 | |||
| 116 | if($record && method_exists($record, 'getValidator')) { |
||
| 117 | $validator = $record->getValidator(); |
||
| 118 | } else { |
||
| 119 | $validator = Member::singleton()->getValidator(); |
||
| 120 | } |
||
| 121 | |||
| 122 | $memberListConfig |
||
|
|
|||
| 123 | ->getComponentByType('GridFieldDetailForm') |
||
| 124 | ->setValidator($validator); |
||
| 125 | |||
| 126 | $groupList = GridField::create( |
||
| 127 | 'Groups', |
||
| 128 | false, |
||
| 129 | Group::get(), |
||
| 130 | GridFieldConfig_RecordEditor::create() |
||
| 131 | ); |
||
| 132 | $columns = $groupList->getConfig()->getComponentByType('GridFieldDataColumns'); |
||
| 133 | $columns->setDisplayFields(array( |
||
| 134 | 'Breadcrumbs' => Group::singleton()->fieldLabel('Title') |
||
| 135 | )); |
||
| 136 | $columns->setFieldFormatting(array( |
||
| 137 | 'Breadcrumbs' => function($val, $item) { |
||
| 138 | return Convert::raw2xml($item->getBreadcrumbs(' > ')); |
||
| 139 | } |
||
| 140 | )); |
||
| 141 | |||
| 142 | $fields = new FieldList( |
||
| 143 | $root = new TabSet( |
||
| 144 | 'Root', |
||
| 145 | $usersTab = new Tab('Users', _t('SecurityAdmin.Users', 'Users'), |
||
| 146 | |||
| 147 | new LiteralField('MembersCautionText', |
||
| 148 | sprintf('<div class="alert alert-warning" role="alert">%s</div>', |
||
| 149 | _t( |
||
| 150 | 'SecurityAdmin.MemberListCaution', |
||
| 151 | 'Caution: Removing members from this list will remove them from all groups and the database' |
||
| 152 | ) |
||
| 153 | ) |
||
| 154 | ), |
||
| 155 | $memberList |
||
| 156 | ), |
||
| 157 | $groupsTab = new Tab('Groups', singleton('SilverStripe\\Security\\Group')->i18n_plural_name(), |
||
| 158 | $groupList |
||
| 159 | ) |
||
| 160 | ), |
||
| 161 | // necessary for tree node selection in LeftAndMain.EditForm.js |
||
| 162 | new HiddenField('ID', false, 0) |
||
| 163 | ); |
||
| 164 | |||
| 165 | // Add import capabilities. Limit to admin since the import logic can affect assigned permissions |
||
| 166 | if(Permission::check('ADMIN')) { |
||
| 167 | $fields->addFieldsToTab('Root.Users', array( |
||
| 168 | new HeaderField(_t('SecurityAdmin.IMPORTUSERS', 'Import users'), 3), |
||
| 169 | new LiteralField( |
||
| 170 | 'MemberImportFormIframe', |
||
| 171 | sprintf( |
||
| 172 | '<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 173 | . '</iframe>', |
||
| 174 | $this->Link('memberimport') |
||
| 175 | ) |
||
| 176 | ) |
||
| 177 | )); |
||
| 178 | $fields->addFieldsToTab('Root.Groups', array( |
||
| 179 | new HeaderField(_t('SecurityAdmin.IMPORTGROUPS', 'Import groups'), 3), |
||
| 180 | new LiteralField( |
||
| 181 | 'GroupImportFormIframe', |
||
| 182 | sprintf( |
||
| 183 | '<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="250px" frameBorder="0">' |
||
| 184 | . '</iframe>', |
||
| 185 | $this->Link('groupimport') |
||
| 186 | ) |
||
| 187 | ) |
||
| 188 | )); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Tab nav in CMS is rendered through separate template |
||
| 192 | $root->setTemplate('CMSTabSet'); |
||
| 193 | |||
| 194 | // Add roles editing interface |
||
| 195 | if(Permission::check('APPLY_ROLES')) { |
||
| 196 | $rolesField = GridField::create('Roles', |
||
| 197 | false, |
||
| 198 | PermissionRole::get(), |
||
| 199 | GridFieldConfig_RecordEditor::create() |
||
| 200 | ); |
||
| 201 | |||
| 202 | $rolesTab = $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.TABROLES', 'Roles')); |
||
| 203 | $rolesTab->push($rolesField); |
||
| 204 | } |
||
| 205 | |||
| 206 | $actionParam = $this->getRequest()->param('Action'); |
||
| 207 | if($actionParam == 'groups') { |
||
| 208 | $groupsTab->addExtraClass('ui-state-active'); |
||
| 209 | } elseif($actionParam == 'users') { |
||
| 210 | $usersTab->addExtraClass('ui-state-active'); |
||
| 211 | } elseif($actionParam == 'roles') { |
||
| 212 | $rolesTab->addExtraClass('ui-state-active'); |
||
| 213 | } |
||
| 214 | |||
| 215 | $actions = new FieldList(); |
||
| 366 |
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: