Completed
Push — master ( b0bc38...ba4ad2 )
by Will
02:12
created

ElementalArea::ItemsToRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

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 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package elemental
5
 */
6
class ElementalArea extends WidgetArea
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
    public function Elements()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
9
    {
10
        $result = $this->getComponents('Widgets');
11
12
        if ($result instanceof UnsavedRelationList) {
13
            // Setup a proper UnsavedRelationList so that an page using this extension can be created 
14
            // programmatically and have unsaved/saved BaseElement records attached to it.
15
            
16
            // NOTE(SilbinaryWolf): Able to set protected var 'dataClass' due to ViewableData using magic get/set for properties
17
            $result->dataClass = 'BaseElement'; // Change from 'Widget' to 'BaseElement'
18
            return $result;
19
        }
20
21
        $list = new HasManyList('BaseElement', $result->getForeignKey());
22
        $list->setDataModel($this->model);
23
        $list->sort('Sort ASC');
24
25
        $list = $list->forForeignID($this->ID);
26
        $list = $list->filter(array(
27
            'Enabled' => 1
28
        ));
29
30
        return $list;
31
    }
32
33
    /**
34
     * @return HasManyList
35
     */
36
    public function ItemsToRender() {
37
        return $this->Elements();
38
    }
39
40
    /**
41
    * Return an ArrayList of pages with the Element Page Extension
42
    *
43
    * @return ArrayList
44
    */
45
    public function getOwnerPage()
46
    {
47
        foreach (get_declared_classes() as $class) {
48
            if (is_subclass_of($class, 'SiteTree')) {
49
                $object = singleton($class);
50
                $classes = ClassInfo::subclassesFor('ElementPageExtension');
51
                $isElemental = false;
52
53
                foreach ($classes as $extension) {
0 ignored issues
show
Bug introduced by
The expression $classes 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...
54
                    if ($object->hasExtension($extension)) {
55
                        $isElemental = true;
56
                    }
57
                }
58
59
                if ($isElemental) {
60
                    $page = $class::get()->filter('ElementAreaID', $this->ID);
61
                    if ($page && $page->exists()) {
62
                        return $page->first();
63
                    }
64
                }
65
            }
66
        }
67
68
        return false;
69
    }
70
}
71