ContextMenuHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 54
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B actionUrl() 0 41 6
1
<?php
2
3
namespace devgroup\JsTreeWidget\helpers;
4
5
use Yii;
6
use yii\helpers\Json;
7
use yii\helpers\Url;
8
use yii\web\JsExpression;
9
10
class ContextMenuHelper
11
{
12
13
    /**
14
     * Returns JavaScript expression(\yii\web\JsExpression) for context menu item.
15
     * Used for redirecting browser to node-specific action(ie. edit, delete).
16
     * Automatically appends needed $.data() attributes(can be specified in HTML5 data attribute format too).
17
     *
18
     * @param array $route Action route
19
     * @param bool|array $dataAttributes Array of attributes to append, true to append all attributes, false if you don't want to append any data-attributes.
20
     * @return JsExpression
21
     */
22
    public static function actionUrl(array $route, $dataAttributes = true)
23
    {
24
        $baseUrl = Json::encode(Url::to($route));
25
        $union = strpos($baseUrl, '?') > 0 ? '&' : '?';
26
27
        $dataExpression = "var data = \$object.data(), dataVariables = [];";
28
29
        if (is_array($dataAttributes) === true) {
30
            // only selected set of attributes
31
            foreach ($dataAttributes as $attribute => $match) {
32
                if (is_numeric($attribute) === true) {
33
                    $attribute = $match;
34
                }
35
                $jsonAttribute = Json::encode($attribute);
0 ignored issues
show
Unused Code introduced by
$jsonAttribute 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...
36
                $matchAttribute = Json::encode($match);
37
38
                $dataExpression .= "
39
                if (typeof(data[$matchAttribute]) !== 'undefined') {
40
                    dataVariables.push( '$attribute=' + encodeURIComponent(data[$matchAttribute]) );
41
                }\n";
42
43
            }
44
        } elseif ($dataAttributes === true) {
45
            // all attributes
46
            $dataExpression .= "
47
            for (var attributeName in data) {
48
                dataVariables.push(encodeURIComponent(attributeName) + '=' + encodeURIComponent(data[attributeName]));
49
            }\n";
50
        } else {
51
            $dataExpression = "var dataVariables = '';";
52
        }
53
        $dataExpression .= "dataVariables=dataVariables.join('&'); ";
54
        return new JsExpression("
55
            function(node) {
56
                var \$object = node.reference ? \$(node.reference[0]) : node;
57
                $dataExpression
58
                document.location = $baseUrl + '$union' + dataVariables;
59
                return false;
60
            }
61
            ");
62
    }
63
}
64