Moo_EditableFieldGroup   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 64
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSValidator() 0 4 1
B getCMSFields() 0 26 2
1
<?php
2
3
/**
4
 * Moo_EditableFieldGroup is a data object class for editable field group.
5
 *
6
 * @author  Mohamed Alsharaf <[email protected]>
7
 *
8
 * @method ManyManyList Fields()
9
 */
10
class Moo_EditableFieldGroup extends DataObject
11
{
12
    private static $db = [
13
        'Title' => 'Varchar(255)',
14
    ];
15
16
    private static $many_many = [
17
        'Fields' => 'Moo_EditableField',
18
    ];
19
20
    private static $many_many_extraFields = [
21
        'Fields' => [
22
            'Sort' => 'Int',
23
        ],
24
    ];
25
26
    private static $default_sort = '"Title"';
27
28
    private static $singular_name = 'Group';
29
30
    private static $plural_name = 'Groups';
31
32
    /**
33
     * Make sure the title is required field.
34
     *
35
     * @return RequiredFields
36
     */
37 1
    public function getCMSValidator()
38
    {
39 1
        return new RequiredFields('Title');
40
    }
41
42
    /**
43
     * Returns form fields for adding/editing the data object.
44
     *
45
     * @return FieldList
46
     */
47 1
    public function getCMSFields()
48
    {
49 1
        $fields = parent::getCMSFields();
50
51
        // Field visible on edit only
52 1
        if ($this->isInDB()) {
53 1
            $config = GridFieldConfig_RelationEditor::create();
54
            $config
55 1
                ->getComponentByType('GridFieldPaginator')
56 1
                ->setItemsPerPage(10);
57 1
            $config->removeComponentsByType('GridFieldAddNewButton');
58 1
            $config->removeComponentsByType('GridFieldEditButton');
59
            $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...
60 1
                ->getComponentByType('GridFieldDataColumns')
61 1
                ->setDisplayFields([
62 1
                    'Name'  => _t('Moo_EditableFieldGroup.NAME', 'Name'),
63 1
                    'Title' => _t('Moo_EditableFieldGroup.TITLE', 'Title'),
64 1
                    'Sort'  => _t('Moo_EditableFieldGroup.SORT', 'Sort'),
65 1
                ]);
66 1
            $config->addComponent(new GridFieldOrderableRows('Sort'));
67 1
            $field = new GridField('Fields', 'Fields', $this->Fields(), $config);
68 1
            $fields->addFieldToTab('Root.Fields', $field);
69 1
        }
70
71 1
        return $fields;
72
    }
73
}
74