Completed
Push — master ( 3fdab8...e0dbd9 )
by Nicolaas
01:55
created

TemplateoverviewPageAPI::TotalCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
5
class TemplateoverviewPageAPI extends Object
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...
6
{
7
8
    private static $list_of_all_classes = array();
9
10
    private static $classes_to_exclude = array("SiteTree", "RedirectorPage", "VirtualPage");
0 ignored issues
show
Unused Code introduced by
The property $classes_to_exclude is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    /**
13
     *
14
     * @var Bool
15
     */
16
    protected $showAll = false;
17
18
    /**
19
     *
20
     * @var int
21
     */
22
    protected $counter = 0;
23
24
25
    public function ListOfAllClasses($checkCurrentClass = true)
26
    {
27
        if (!self::$list_of_all_classes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$list_of_all_classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
28
            $ArrayOfAllClasses =  array();
29
            //$classes = ClassInfo::subclassesFor("SiteTree");
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
            $classes = SiteTree::page_type_classes();
31
            $classesToRemove = array();
32
33
            foreach ($classes as $className) {
34
                if (!in_array($className, $this->config()->get("classes_to_exclude"))) {
35
                    if ($this->showAll) {
36
                        $objects = $className::get()
37
                            ->filter(array("ClassName" => $className))
38
                            ->sort("RAND() ASC")
39
                            ->limit(25);
40
                        $count = 0;
41
                        if ($objects->count()) {
42
                            foreach ($objects as $obj) {
43
                                if (!$count) {
44
                                    if ($ancestorToHide = $obj->stat('hide_ancestor')) {
45
                                        $classesToRemove[] = $ancestorToHide;
46
                                    }
47
                                }
48
                                $object = $this->createPageObject($obj, $count++);
49
                                $ArrayOfAllClasses[$object->indexNumber] = clone $object;
50
                            }
51
                        }
52
                    } else {
53
                        $obj = null;
0 ignored issues
show
Unused Code introduced by
$obj is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
                        $obj = $className::get()
55
                            ->filter(array("ClassName" => $className))
56
                            ->sort("RAND() ASC")
57
                            ->limit(1)
58
                            ->first();
59
                        if ($obj) {
60
                            $count = SiteTree::get()->filter(array("ClassName" => $obj->ClassName))->count();
61
                        } else {
62
                            $obj = $className::create();
63
                            $count = 0;
64
                        }
65
                        if ($ancestorToHide = $obj->stat('hide_ancestor')) {
66
                            $classesToRemove[] = $ancestorToHide;
67
                        }
68
                        $object = $this->createPageObject($obj, $count);
69
                        $ArrayOfAllClasses[$object->indexNumber] = clone $object;
70
                    }
71
                }
72
            }
73
74
            //remove the hidden ancestors...
75
            if ($classesToRemove && count($classesToRemove)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classesToRemove of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76
                $classesToRemove = array_unique($classesToRemove);
77
                // unset from $classes
78
                foreach ($ArrayOfAllClasses as $tempKey => $tempClass) {
79
                    if (in_array($tempClass->ClassName, $classesToRemove)) {
80
                        unset($ArrayOfAllClasses[$tempKey]);
81
                    }
82
                }
83
            }
84
            ksort($ArrayOfAllClasses);
85
            self::$list_of_all_classes =  new ArrayList();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayList() of type object<ArrayList> is incompatible with the declared type array of property $list_of_all_classes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
            $currentClassname = '';
87
            if ($checkCurrentClass) {
88
                if ($c = Controller::curr()) {
89
                    if ($d = $c->dataRecord) {
90
                        $currentClassname = $d->ClassName;
91
                    }
92
                }
93
            }
94
            if (count($ArrayOfAllClasses)) {
95
                foreach ($ArrayOfAllClasses as $item) {
96
                    if ($item->ClassName == $currentClassname) {
97
                        $item->LinkingMode = "current";
98
                    } else {
99
                        $item->LinkingMode = "link";
100
                    }
101
                    self::$list_of_all_classes->push($item);
102
                }
103
            }
104
        }
105
        return self::$list_of_all_classes;
106
    }
107
108
    public function ShowAll()
109
    {
110
        $this->showAll = true;
111
        return array();
112
    }
113
114
115
    /**
116
     * @param SiteTree $obj
117
     * @param Int $count
118
     * @param String $ClassName
0 ignored issues
show
Bug introduced by
There is no parameter named $ClassName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
119
     * @return ArrayData
120
     */
121
    private function createPageObject($obj, $count)
122
    {
123
        $this->counter++;
124
        $listArray = array();
125
        $indexNumber = (10000 * $count) + $this->counter;
126
        $listArray["indexNumber"] = $indexNumber;
127
        $listArray["ClassName"] = $obj->ClassName;
128
        $listArray["Count"] = $count;
129
        $listArray["ID"] = $obj->ID;
130
        $listArray["URLSegment"] = $obj->URLSegment;
131
        $listArray["TypoURLSegment"] = $this->Link();
132
        $listArray["Title"] = $obj->MenuTitle;
133
        $listArray["PreviewLink"] = $obj->PreviewLink();
134
        $listArray["CMSEditLink"] = $obj->CMSEditLink();
135
        $staticIcon = $obj->stat("icon", true);
136
        if (is_array($staticIcon)) {
137
            $iconArray = $obj->stat("icon");
138
            $icon = $iconArray[0];
139
        } else {
140
            $icon = $obj->stat("icon");
141
        }
142
        $iconFile = Director::baseFolder().'/'.$icon;
143
        if (!file_exists($iconFile)) {
144
            $icon = $icon."-file.gif";
145
        }
146
        $listArray["Icon"] = $icon;
147
        return new ArrayData($listArray);
148
    }
149
150
    //not used!
151
    public function NoSubClasses($obj)
152
    {
153
        $array = ClassInfo::subclassesFor($obj->ClassName);
154
        if (count($array)) {
155
            foreach ($array as $class) {
0 ignored issues
show
Bug introduced by
The expression $array of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
156
                if ($class::get()->byID($obj->ID)) {
157
                    return false;
158
                }
159
            }
160
        }
161
        return true;
162
    }
163
164
    function Link($action = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
165
    {
166
        $v = '/templates';
167
        if($action) {
168
            $v .= $action . '/';
169
        }
170
171
        return $v;
172
    }
173
174
}
175