Completed
Push — master ( 2bc3ae...eaf16e )
by Mohamed
04:59
created

getFieldConfiguration()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 20
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 2
nop 0
crap 12
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
     * @return DataObject
47
     */
48
    public function duplicate($doWrite = true)
49
    {
50
        $clonedNode = parent::duplicate();
51
52
        if ($this->Options()) {
53
            foreach ($this->Options() as $field) {
54
                $newField           = $field->duplicate();
55
                $newField->ParentID = $clonedNode->ID;
56
                $newField->write();
57
            }
58
        }
59
60
        return $clonedNode;
61
    }
62
63
    public function getFieldConfiguration()
64
    {
65
        if (!$this->isInDB()) {
66
            $field = LiteralField::create('Options', '<p class="message notice">Once you save this field you will be able to add options</p>');
67
        } else {
68
            $config = GridFieldConfig_RelationEditor::create()
69
                ->addComponent(new GridFieldOrderableRows('Sort'));
70
71
            $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...
72
                ->getComponentByType('GridFieldDataColumns')
73
                ->setDisplayFields([
74
                    'Name'    => 'Name',
75
                    'Title'   => 'Title',
76
                    'Default' => 'Default',
77
                ])
78
                ->setFieldFormatting([
79
                    'Default' => function ($_, Moo_EditableFieldOption $option) {
80
                        return $option->Default ? 'Yes' : 'No';
81
                    },
82
                ]);
83
            $field = GridField::create('Options', 'Options', $this->Options(), $config);
84
        }
85
86
        return [
87
            $field,
88
        ];
89
    }
90
91
    /**
92
     * Return the form field for this object in the front end form view.
93
     *
94
     * @return FormField
95
     */
96
    protected function initFormField()
97
    {
98
        $options = $this->Options()->map('EscapedTitle', 'Title');
99
100
        return new OptionsetField($this->Name, $this->Title, $options);
101
    }
102
}
103