Completed
Pull Request — master (#491)
by Richard
10:43
created

DropDownButton   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 41.1 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 30
loc 73
rs 10
wmc 12
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 23 5
C renderItem() 30 32 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xoops\Html\Menu\Render;
13
14
use Xoops\Html\Menu\Item;
15
use Xoops\Html\Menu\ItemList;
16
17
/**
18
 * DropDownButton - render a button dropdown menu
19
 *
20
 * @category  Xoops\Html\Menu\Render
21
 * @package   DropDownButton
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2016 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class DropDownButton extends RenderAbstract
28
{
29
    /**
30
     * render menu from ItemList
31
     *
32
     * @param ItemList $menu menu items
33
     *
34
     * @return string rendered HTML for menu
35
     */
36
    public function render(ItemList $menu)
37
    {
38
        $dropdown = $menu->get('dropdown', 'dropdown');
39
        $renderedMenu = "<div class=\"{$dropdown}\">\n";
40
        $class = $menu->get('class', 'btn btn-default dropdown-toggle');
41
        $id = ($menu->has('id')) ? ' id="' . $menu->get('id') . '"' : '';
42
        $labeledId = ($menu->has('id')) ? ' aria-labelledby="' . $menu->get('id') . '"' : '';
43
        $caption = $menu->get('caption', '');
44
        $icon = $menu->has('icon') ? '<span class="' . $menu->get('icon') . '" aria-hidden="true"></span> ' : '';
45
        $renderedMenu .= <<<EOT
46
<button class="{$class}" type="button"{$id} data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
47
{$icon}{$caption} <span class="caret"></span>
48
</button>
49
  <ul class="dropdown-menu"{$labeledId}">
50
EOT;
51
52
        foreach ($menu['items'] as $item) {
53
            $renderedMenu .= $this->renderItem($item);
54
        }
55
56
        $renderedMenu .= "  </ul>\n</div>\n";
57
        return $renderedMenu;
58
    }
59
60
    /**
61
     * render items, call recursively to handle ItemList, skip unsupported types
62
     *
63
     * @param Item $item Item to render
64
     *
65
     * @return string
66
     */
67 View Code Duplication
    protected function renderItem(Item $item)
68
    {
69
        $renderedItems = '';
70
        $type = $item->get('type', 'error');
71
        switch ($type) {
72
            case Item::TYPE_LINK:
73
                $anchorStart = '';
74
                $anchorEnd = '';
75
                $liClass = ' class="active"';
76
                if ($item->has('link')) {
77
                    $anchorStart = '<a href="' . $this->xoops->url($item->get('link')) . '">';
78
                    $anchorEnd = '</a>';
79
                    $liClass = '';
80
                }
81
                $caption = $item->get('caption', '');
82
                $icon = $item->has('icon') ?
83
                    '<span class="' . $item->get('icon') . '" aria-hidden="true"></span> ' : '';
84
                $renderedItems .= "<li{$liClass}>{$anchorStart}{$icon}{$caption}{$anchorEnd}</li>";
85
                break;
86
            case Item::TYPE_LIST:
87
                foreach ($item['items'] as $listItem) {
88
                    $renderedItems .= $this->renderItem($listItem);
89
                }
90
                break;
91
            case Item::TYPE_DIVIDER:
92
                $renderedItems .= '<li role="separator" class="divider"></li>';
93
                break;
94
            default:
95
                break;
96
        }
97
        return $renderedItems;
98
    }
99
}
100