Completed
Push — master ( f3b14d...1fe4c3 )
by Damian
10s
created

WidgetContentControllerExtension::handleWidget()   C

Complexity

Conditions 11
Paths 39

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 24
nc 39
nop 0

How to fix   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
namespace SilverStripe\Widgets\Controllers;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Widgets\Model\WidgetArea;
7
8
/**
9
 * Add this to ContentController to enable widgets
10
 *
11
 * @package widgets
12
 */
13
class WidgetContentControllerExtension extends Extension
14
{
15
    /**
16
     *
17
     * @var array
18
     */
19
    private static $allowed_actions = array(
0 ignored issues
show
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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
20
        'handleWidget'
21
    );
22
23
    /**
24
     * Handles widgets attached to a page through one or more {@link WidgetArea}
25
     * elements.
26
     *
27
     * Iterated through each $has_one relation with a {@link WidgetArea} and
28
     * looks for connected widgets by their database identifier.
29
     *
30
     * Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
31
     *
32
     * @return RequestHandler
33
     */
34
    public function handleWidget()
35
    {
36
        $SQL_id = $this->owner->getRequest()->param('ID');
37
        if (!$SQL_id) {
38
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SilverStripe\Widgets\Con...Extension::handleWidget of type SilverStripe\Widgets\Controllers\RequestHandler.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
39
        }
40
        /** @var SiteTree $widgetOwner */
41
        $widgetOwner = $this->owner->data();
42
        while ($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()) {
43
            $widgetOwner = $widgetOwner->Parent();
44
        }
45
46
        // find WidgetArea relations
47
        $widgetAreaRelations = array();
48
        $hasOnes = $widgetOwner->hasOne();
49
50
        if (!$hasOnes) {
51
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by SilverStripe\Widgets\Con...Extension::handleWidget of type SilverStripe\Widgets\Controllers\RequestHandler.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
        }
53
54
        foreach ($hasOnes as $hasOneName => $hasOneClass) {
55
            if ($hasOneClass == WidgetArea::class || is_subclass_of($hasOneClass, WidgetArea::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SilverStripe\Widgets\Model\WidgetArea::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
56
                $widgetAreaRelations[] = $hasOneName;
57
            }
58
        }
59
60
        // find widget
61
        $widget = null;
62
63
        foreach ($widgetAreaRelations as $widgetAreaRelation) {
64
            if ($widget) {
65
                break;
66
            }
67
68
            $widget = $widgetOwner->$widgetAreaRelation()->Widgets()
69
                ->filter('ID', $SQL_id)
70
                ->First();
71
        }
72
73
        if (!$widget) {
74
            user_error('No widget found', E_USER_ERROR);
75
        }
76
77
        return $widget->getController();
78
    }
79
}
80