Passed
Pull Request — master (#123)
by Robbie
01:46
created

ElementUseReport::getCount()   A

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
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
use SilverStripe\Reports\Report;
4
use DNADesign\Elemental\Models\BaseElement;
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\View\ArrayData;
9
use SilverStripe\Core\Convert;
10
11
class ElementUseReport extends Report
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...
12
{
13
    protected $title = 'Content Blocks in Use';
14
15
    protected $description = 'Show what Content Blocks are being used.';
16
17
    public function getCount($params = array())
18
    {
19
        return BaseElement::get()->count();
20
    }
21
22
    public function sourceRecords($params = [])
23
    {
24
        if (empty($params)) {
25
            $classes = ClassInfo::subclassesFor(BaseElement::class);
26
            $output = new ArrayList();
27
28
            foreach ($classes as $class) {
29
                if ($class === BaseElement::class) {
30
                    continue;
31
                }
32
33
                $inst = Injector::inst()->create($class);
34
35
                $output->push(new ArrayData([
36
                    'Icon' => $inst->getIcon(),
37
                    'Title' => $inst->getTypeNice(),
38
                    'ClassName' => $inst->sanitiseClassName($class),
39
                    'Total' => $class::get()->count()
40
                ]));
41
            }
42
43
            return $output;
44
        } else if (isset($params['ClassName'])) {
45
            $convertClass = Injector::inst()->create(BaseElement::class)->unsanitiseClassName($params['ClassName']);
46
47
            if (ClassInfo::exists($convertClass)) {
48
                return $convertClass::get()->sort('Title', 'ASC');
49
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
50
51
            }
52
        }
53
    }
54
55
    public function columns()
56
    {
57
        $params = $this->params;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist on ElementUseReport. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
59
        if (empty($params)) {
60
            return [
61
                'Icon' => [
62
                    'title' => ''
63
                ],
64
                'Title' => [
65
                    'title' => _t(__CLASS__.'.Title', 'Content Type')
66
                ],
67
                'Total' => [
68
                    'title' => _t(__CLASS__.'.Total', 'Total'),
69
                    'link' => function($value, $item) {
70
                        return sprintf(
71
                            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
72
                            Convert::raw2att($this->getLink('?filters[ClassName]='. $item->ClassName)),
73
                            Convert::raw2att($value),
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Convert::raw2att($value) can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
                            /** @scrutinizer ignore-type */ Convert::raw2att($value),
Loading history...
74
                            Convert::raw2xml($value)
75
                        );
76
                    }
77
                ]
78
            ];
79
        } else {
80
            return [
81
                'EditorPreview' => [
82
                    'title' => ''
83
                ],
84
                'PageTitle' => [
85
                    'title' => _t(__CLASS__.'.Page', 'Page'),
86
                    'link' => true
87
                ]
88
            ];
89
        }
90
    }
91
}
92