GridFieldSiteTree_PageHolderExtension   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 2
c 3
b 0
f 0
lcom 0
cbo 6
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A AllChildrenIncludingDeleted() 0 4 1
B getGridFieldSiteTreeField() 0 38 1
1
<?php
2
3
/*
4
 * @author Christopher Darling (www.christopherdarling.co.uk)
5
 */
6
class GridFieldSiteTree_PageHolderExtension extends DataExtension
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...
7
{
8
9
    /**
10
     * Prevent children pages from showing up in the SiteTree TreeView
11
     **/
12
    public function AllChildrenIncludingDeleted()
13
    {
14
        return false;
15
    }
16
17
    /*
18
     * Create a GridField to display the pages.
19
     *
20
     * @see CMSMain::ListViewForm() heavily based on this code.
21
     * @param string $name
22
     * @param string $title
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     * @param SS_List $dataList
0 ignored issues
show
Documentation introduced by
Should the type for parameter $dataList not be null|SS_List?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
24
     * @return GridField
25
     */
26
    public function getGridFieldSiteTreeField($name, $title=null, SS_List $dataList = null)
27
    {
28
        $gf = GridField::create(
29
            $name,
30
            $title,
31
            $dataList,
32
            $config = GridFieldConfig::create()
33
        );
34
35
        $config->addComponents(
36
            new GridFieldSortableHeader(),
37
            $columns = new GridFieldDataColumns(),
38
            new GridFieldPaginator(15)
39
        );
40
41
        $fields = array(
42
            'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'),
43
            'singular_name' => _t('SiteTree.PAGETYPE'),
44
            'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'),
45
        );
46
        $columns->setDisplayFields($fields);
47
        $columns->setFieldCasting(array(
48
            'Created' => 'Datetime->Ago',
49
            'LastEdited' => 'Datetime->Ago',
50
            'getTreeTitle' => 'HTMLText'
51
        ));
52
53
        $config->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title'));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setFieldSorting() does only exist in the following implementations of said interface: GridFieldSortableHeader.

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...
54
55
        $controller = $this;
56
        $columns->setFieldFormatting(array(
57
            'getTreeTitle' => function ($value, &$item) use ($controller) {
58
                return '<a class="action-detail" href="' . singleton('CMSPageEditController')->Link('show') . '/' . $item->ID . '">' . $item->TreeTitle . '</a>';
59
            }
60
        ));
61
62
        return $gf;
63
    }
64
}
65