Completed
Pull Request — master (#491)
by Richard
14:12 queued 03:48
created

BreadCrumb   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 27
loc 54
rs 10
ccs 0
cts 27
cp 0
wmc 8
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 2
B renderItem() 27 27 6

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
 * BreadCrumb - render a breadcrumb link menu
19
 *
20
 * @category  Xoops\Html\Menu\Render
21
 * @package   BreadCrumb
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 BreadCrumb extends RenderAbstract
28
{
29
    /**
30
     * render breadcrumb from ItemList
31
     *
32
     * @param ItemList $menu breadcrumb menu items
33
     *
34
     * @return string rendered HTML for breadcrumb menu
35
     */
36
    public function render(ItemList $menu)
37
    {
38
        $renderedMenu = "<ul class=\"breadcrumb\">\n";
39
        foreach ($menu['items'] as $item) {
40
            $renderedMenu .= $this->renderItem($item);
41
        }
42
        $renderedMenu .= "</ul>\n";
43
        return $renderedMenu;
44
    }
45
46
    /**
47
     * render items, call recursively to handle ItemList, skip unsupported types
48
     *
49
     * @param Item $item Item to render
50
     *
51
     * @return string
52
     */
53 View Code Duplication
    protected function renderItem(Item $item) {
54
        $renderedItems = '';
55
        $type = $item->get('type', 'error');
56
        switch ($type) {
57
            case Item::TYPE_LINK;
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
58
                $anchorStart = '';
59
                $anchorEnd = '';
60
                $liClass = ' class="active"';
61
                if ($item->has('link')) {
62
                    $anchorStart = '<a href="' . $this->xoops->url($item->get('link')) . '">';
63
                    $anchorEnd = '</a>';
64
                    $liClass = '';
65
                }
66
                $caption = $item->get('caption', '');
67
                $icon = $item->has('icon') ? '<span class="' . $item->get('icon') . '" aria-hidden="true"></span> ' : '';
68
                $renderedItems .= "<li{$liClass}>{$anchorStart}{$icon}{$caption}{$anchorEnd}</li>";
69
                break;
70
            case Item::TYPE_LIST;
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
                foreach ($item['items'] as $listItem) {
72
                    $renderedItems .= $this->renderItem($listItem);
73
                }
74
                break;
75
            default;
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
76
                break;
77
        }
78
        return $renderedItems;
79
    }
80
}
81