Passed
Push — master ( b101db...a45b24 )
by Robbie
05:17
created

SiteTreeArchiveExtension::getArchiveFieldClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Extensions;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Forms\GridField\GridFieldDataColumns;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\Security\Member;
9
use SilverStripe\VersionedAdmin\ArchiveAdmin;
10
use SilverStripe\VersionedAdmin\ArchiveViewProvider;
11
12
/**
13
 * Adds a archive view for Pages
14
 */
15
class SiteTreeArchiveExtension extends DataExtension implements ArchiveViewProvider
16
{
17
    /**
18
     * @inheritDoc
19
    */
20
    public function getArchiveFieldClass()
21
    {
22
        return SiteTree::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\CMS\Model\SiteTree::class returns the type string which is incompatible with the return type mandated by SilverStripe\VersionedAd...:getArchiveFieldClass() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
23
    }
24
25
    /**
26
     * @inheritDoc
27
    */
28
    public function getArchiveField()
29
    {
30
        $listField = ArchiveAdmin::createArchiveGridField('Pages', SiteTree::class);
31
32
        $listColumns = $listField->getConfig()->getComponentByType(GridFieldDataColumns::class);
33
        $listColumns->setDisplayFields([
0 ignored issues
show
Bug introduced by
The method setDisplayFields() does not exist on SilverStripe\Forms\GridField\GridFieldComponent. It seems like you code against a sub-type of SilverStripe\Forms\GridField\GridFieldComponent such as SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldDataColumns or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\Tests...ndlerTest\TestComponent or SilverStripe\Forms\GridField\GridFieldDetailForm. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $listColumns->/** @scrutinizer ignore-call */ 
34
                      setDisplayFields([
Loading history...
34
            'Title' => SiteTree::singleton()->fieldLabel('Title'),
35
            'i18n_singular_name' => _t(ArchiveAdmin::class . '.COLUMN_TYPE', 'Type'),
36
            'LastEdited.Ago' => _t(ArchiveAdmin::class . '.COLUMN_DATEARCHIVED', 'Date Archived'),
37
            'ParentID' => _t(ArchiveAdmin::class . '.COLUMN_ORIGIN', 'Origin'),
38
            'AuthorID' => _t(ArchiveAdmin::class . '.COLUMN_ARCHIVEDBY', 'Archived By')
39
        ]);
40
        $listColumns->setFieldFormatting([
0 ignored issues
show
Bug introduced by
The method setFieldFormatting() does not exist on SilverStripe\Forms\GridField\GridFieldComponent. It seems like you code against a sub-type of SilverStripe\Forms\GridField\GridFieldComponent such as SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldDataColumns or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\Tests...ndlerTest\TestComponent or SilverStripe\Forms\GridField\GridFieldDetailForm. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $listColumns->/** @scrutinizer ignore-call */ 
41
                      setFieldFormatting([
Loading history...
41
            'ParentID' => function ($val, $item) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
            'ParentID' => function ($val, /** @scrutinizer ignore-unused */ $item) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
                if (SiteTree::get_by_id($val)) {
43
                    $breadcrumbs = SiteTree::get_by_id($val)->getBreadcrumbItems(2);
44
                    $breadcrumbString = '../';
45
                    foreach ($breadcrumbs as $item) {
0 ignored issues
show
introduced by
$item is overwriting one of the parameters of this function.
Loading history...
46
                        $breadcrumbString = $breadcrumbString . $item->URLSegment . '/';
47
                    };
48
                    return $breadcrumbString;
49
                }
50
            },
51
            'AuthorID' => function ($val, $item) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
            'AuthorID' => function ($val, /** @scrutinizer ignore-unused */ $item) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
                $member = Member::get_by_id($val);
53
                return $member ? $member->Name : null;
0 ignored issues
show
introduced by
$member is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
54
            },
55
        ]);
56
57
        return $listField;
58
    }
59
60
    /**
61
     * @inheritDoc
62
    */
63
    public function isArchiveFieldEnabled()
64
    {
65
        return true;
66
    }
67
}
68