| Conditions | 8 |
| Paths | 13 |
| Total Lines | 64 |
| Code Lines | 36 |
| 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 |
||
| 21 | protected function getFormActions() |
||
| 22 | { |
||
| 23 | $actions = parent::getFormActions(); |
||
| 24 | |||
| 25 | // Check if record is versionable |
||
| 26 | /** @var Versioned|DataObject $record */ |
||
| 27 | $record = $this->getRecord(); |
||
| 28 | if (!$record || !$record->has_extension(Versioned::class)) { |
||
|
|
|||
| 29 | return $actions; |
||
| 30 | } |
||
| 31 | |||
| 32 | // Save & Publish action |
||
| 33 | if ($record->canPublish()) { |
||
| 34 | // "publish", as with "save", it supports an alternate state to show when action is needed. |
||
| 35 | $publish = FormAction::create( |
||
| 36 | 'doPublish', |
||
| 37 | _t('VersionedGridFieldItemRequest.BUTTONPUBLISH', 'Publish') |
||
| 38 | ) |
||
| 39 | ->setUseButtonTag(true) |
||
| 40 | ->addExtraClass('ss-ui-action-constructive') |
||
| 41 | ->setAttribute('data-icon', 'accept'); |
||
| 42 | |||
| 43 | // Insert after save |
||
| 44 | if ($actions->fieldByName('action_doSave')) { |
||
| 45 | $actions->insertAfter('action_doSave', $publish); |
||
| 46 | } else { |
||
| 47 | $actions->push($publish); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | // Unpublish action |
||
| 52 | $isPublished = $record->isPublished(); |
||
| 53 | if ($isPublished && $record->canUnpublish()) { |
||
| 54 | $actions->push( |
||
| 55 | FormAction::create( |
||
| 56 | 'doUnpublish', |
||
| 57 | _t('VersionedGridFieldItemRequest.BUTTONUNPUBLISH', 'Unpublish') |
||
| 58 | ) |
||
| 59 | ->setUseButtonTag(true) |
||
| 60 | ->setDescription(_t( |
||
| 61 | 'VersionedGridFieldItemRequest.BUTTONUNPUBLISHDESC', |
||
| 62 | 'Remove this record from the published site' |
||
| 63 | )) |
||
| 64 | ->addExtraClass('ss-ui-action-destructive') |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Archive action |
||
| 69 | if ($record->canArchive()) { |
||
| 70 | // Replace "delete" action |
||
| 71 | $actions->removeByName('action_doDelete'); |
||
| 72 | |||
| 73 | // "archive" |
||
| 74 | $actions->push( |
||
| 75 | FormAction::create('doArchive', _t('VersionedGridFieldItemRequest.ARCHIVE', 'Archive')) |
||
| 76 | ->setDescription(_t( |
||
| 77 | 'VersionedGridFieldItemRequest.BUTTONARCHIVEDESC', |
||
| 78 | 'Unpublish and send to archive' |
||
| 79 | )) |
||
| 80 | ->addExtraClass('delete ss-ui-action-destructive') |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | return $actions; |
||
| 84 | } |
||
| 85 | |||
| 201 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: