Conditions | 20 |
Paths | 4657 |
Total Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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; |
||
|
|||
62 | $ids = null; |
||
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; |
||
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]) { |
||
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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.