GridFieldExtraColumns::getColumnContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
4
class GridFieldExtraColumns implements GridField_ColumnProvider
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...
5
{
6
    protected $field = '';
7
8
    public function __construct($field)
9
    {
10
        $this->field = $field;
11
    }
12
13
    public function augmentColumns($gridField, &$columns)
14
    {
15
        array_unshift($columns, $this->field);
16
    }
17
18
    public function getColumnAttributes($gridField, $record, $columnName)
19
    {
20
        return array(
21
            'data-extrafield' => '1',
22
            'data-url' => '/admin/boardcheck',
23
            'data-field' => $this->field,
24
            'data-id' => $record->ID,
25
            'onclick' => "if(event.stopPropagation){event.stopPropagation();}event.cancelBubble=true;var val = prompt('Enter value');var t = jQuery(this);t.html(val);var model = t.parents('tr').data('class');jQuery.post(t.data('url') + '/' + model + '/extrafields/' + t.data('id'),{field:t.data('field'),table:t.data('table'),id:t.data('id'),value:val});return false"
26
            );
27
    }
28
29
    public function getColumnContent($gridField, $record, $columnName)
30
    {
31
        $res = $gridField->getList()->getExtraData($this->field, $record->ID);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SS_List as the method getExtraData() does only exist in the following implementations of said interface: ManyManyList, Member_GroupSet.

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...
32
        if ($res) {
33
            return $res[$this->field];
34
        }
35
    }
36
37
    public function getColumnMetadata($gridField, $columnName)
38
    {
39
        return array('title' => $this->field);
40
    }
41
42
    public function getColumnsHandled($gridField)
43
    {
44
        $form = $gridField->getConfig()->getComponentByType('GridFieldDetailForm');
45
        if ($form) {
46
            $field = $this->field;
47
            //this does nothing :(
48
            $form->setItemEditFormCallback(function ($form, $components) use ($field) {
0 ignored issues
show
Unused Code introduced by
The parameter $components 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...
49
                $form->addFieldToTab('Root.Main', new TextField($field));
50
            });
51
        }
52
        return array($this->field);
53
    }
54
}
55