Passed
Push — master ( ba645d...9eba8c )
by Michael
07:09 queued 43s
created

ColorPlugin::renderTab()   D

Complexity

Conditions 13
Paths 321

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 26
c 1
b 0
f 0
nc 321
nop 1
dl 0
loc 45
rs 4.2708

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
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
 * this software and associated documentation files (the "Software"), to deal in
10
 * the Software without restriction, including without limitation the rights to
11
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
 * the Software, and to permit persons to whom the Software is furnished to do so,
13
 * subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
26
namespace Kint\Renderer\Rich;
27
28
use Kint\Object\BasicObject;
29
use Kint\Object\Representation\ColorRepresentation;
30
use Kint\Object\Representation\Representation;
31
32
class ColorPlugin extends Plugin implements TabPluginInterface, ObjectPluginInterface
33
{
34
    public function renderObject(BasicObject $o)
35
    {
36
        $r = $o->getRepresentation('color');
37
38
        if (!$r instanceof ColorRepresentation) {
39
            return;
40
        }
41
42
        $children = $this->renderer->renderChildren($o);
43
44
        $header = $this->renderer->renderHeader($o);
45
        $header .= '<div class="kint-color-preview"><div style="background:';
46
        $header .= $r->getColor(ColorRepresentation::COLOR_RGBA);
47
        $header .= '"></div></div>';
48
49
        $header = $this->renderer->renderHeaderWrapper($o, (bool) \strlen($children), $header);
50
51
        return '<dl>'.$header.$children.'</dl>';
52
    }
53
54
    public function renderTab(Representation $r)
55
    {
56
        if (!$r instanceof ColorRepresentation) {
57
            return;
58
        }
59
60
        $out = '';
61
62
        if ($color = $r->getColor(ColorRepresentation::COLOR_NAME)) {
63
            $out .= '<dfn>'.$color."</dfn>\n";
64
        }
65
        if ($color = $r->getColor(ColorRepresentation::COLOR_HEX_3)) {
66
            $out .= '<dfn>'.$color."</dfn>\n";
67
        }
68
        if ($color = $r->getColor(ColorRepresentation::COLOR_HEX_6)) {
69
            $out .= '<dfn>'.$color."</dfn>\n";
70
        }
71
72
        if ($r->hasAlpha()) {
73
            if ($color = $r->getColor(ColorRepresentation::COLOR_HEX_4)) {
74
                $out .= '<dfn>'.$color."</dfn>\n";
75
            }
76
            if ($color = $r->getColor(ColorRepresentation::COLOR_HEX_8)) {
77
                $out .= '<dfn>'.$color."</dfn>\n";
78
            }
79
            if ($color = $r->getColor(ColorRepresentation::COLOR_RGBA)) {
80
                $out .= '<dfn>'.$color."</dfn>\n";
81
            }
82
            if ($color = $r->getColor(ColorRepresentation::COLOR_HSLA)) {
83
                $out .= '<dfn>'.$color."</dfn>\n";
84
            }
85
        } else {
86
            if ($color = $r->getColor(ColorRepresentation::COLOR_RGB)) {
87
                $out .= '<dfn>'.$color."</dfn>\n";
88
            }
89
            if ($color = $r->getColor(ColorRepresentation::COLOR_HSL)) {
90
                $out .= '<dfn>'.$color."</dfn>\n";
91
            }
92
        }
93
94
        if (!\strlen($out)) {
95
            return false;
96
        }
97
98
        return '<pre>'.$out.'</pre>';
99
    }
100
}
101