SiteTreeArchiveExtension::isArchiveFieldEnabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\ORM\FieldType\DBDatetime;
9
use SilverStripe\Security\Member;
10
use SilverStripe\VersionedAdmin\ArchiveAdmin;
11
use SilverStripe\VersionedAdmin\Interfaces\ArchiveViewProvider;
12
13
/**
14
 * Adds a archive view for Pages
15
 */
16
class SiteTreeArchiveExtension extends DataExtension implements ArchiveViewProvider
17
{
18
    /**
19
     * @inheritDoc
20
    */
21
    public function getArchiveFieldClass()
22
    {
23
        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...
24
    }
25
26
    /**
27
     * @inheritDoc
28
    */
29
    public function getArchiveField()
30
    {
31
        $listField = ArchiveAdmin::createArchiveGridField('Pages', SiteTree::class);
32
33
        $listColumns = $listField->getConfig()->getComponentByType(GridFieldDataColumns::class);
34
        $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

34
        $listColumns->/** @scrutinizer ignore-call */ 
35
                      setDisplayFields([
Loading history...
35
            'Title' => SiteTree::singleton()->fieldLabel('Title'),
36
            'i18n_singular_name' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_TYPE', 'Type'),
37
            'Versions.first.LastEdited' => _t(
38
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_DATEARCHIVED',
39
                'Date Archived'
40
            ),
41
            'ParentID' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ORIGIN', 'Origin'),
42
            'Versions.first.Author.Name' => _t(
43
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ARCHIVEDBY',
44
                'Archived By'
45
            )
46
        ]);
47
        $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

47
        $listColumns->/** @scrutinizer ignore-call */ 
48
                      setFieldFormatting([
Loading history...
48
            '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

48
            '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...
49
                if (SiteTree::get_by_id($val)) {
50
                    $breadcrumbs = SiteTree::get_by_id($val)->getBreadcrumbItems(2);
51
                    $breadcrumbString = '../';
52
                    foreach ($breadcrumbs as $item) {
0 ignored issues
show
introduced by
$item is overwriting one of the parameters of this function.
Loading history...
53
                        $breadcrumbString = $breadcrumbString . $item->URLSegment . '/';
54
                    };
55
                    return $breadcrumbString;
56
                }
57
            },
58
            'Versions.first.LastEdited' => 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

58
            'Versions.first.LastEdited' => 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...
59
                return DBDatetime::create_field('Datetime', $val)->Ago();
60
            },
61
        ]);
62
63
        return $listField;
64
    }
65
66
    /**
67
     * @inheritDoc
68
    */
69
    public function isArchiveFieldEnabled()
70
    {
71
        return true;
72
    }
73
}
74