Completed
Pull Request — master (#518)
by Sam
34:41
created

EditableMultipleOptionField   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 3.11 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 5
dl 6
loc 193
rs 10
c 0
b 0
f 0

8 Methods

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

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
use SilverStripe\Forms\TextField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TextField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use SilverStripe\Forms\CheckboxField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CheckboxField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use SilverStripe\Forms\GridField\GridFieldConfig;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldConfig.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldToolbarHeader.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use SilverStripe\Forms\GridField\GridFieldButtonRow;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldButtonRow.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridFieldDeleteAction.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use SilverStripe\Forms\GridField\GridField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, GridField.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use SilverStripe\Forms\Tab;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tab.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use SilverStripe\ORM\Versioning\Versioned;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Versioned.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use SilverStripe\ORM\Map;
13
14
/**
15
 * Base class for multiple option fields such as {@link EditableDropdownField}
16
 * and radio sets.
17
 *
18
 * Implemented as a class but should be viewed as abstract, you should
19
 * instantiate a subclass such as {@link EditableDropdownField}
20
 *
21
 * @see EditableCheckboxGroupField
22
 * @see EditableDropdownField
23
 *
24
 * @package userforms
25
 */
26
27
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...
28
{
29
30
    /**
31
     * Define this field as abstract (not inherited)
32
     *
33
     * @config
34
     * @var bool
35
     */
36
    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...
37
38
    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...
39
        "Options" => "EditableOption"
40
    );
41
42
    private static $owns = [
0 ignored issues
show
Unused Code introduced by
The property $owns 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...
43
        "Options",
44
    ];
45
46
    /**
47
     * @return FieldList
48
     */
49
    public function getCMSFields()
50
    {
51
        $fields = parent::getCMSFields();
52
53
        $editableColumns = new GridFieldEditableColumns();
54
        $editableColumns->setDisplayFields(array(
55
            'Title' => array(
56
                'title' => _t('EditableMultipleOptionField.TITLE', 'Title'),
57
                '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...
58
                    return TextField::create($column);
59
                }
60
            ),
61
            'Value' => array(
62
                'title' => _t('EditableMultipleOptionField.VALUE', 'Value'),
63
                '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...
64
                    return TextField::create($column);
65
                }
66
            ),
67
            'Default' => array(
68
                'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
69
                '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...
70
                    return CheckboxField::create($column);
71
                }
72
            )
73
        ));
74
75
        $optionsConfig = GridFieldConfig::create()
76
            ->addComponents(
77
                new GridFieldToolbarHeader(),
78
                new GridFieldTitleHeader(),
79
                new GridFieldOrderableRows('Sort'),
80
                $editableColumns,
81
                new GridFieldButtonRow(),
82
                new GridFieldAddNewInlineButton(),
83
                new GridFieldDeleteAction()
84
            );
85
86
        $optionsGrid = GridField::create(
87
            'Options',
88
            _t('EditableFormField.CUSTOMOPTIONS', 'Options'),
89
            $this->Options(),
90
            $optionsConfig
91
        );
92
93
        $fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB', 'Options')), 'Main');
94
        $fields->addFieldToTab('Root.Options', $optionsGrid);
95
96
        return $fields;
97
    }
98
99
    /**
100
     * Publishing Versioning support.
101
     *
102
     * When publishing it needs to handle copying across / publishing
103
     * each of the individual field options
104
     *
105
     * @return void
106
     */
107
    public function doPublish($fromStage, $toStage, $createNewVersion = false)
108
    {
109
        $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
110
111
        if ($live) {
112
            foreach ($live as $option) {
113
                $option->delete();
114
            }
115
        }
116
117
        if ($this->Options()) {
118
            foreach ($this->Options() as $option) {
119
                $option->publish($fromStage, $toStage, $createNewVersion);
120
            }
121
        }
122
123
        parent::doPublish($fromStage, $toStage, $createNewVersion);
124
    }
125
126
    /**
127
     * Unpublishing Versioning support
128
     *
129
     * When unpublishing the field it has to remove all options attached
130
     *
131
     * @return void
132
     */
133
    public function doDeleteFromStage($stage)
134
    {
135
        // Remove options
136
        $options = Versioned::get_by_stage('EditableOption', $stage)
137
            ->filter('ParentID', $this->ID);
138
        foreach ($options as $option) {
139
            $option->deleteFromStage($stage);
140
        }
141
142
        parent::doDeleteFromStage($stage);
143
    }
144
145
    /**
146
     * Deletes all the options attached to this field before deleting the
147
     * field. Keeps stray options from floating around
148
     *
149
     * @return void
150
     */
151
    public function delete()
152
    {
153
        $options = $this->Options();
154
155
        if ($options) {
156
            foreach ($options as $option) {
157
                $option->delete();
158
            }
159
        }
160
161
        parent::delete();
162
    }
163
164
    /**
165
     * Duplicate a pages content. We need to make sure all the fields attached
166
     * to that page go with it
167
     *
168
     * @return DataObject
169
     */
170
    public function duplicate($doWrite = true)
171
    {
172
        $clonedNode = parent::duplicate();
173
174 View Code Duplication
        foreach ($this->Options() as $field) {
0 ignored issues
show
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...
175
            $newField = $field->duplicate(false);
176
            $newField->ParentID = $clonedNode->ID;
177
            $newField->Version = 0;
178
            $newField->write();
179
        }
180
181
        return $clonedNode;
182
    }
183
184
    /**
185
     * Return whether or not this field has addable options such as a
186
     * {@link EditableDropdownField} or {@link EditableRadioField}
187
     *
188
     * @return bool
189
     */
190
    public function getHasAddableOptions()
191
    {
192
        return true;
193
    }
194
195
    /**
196
     * Gets map of field options suitable for use in a form
197
     *
198
     * @return array
199
     */
200
    protected function getOptionsMap()
201
    {
202
        $optionSet = $this->Options();
203
        $optionMap = $optionSet->map('Value', 'Title');
204
        if ($optionMap instanceof Map) {
0 ignored issues
show
Bug introduced by
The class SilverStripe\ORM\Map does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
205
            return $optionMap->toArray();
206
        }
207
        return $optionMap;
208
    }
209
210
    /**
211
     * Returns all default options
212
     *
213
     * @return SS_List
214
     */
215
    protected function getDefaultOptions()
216
    {
217
        return $this->Options()->filter('Default', 1);
218
    }
219
}
220