Panel::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
c 0
b 0
f 0
rs 9.7692
cc 2
eloc 39
nc 2
nop 1

How to fix   Long Method   

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
namespace rtens\domin\delivery\web\renderers\dashboard\types;
3
4
use rtens\domin\delivery\RendererRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\HeadElements;
7
use rtens\domin\delivery\web\renderers\dashboard\DashboardItem;
8
use rtens\domin\delivery\web\WebRenderer;
9
10
class Panel implements DashboardItem {
11
12
    /** @var string */
13
    private $heading;
14
15
    /** @var mixed */
16
    private $content;
17
18
    /** @var array|Element[] */
19
    public $rightHeading = [];
20
21
    /** @var null|string */
22
    private $maxHeight;
23
24
    /**
25
     * @param string $heading
26
     * @param mixed $content
27
     */
28
    public function __construct($heading, $content) {
29
        $this->heading = $heading;
30
        $this->content = $content;
31
    }
32
33
    /**
34
     * @param RendererRegistry $renderers
35
     * @return Element
36
     * @throws \Exception
37
     */
38
    public function render(RendererRegistry $renderers) {
39
        $content = [
40
            new Element('div', ['class' => 'panel-heading'], [
41
                $this->heading,
42
                new Element('div', ['class' => 'pull-right'],
43
                    $this->rightHeading
44
                )
45
            ])
46
        ];
47
48
        if ($this->maxHeight) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->maxHeight of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
            $content[] = new Element('div', [
50
                'class' => 'panel-body',
51
                'style' => 'overflow-x: auto; overflow-y: hidden; max-height: ' . $this->maxHeight,
52
                'data-maxheight' => $this->maxHeight
53
            ], [
54
                $renderers->getRenderer($this->content)->render($this->content)
55
            ]);
56
            $content[] = new Element('div', ['class' => 'panel-footer clearfix'], [
57
                new Element('div', ['class' => 'pull-right'], [
58
                    new Element('a', [
59
                        'href' => 'javascript:',
60
                        'onclick' => "var body = $(this).closest('.panel').find('.panel-body');" .
61
                            "body.css('max-height', 'none');" .
62
                            "body.css('overflow-y', 'visible');" .
63
                            "$(this).hide(); " .
64
                            "$(this).next().show();",
65
                        'class' => 'show-all'
66
                    ], [
67
                        'Show all',
68
                        new Element('span', ['class' => 'glyphicon glyphicon-chevron-down'])
69
                    ]),
70
                    new Element('a', [
71
                        'href' => 'javascript:',
72
                        'onclick' => "var body = $(this).closest('.panel').find('.panel-body');" .
73
                            "body.css('max-height', body.data('maxheight'));" .
74
                            "body.css('overflow-y', 'hidden');" .
75
                            "$(this).hide();" .
76
                            "$(this).prev().show();",
77
                        'class' => 'show-less',
78
                        'style' => 'display: none'
79
                    ], [
80
                        'Show less',
81
                        new Element('span', ['class' => 'glyphicon glyphicon-chevron-up'])
82
                    ])
83
                ])
84
            ]);
85
        } else {
86
            $content[] = new Element('div', ['class' => 'panel-body'], [
87
                $renderers->getRenderer($this->content)->render($this->content)
88
            ]);
89
        }
90
91
        return new Element('div', ['class' => 'panel panel-default'], $content);
92
    }
93
94
    /**
95
     * @param RendererRegistry $renderers
96
     * @return \rtens\domin\delivery\web\Element[]
97
     */
98
    public function headElements(RendererRegistry $renderers) {
99
        $elements = [HeadElements::jquery()];
100
101
        $renderer = $renderers->getRenderer($this->content);
102
        if ($renderer instanceof WebRenderer) {
103
            $elements = array_merge($elements, $renderer->headElements($this->content));
104
        }
105
        return $elements;
106
    }
107
108
    /**
109
     * @param array|Element[] $rightHeading
110
     * @return static
111
     */
112
    public function setRightHeading(array $rightHeading) {
113
        $this->rightHeading = $rightHeading;
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $height e.g. "20px"
119
     * @return static
120
     */
121
    public function setMaxHeight($height) {
122
        $this->maxHeight = $height;
123
        return $this;
124
    }
125
}