GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JSSelectableTreeGetTree::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace app\backend\actions;
4
5
use app;
6
use yii;
7
use yii\base\Action;
8
use yii\base\InvalidConfigException;
9
10
class JSSelectableTreeGetTree extends Action
11
{
12
    public $modelName = null;
13
14
    public $label_attribute = 'name';
15
    public $parent_attribute = 'parent_id';
16
    public $additional_search_conditions = [];
17
    public $query_parent_attribute = 'id';
18
    public $vary_by_type_attribute = 'show_type';
19
    public $order = [];
20
21
22 View Code Duplication
    public function init()
23
    {
24
        if (!isset($this->modelName)) {
25
            throw new InvalidConfigException("Model name should be set in controller actions");
26
        }
27
        if (!class_exists($this->modelName)) {
28
            throw new InvalidConfigException("Model class does not exists");
29
        }
30
    }
31
32
    public function run()
33
    {
34
        if (!isset($_GET[$this->query_parent_attribute])) {
35
            throw new yii\web\NotFoundHttpException;
36
        }
37
38
        $modelName = $this->modelName;
39
        $selectedItems = Yii::$app->request->get('selectedItems', []);
40
        if (!empty($selectedItems)) {
41
            $selectedItems = explode(',', $selectedItems);
42
        }
43
        $parents = [0];
44
        $ids = $selectedItems;
45
        $q = new yii\db\Query;
46
        $q->select('parent_id')
47
            ->from($modelName::tableName());
48
        while (!empty($ids)) {
49
            $q->where(['id' => $ids]);
50
            $result = $q->all();
51
            $ids = [];
52
            foreach ($result as $row) {
53
                if (!in_array($row['parent_id'], $parents)) {
54
                    $parents[] = $row['parent_id'];
55
                }
56
                if ($row['parent_id'] != 0 && !in_array($row['parent_id'], $ids)) {
57
                    $ids[] = $row['parent_id'];
58
                }
59
            }
60
        }
61
        $q = null;
0 ignored issues
show
Unused Code introduced by
$q 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...
62
        $ids = null;
0 ignored issues
show
Unused Code introduced by
$ids 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...
63
        //
64
        Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
65
66
        $query = new yii\db\Query;
67
68
        $fields = 'id, '.$this->label_attribute.', '.$this->parent_attribute;
69
        if (isset($this->vary_by_type_attribute)) {
70
            $fields .= ', '.$this->vary_by_type_attribute;
71
        }
72
73
        if (isset($this->expand_in_admin_attribute)) {
74
            $fields .= ', '.$this->expand_in_admin_attribute;
0 ignored issues
show
Documentation introduced by
The property expand_in_admin_attribute does not exist on object<app\backend\actio...SSelectableTreeGetTree>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
        }
76
77
        $fields .= ', (';
78
        $fields .= 'SELECT count(id) FROM '.$modelName::tableName().' counter ';
79
        $fields .= 'WHERE counter.parent_id = '.$modelName::tableName().'.id)';
80
        $fields .= " AS 'children_count'";
81
82
        $query->select($fields)
83
            ->from($modelName::tableName())
84
            ->where([$this->parent_attribute => $_GET[$this->query_parent_attribute]])
85
            ->andWhere($this->additional_search_conditions)
86
            ->orderBy($this->order);
87
88
        $rows = $query
89
            ->all();
90
        $result = [];
91
        foreach ($rows as $row) {
92
            $item = [
93
                'id' => $row['id'],
94
                'text' => $row[$this->label_attribute],
95
            ];
96
            if (isset($this->vary_by_type_attribute)) {
97
                $item['type'] = $row[$this->vary_by_type_attribute];
98
            }
99
            $item['children'] = $row['children_count'] > 0;
100
            $item['a_attr'] = ['data-id'=>$row['id'], 'data-parent-id'=>$row['parent_id']];
101
            $item['state'] = [];
102
103
            if (in_array($row['id'], $parents)) {
104
                $item['state']['opened'] = true;
105
            }
106
            if (is_array($selectedItems) && in_array($row['id'], $selectedItems)) {
107
                $item['state']['selected'] = true;
108
            }
109
110
            if (isset($this->expand_in_admin_attribute)) {
111
                if ($row[$this->expand_in_admin_attribute]) {
0 ignored issues
show
Documentation introduced by
The property expand_in_admin_attribute does not exist on object<app\backend\actio...SSelectableTreeGetTree>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
                    $item['state']['opened'] = true;
113
                }
114
            }
115
            if (isset($_GET['selected_id']) && $item['id'] == $_GET['selected_id']) {
116
                $item['state']['selected'] = true;
117
            }
118
            if (count($item['state']) == 0) {
119
                unset($item['state']);
120
            }
121
122
123
            $result[] = $item;
124
        }
125
126
        return $result;
127
    }
128
}
129