Conditions | 10 |
Paths | 15 |
Total Lines | 73 |
Code Lines | 50 |
Lines | 0 |
Ratio | 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 |
||
101 | public function node($ref) |
||
102 | { |
||
103 | $type = $this->alias(); |
||
104 | $table = $this->table(); |
||
105 | $path = []; |
||
106 | |||
107 | if (is_array($ref)) { |
||
108 | $path = implode('/', array_values(array_filter($ref))); |
||
109 | $path = explode('/', $path); |
||
110 | } elseif (is_string($ref)) { |
||
111 | $path = explode('/', $ref); |
||
112 | } |
||
113 | |||
114 | if (empty($path)) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | $start = $path[0]; |
||
119 | unset($path[0]); |
||
120 | |||
121 | $queryData = [ |
||
122 | 'conditions' => [ |
||
123 | "{$type}.lft" . ' <= ' . "{$type}0.lft", |
||
124 | "{$type}.rght" . ' >= ' . "{$type}0.rght", |
||
125 | ], |
||
126 | 'fields' => ['id', 'parent_id', 'alias'], |
||
127 | 'join' => [[ |
||
128 | 'table' => $table, |
||
129 | 'alias' => "{$type}0", |
||
130 | 'type' => 'INNER', |
||
131 | 'conditions' => [ |
||
132 | "{$type}0.alias_hash" => md5($start), |
||
133 | "{$type}0.plugin = {$type}.plugin", |
||
134 | ] |
||
135 | ]], |
||
136 | 'order' => "{$type}.lft" . ' DESC' |
||
137 | ]; |
||
138 | |||
139 | foreach ($path as $i => $alias) { |
||
140 | $j = $i - 1; |
||
141 | |||
142 | $queryData['join'][] = [ |
||
143 | 'table' => $table, |
||
144 | 'alias' => "{$type}{$i}", |
||
145 | 'type' => 'INNER', |
||
146 | 'conditions' => [ |
||
147 | "{$type}{$i}.lft" . ' > ' . "{$type}{$j}.lft", |
||
148 | "{$type}{$i}.rght" . ' < ' . "{$type}{$j}.rght", |
||
149 | "{$type}{$i}.alias_hash" => md5($alias), |
||
150 | "{$type}{$i}.plugin = {$type}{$i}.plugin", |
||
151 | "{$type}{$j}.id" . ' = ' . "{$type}{$i}.parent_id" |
||
152 | ] |
||
153 | ]; |
||
154 | |||
155 | $queryData['conditions'] = [ |
||
156 | 'OR' => [ |
||
157 | "{$type}.lft" . ' <= ' . "{$type}0.lft" . ' AND ' . "{$type}.rght" . ' >= ' . "{$type}0.rght", |
||
158 | "{$type}.lft" . ' <= ' . "{$type}{$i}.lft" . ' AND ' . "{$type}.rght" . ' >= ' . "{$type}{$i}.rght" |
||
159 | ] |
||
160 | ]; |
||
161 | } |
||
162 | $query = $this->find('all', $queryData); |
||
163 | $result = $query->toArray(); |
||
164 | $path = array_values($path); |
||
165 | if (!isset($result[0]) || |
||
166 | (!empty($path) && $result[0]->alias !== $path[count($path) - 1]) || |
||
167 | (empty($path) && $result[0]->alias !== $start) |
||
168 | ) { |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | return $query; |
||
173 | } |
||
174 | } |
||
175 |