Completed
Push — develop ( cb7ecf...5e631f )
by Dmytro
17s
created

ContextMenu   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 78
Duplicated Lines 8.97 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 7
loc 78
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 4
C addItem() 7 24 8
A addSeparator() 0 5 1
A render() 0 17 2
A getClientScriptObject() 0 3 1

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 namespace EvolutionCMS\Support;
2
3
use EvolutionCMS\Interfaces\ContextMenuInterface;
4
5
class ContextMenu implements ContextMenuInterface{
6
    public $id;
7
    /**
8
     * @var string
9
     */
10
    public $html = '';
11
    /**
12
     * @var bool
13
     */
14
    public $visible = false;
15
    /**
16
     * @var int
17
     */
18
    public $width = 120;
19
20
    public static $cnt;
21
22
    public function __construct($id = '', $width = 120, $visible = false) {
23
        self::$cnt++;
24
        $this->html = "";
25
        $this->visible = $visible ? $visible : false;
26
        $this->width = is_numeric($width) ? (int)$width : 120;
27
        $this->id = $id ? $id : "cntxMnu" . self::$cnt;    // set id
28
    }
29
30
    public function addItem($text, $action = "", $img = "", $disabled = 0) {
31
        global $base_url, $_style;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
32
        if($disabled) {
33
            return;
34
        }
35
        if(!$img) {
36
            $img = $base_url . $_style['tx'];
37
        }
38 View Code Duplication
        if(substr($action, 0, 3) == "js:") {
39
            $action = substr($action, 3);
40
        } else if(substr($action, 0, 3) == "hl:") {
41
            $action = "window.location.href='" . substr($action, 3) . "'";
42
        } else {
43
            $action = "window.location.href='" . $action . "'";
44
        }
45
        $action = " onmouseover=\"this.className='cntxMnuItemOver';\" onmouseout=\"this.className='cntxMnuItem';\" onclick=\"$action; hideCntxMenu('" . $this->id . "');\"";
46
        $this->html .= "<div class='" . ($disabled ? "cntxMnuItemDisabled" : "cntxMnuItem") . "' $action>";
47
        if(substr($img, 0, 5) == 'fa fa') {
48
            $img = '<i class="' . $img . '"></i>';
49
        } else if(substr($img, 0, 1) != '<') {
50
            $img = '<img src="' . $img . '" />';
51
        }
52
        $this->html .= $img . '&nbsp;' . $text . '</div>';
53
    }
54
55
    public function addSeparator() {
56
        $this->html .= "
57
			<div class='cntxMnuSeparator'></div>
58
		";
59
    }
60
61
    public function render() {
62
        $ContextMenuScript = <<<BLOCK
63
<script>
64
	function getCntxMenu(id) {
65
		return document.getElementById(id);
66
	}
67
	function hideCntxMenu(id){
68
		var cm = getCntxMenu(id);
69
		cm.style.visibility = 'hidden';
70
	}
71
</script>
72
BLOCK;
73
74
        $html = $ContextMenuScript . "<div id='" . $this->id . "' class='contextMenu' style='width:" . $this->width . "px; visibility:" . ($this->visible ? 'visible' : 'hidden') . "'>" . $this->html . "</div>";
75
        $ContextMenuScript = ""; // reset css
0 ignored issues
show
Unused Code introduced by
$ContextMenuScript is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
        return $html;
77
    }
78
79
    public function getClientScriptObject() {
80
        return "getCntxMenu('" . $this->id . "')";
81
    }
82
}
83