Test Setup Failed
Push — prerelease ( 30729b...b0ba5c )
by
unknown
06:23
created

ContextMenuHelper.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace devgroup\JsTreeWidget;
4
5
use Yii;
6
use yii\helpers\Json;
7
use yii\helpers\Url;
8
use yii\web\JsExpression;
9
10
11
class ContextMenuHelper
12
{
13
14
    /**
15
     * Returns JavaScript expression(\yii\web\JsExpression) for context menu item.
16
     * Used for redirecting browser to node-specific action(ie. edit, delete).
17
     * Automatically appends needed $.data() attributes(can be specified in HTML5 data attribute format too).
18
     *
19
     * @param array $route Action route
20
     * @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.
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 157 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
21
     * @return JsExpression
22
     */
23
    public static function actionUrl(array $route, $dataAttributes = true)
24
    {
25
        $baseUrl = Json::encode(Url::to($route));
26
        $union = strpos($baseUrl, '?') > 0 ? '&' : '?';
27
28
        $dataExpression = "var data = \$object.data(), dataVariables = [];";
29
30
        if (is_array($dataAttributes) === true) {
31
            // only selected set of attributes
32
            foreach ($dataAttributes as $attribute => $match) {
33
                if (is_numeric($attribute) === true) {
34
                    $attribute = $match;
35
                }
36
                $jsonAttribute = Json::encode($attribute);
0 ignored issues
show
$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...
37
                $matchAttribute = Json::encode($match);
38
39
                $dataExpression .= "
40
                if (typeof(data[$matchAttribute]) !== 'undefined') {
41
                    dataVariables.push( '$attribute=' + encodeURIComponent(data[$matchAttribute]) );
42
                }\n";
43
44
            }
45
        } elseif ($dataAttributes === true) {
46
            // all attributes
47
            $dataExpression .= "
48
            for (var attributeName in data) {
49
                dataVariables.push(encodeURIComponent(attributeName) + '=' + encodeURIComponent(data[attributeName]));
50
            };\n";
51
        } else {
52
            $dataExpression = "var dataVariables = '';";
53
        }
54
        $dataExpression .= "dataVariables=dataVariables.join('&'); ";
55
        return new JsExpression(
56
            "function(node) {
57
                var \$object = node.reference ? \$(node.reference[0]) : node;
58
                $dataExpression
59
                document.location = $baseUrl + '$union' + dataVariables;
60
                return false;
61
            }"
62
        );
63
    }
64
}