Moo_EditableFieldMultipleOption::duplicate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * Moo_EditableFieldMultipleOption is a base class for multiple option fields to extend.
5
 *
6
 * @see     Moo_EditableFieldCheckboxGroup, Moo_EditableFieldDropdown
7
 *
8
 * @package editablefield
9
 *
10
 * @author  Mohamed Alsharaf <[email protected]>
11
 *
12
 * @method  HasManyList Options()
13
 *
14
 * @property string $Name
15
 * @property string $Title
16
 */
17
class Moo_EditableFieldMultipleOption extends Moo_EditableField
18
{
19
    private static $has_many = [
20
        'Options' => 'Moo_EditableFieldOption',
21
    ];
22
23
    /**
24
     * Deletes all the options attached to this field before deleting the
25
     * field. Keeps stray options from floating around.
26
     *
27
     * @return void
28
     */
29
    public function delete()
30
    {
31
        $options = $this->Options();
32
33
        if ($options) {
34
            foreach ($options as $option) {
35
                $option->delete();
36
            }
37
        }
38
39
        parent::delete();
40
    }
41
42
    /**
43
     * Duplicate a pages content. We need to make sure all the fields attached
44
     * to that page go with it.
45
     *
46
     * @param bool $doWrite
47
     *
48
     * @return DataObject
49
     */
50 1
    public function duplicate($doWrite = true)
51
    {
52 1
        $clonedNode = parent::duplicate($doWrite);
53
54 1
        if ($this->Options()) {
55 1
            foreach ($this->Options() as $field) {
56 1
                $newField           = $field->duplicate();
57 1
                $newField->ParentID = $clonedNode->ID;
58 1
                $newField->write();
59 1
            }
60 1
        }
61
62 1
        return $clonedNode;
63
    }
64
65
    /**
66
     * Get extra configuration fields.
67
     *
68
     * @return array
69
     */
70
    public function getFieldConfiguration()
71
    {
72
        if (!$this->isInDB()) {
73
            $field = LiteralField::create('Options', '<p class="message notice">Once you save this field you will be able to add options</p>');
74
        } else {
75
            $config = GridFieldConfig_RelationEditor::create()
76
                ->addComponent(new GridFieldOrderableRows('Sort'));
77
78
            $config
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setDisplayFields() does only exist in the following implementations of said interface: GridFieldDataColumns, GridFieldEditableColumns, GridFieldExternalLink.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
79
                ->getComponentByType('GridFieldDataColumns')
80
                ->setDisplayFields([
81
                    'Name'    => 'Name',
82
                    'Title'   => 'Title',
83
                    'Default' => 'Default',
84
                ])
85
                ->setFieldFormatting([
86
                    'Default' => function ($_, Moo_EditableFieldOption $option) {
87
                        return $option->Default ? 'Yes' : 'No';
88
                    },
89
                ]);
90
            $field = GridField::create('Options', 'Options', $this->Options(), $config);
91
        }
92
93
        return [
94
            $field,
95
        ];
96
    }
97
98
    /**
99
     * Return the form field for this object in the front end form view.
100
     *
101
     * @return FormField
102
     */
103 1
    protected function initFormField()
104
    {
105 1
        $options = $this->Options()->map('EscapedTitle', 'Title');
106
107 1
        return new OptionsetField($this->Name, $this->Title, $options);
108
    }
109
}
110