Completed
Push — master ( a0b792...8ecc61 )
by Will
9s
created

handleWidget()   C

Complexity

Conditions 14
Paths 20

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 51
rs 5.6426
cc 14
eloc 27
nc 20
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class ElementalContentControllerExtension extends Extension
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...
4
{
5
   /**
6
     *
7
     * @var array
8
     */
9
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions 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...
10
        'handleWidget'
11
    );
12
13
    /**
14
     * Handles widgets attached to a page through one or more {@link WidgetArea}
15
     * elements.
16
     *
17
     * Iterated through each $has_one relation with a {@link WidgetArea} and
18
     * looks for connected widgets by their database identifier.
19
     *
20
     * Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
21
     *
22
     * @return RequestHandler
23
     */
24
    public function handleWidget()
25
    {
26
        $SQL_id = $this->owner->getRequest()->param('ID');
27
        if (!$SQL_id) {
28
            return false;
29
        }
30
31
        // find WidgetArea relations
32
        $widgetAreaRelations = array();
33
        $hasOnes = $this->owner->data()->hasOne();
34
35
        if (!$hasOnes) {
36
            return false;
37
        }
38
39
        foreach ($hasOnes as $hasOneName => $hasOneClass) {
40
            if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
41
                $widgetAreaRelations[] = $hasOneName;
42
            }
43
        }
44
45
        // find widget
46
        $widget = null;
47
        $linked = null;
0 ignored issues
show
Unused Code introduced by
$linked 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...
48
49
        foreach ($widgetAreaRelations as $widgetAreaRelation) {
50
            if ($widget) {
51
                break;
52
            }
53
54
            $widget = $this->owner->data()->$widgetAreaRelation()->Widgets()
55
                ->filter('ID', $SQL_id)
56
                ->First();
57
58
            if(!$widget && ($this->owner->data()->$widgetAreaRelation() instanceof ElementalArea)) {
59
                foreach($this->owner->data()->$widgetAreaRelation()->AllElements() as $element) {
60
                    if($element instanceof ElementVirtualLinked && $element->LinkedElementID == $SQL_id) {
0 ignored issues
show
Documentation introduced by
The property LinkedElementID does not exist on object<ElementVirtualLinked>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
                        $widget = $element->LinkedElement();
0 ignored issues
show
Documentation Bug introduced by
The method LinkedElement does not exist on object<ElementVirtualLinked>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
62
63
                        break;
64
                    }
65
                }
66
            }
67
        }
68
69
        if (!$widget) {
70
            return;
71
        }
72
73
        return $widget->getController();
74
    }
75
}
76