Completed
Push — master ( 67ead9...61a703 )
by Daniel
12s
created

EditableMultipleOptionField   B

Complexity

Total Complexity 17

Size/Duplication

Total Lines 211
Duplicated Lines 13.27 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 36.14%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 17
dl 28
loc 211
ccs 30
cts 83
cp 0.3614
rs 7.8571
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A doPublish() 0 5 1
A getHasAddableOptions() 0 4 1
A getOptionsMap() 0 9 2
A getDefaultOptions() 0 4 1
A getCMSFields() 0 51 1
B publishOptions() 22 22 4
A doDeleteFromStage() 0 11 2
A delete() 0 12 3
A duplicate() 6 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Base class for multiple option fields such as {@link EditableDropdownField}
5
 * and radio sets.
6
 *
7
 * Implemented as a class but should be viewed as abstract, you should
8
 * instantiate a subclass such as {@link EditableDropdownField}
9
 *
10
 * @see EditableCheckboxGroupField
11
 * @see EditableDropdownField
12
 *
13
 * @package userforms
14
 */
15
16
class EditableMultipleOptionField extends EditableFormField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
19
    /**
20
     * Define this field as abstract (not inherited)
21
     *
22
     * @config
23
     * @var bool
24
     */
25
    private static $abstract = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $abstract is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    private static $has_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
        "Options" => "EditableOption"
29
    );
30
31
    /**
32
     * @return FieldList
33
     */
34
    public function getCMSFields()
35
    {
36
        $this->beforeUpdateCMSFields(function($fields) {
37
            $editableColumns = new GridFieldEditableColumns();
38
            $editableColumns->setDisplayFields(array(
39
                'Title' => array(
40
                    'title' => _t('EditableMultipleOptionField.TITLE', 'Title'),
41
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
                        return TextField::create($column);
43
                    }
44
                ),
45
                'Value' => array(
46
                    'title' => _t('EditableMultipleOptionField.VALUE', 'Value'),
47
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
                        return TextField::create($column);
49
                    }
50
                ),
51
                'Default' => array(
52
                    'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
53
                    'callback' => function ($record, $column, $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
                        return CheckboxField::create($column);
55
                    }
56
                )
57
            ));
58
59
            $optionsConfig = GridFieldConfig::create()
60
                ->addComponents(
61
                    new GridFieldToolbarHeader(),
62
                    new GridFieldTitleHeader(),
63
                    new GridFieldOrderableRows('Sort'),
64
                    $editableColumns,
65
                    new GridFieldButtonRow(),
66
                    new GridFieldAddNewInlineButton(),
67
                    new GridFieldDeleteAction()
68
                );
69
70
            $optionsGrid = GridField::create(
71
                'Options',
72
                _t('EditableFormField.CUSTOMOPTIONS', 'Options'),
73
                $this->Options(),
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
74
                $optionsConfig
75
            );
76
77
            $fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB', 'Options')), 'Main');
78
            $fields->addFieldToTab('Root.Options', $optionsGrid);
79
        });
80
81
        $fields = parent::getCMSFields();
82
83
        return $fields;
84
    }
85
86
    /**
87
     * Publishing Versioning support.
88
     *
89
     * When publishing it needs to handle copying across / publishing
90
     * each of the individual field options
91
     *
92
     * @param string $fromStage
93
     * @param string $toStage
94
     * @param bool $createNewVersion
95
     */
96 2
    public function doPublish($fromStage, $toStage, $createNewVersion = false)
97
    {
98 2
        parent::doPublish($fromStage, $toStage, $createNewVersion);
99 2
        $this->publishOptions($fromStage, $toStage, $createNewVersion);
100 2
    }
101
102
103
    /**
104
     * Publish list options
105
     *
106
     * @param string $fromStage
107
     * @param string $toStage
108
     * @param bool $createNewVersion
109
     */
110 2 View Code Duplication
    protected function publishOptions($fromStage, $toStage, $createNewVersion)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 2
        $seenIDs = array();
113
114
        // Publish all options
115 2
        foreach ($this->Options() as $option) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116 2
            $seenIDs[] = $option->ID;
117 2
            $option->publish($fromStage, $toStage, $createNewVersion);
118
        }
119
120
        // remove any orphans from the "fromStage"
121 2
        $options = Versioned::get_by_stage('EditableOption', $toStage)
122 2
            ->filter('ParentID', $this->ID);
123
124 2
        if (!empty($seenIDs)) {
125 2
            $options = $options->exclude('ID', $seenIDs);
126
        }
127
128 2
        foreach ($options as $rule) {
129
            $rule->deleteFromStage($toStage);
130
        }
131 2
    }
132
133
    /**
134
     * Unpublishing Versioning support
135
     *
136
     * When unpublishing the field it has to remove all options attached
137
     *
138
     * @return void
139
     */
140
    public function doDeleteFromStage($stage)
141
    {
142
        // Remove options
143
        $options = Versioned::get_by_stage('EditableOption', $stage)
144
            ->filter('ParentID', $this->ID);
145
        foreach ($options as $option) {
146
            $option->deleteFromStage($stage);
147
        }
148
149
        parent::doDeleteFromStage($stage);
150
    }
151
152
    /**
153
     * Deletes all the options attached to this field before deleting the
154
     * field. Keeps stray options from floating around
155
     *
156
     * @return void
157
     */
158
    public function delete()
159
    {
160
        $options = $this->Options();
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
161
162
        if ($options) {
163
            foreach ($options as $option) {
164
                $option->delete();
165
            }
166
        }
167
168
        parent::delete();
169
    }
170
171
    /**
172
     * Duplicate a pages content. We need to make sure all the fields attached
173
     * to that page go with it
174
     *
175
     * @return DataObject
176
     */
177 1
    public function duplicate($doWrite = true)
178
    {
179 1
        $clonedNode = parent::duplicate();
180
181 1 View Code Duplication
        foreach ($this->Options() as $field) {
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182 1
            $newField = $field->duplicate(false);
183 1
            $newField->ParentID = $clonedNode->ID;
184 1
            $newField->Version = 0;
185 1
            $newField->write();
186
        }
187
188 1
        return $clonedNode;
189
    }
190
191
    /**
192
     * Return whether or not this field has addable options such as a
193
     * {@link EditableDropdownField} or {@link EditableRadioField}
194
     *
195
     * @return bool
196
     */
197
    public function getHasAddableOptions()
198
    {
199
        return true;
200
    }
201
202
    /**
203
     * Gets map of field options suitable for use in a form
204
     *
205
     * @return array
206
     */
207 3
    protected function getOptionsMap()
208
    {
209 3
        $optionSet = $this->Options();
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
210 3
        $optionMap = $optionSet->map('Value', 'Title');
211 3
        if ($optionMap instanceof SS_Map) {
212 3
            return $optionMap->toArray();
213
        }
214
        return $optionMap;
215
    }
216
217
    /**
218
     * Returns all default options
219
     *
220
     * @return SS_List
221
     */
222 3
    protected function getDefaultOptions()
223
    {
224 3
        return $this->Options()->filter('Default', 1);
0 ignored issues
show
Bug introduced by
The method Options() does not exist on EditableMultipleOptionField. Did you maybe mean publishOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
225
    }
226
}
227