Conditions | 5 |
Paths | 16 |
Total Lines | 96 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
170 | public function checkIntegrity(): array |
||
171 | { |
||
172 | $table = $this->getTable(); |
||
173 | $pk = $table->aliasField($table->getPrimaryKey()); |
||
|
|||
174 | $left = $table->aliasField($this->getConfigOrFail('left')); |
||
175 | $right = $table->aliasField($this->getConfigOrFail('right')); |
||
176 | $parent = $table->aliasField($this->getConfigOrFail('parent')); |
||
177 | $childAlias = sprintf('Child%s', $table->getAlias()); |
||
178 | $siblingAlias = sprintf('Sibling%s', $table->getAlias()); |
||
179 | |||
180 | $exists = function (Query $query): bool { |
||
181 | return $query->select(['existing' => 1])->limit(1)->execute()->count() > 0; |
||
182 | }; |
||
183 | |||
184 | $errors = []; |
||
185 | |||
186 | // Check that for every record `lft < rght`. |
||
187 | $query = $table->find() |
||
188 | ->where(function (QueryExpression $exp) use ($left, $right): QueryExpression { |
||
189 | return $exp->gte($left, new IdentifierExpression($right)); |
||
190 | }); |
||
191 | if ($exists($query)) { |
||
192 | $errors[] = sprintf('Found record where %s >= %s', $this->getConfigOrFail('left'), $this->getConfigOrFail('right')); |
||
193 | } |
||
194 | |||
195 | // Check that for every parent, `parent.lft + 1 = MIN(children.lft)` |
||
196 | $query = $table->find() |
||
197 | ->innerJoin( |
||
198 | [$childAlias => $table->getTable()], |
||
199 | function (QueryExpression $exp) use ($pk, $childAlias): QueryExpression { |
||
200 | return $exp |
||
201 | ->equalFields($pk, sprintf('%s.%s', $childAlias, $this->getConfigOrFail('parent'))); |
||
202 | } |
||
203 | ) |
||
204 | ->group([$pk, $left]) |
||
205 | ->having(function (QueryExpression $exp, Query $query) use ($childAlias, $left): QueryExpression { |
||
206 | return $exp->notEq( |
||
207 | new Comparison($left, 1, null, '+'), |
||
208 | $query->func()->min(sprintf('%s.%s', $childAlias, $this->getConfigOrFail('left'))) |
||
209 | ); |
||
210 | }); |
||
211 | if ($exists($query)) { |
||
212 | $errors[] = sprintf('Found record where parent.%s + 1 != MIN(children.%1$s)', $this->getConfigOrFail('left')); |
||
213 | } |
||
214 | |||
215 | // Check that for every parent, `parent.rght - 1 = MAX(children.rght)` |
||
216 | $query = $table->find() |
||
217 | ->innerJoin( |
||
218 | [$childAlias => $table->getTable()], |
||
219 | function (QueryExpression $exp) use ($pk, $childAlias): QueryExpression { |
||
220 | return $exp |
||
221 | ->equalFields($pk, sprintf('%s.%s', $childAlias, $this->getConfigOrFail('parent'))); |
||
222 | } |
||
223 | ) |
||
224 | ->group([$pk, $right]) |
||
225 | ->having(function (QueryExpression $exp, Query $query) use ($childAlias, $right): QueryExpression { |
||
226 | return $exp->notEq( |
||
227 | new Comparison($right, 1, null, '-'), |
||
228 | $query->func()->max(sprintf('%s.%s', $childAlias, $this->getConfigOrFail('right'))) |
||
229 | ); |
||
230 | }); |
||
231 | if ($exists($query)) { |
||
232 | $errors[] = sprintf('Found record where parent.%s - 1 != MAX(children.%1$s)', $this->getConfigOrFail('right')); |
||
233 | } |
||
234 | |||
235 | // Check that for every node, `node.lft - 1 = MAX(sibling.rght)` where `sibling.lft <= node.lft`. |
||
236 | $query = $table->find() |
||
237 | ->innerJoin( |
||
238 | [$siblingAlias => $table->getTable()], |
||
239 | function (QueryExpression $exp) use ($table, $left, $parent, $pk, $siblingAlias): QueryExpression { |
||
240 | return $exp |
||
241 | ->add($exp->or(function (QueryExpression $exp) use ($parent, $siblingAlias): QueryExpression { |
||
242 | $siblingParent = sprintf('%s.%s', $siblingAlias, $this->getConfigOrFail('parent')); |
||
243 | |||
244 | return $exp |
||
245 | ->equalFields($parent, $siblingParent) |
||
246 | ->add($exp->and(function (QueryExpression $exp) use ($parent, $siblingParent): QueryExpression { |
||
247 | return $exp->isNull($parent)->isNull($siblingParent); |
||
248 | })); |
||
249 | })) |
||
250 | ->gte($left, new IdentifierExpression(sprintf('%s.%s', $siblingAlias, $this->getConfigOrFail('left')))) |
||
251 | ->notEq($pk, new IdentifierExpression(sprintf('%s.%s', $siblingAlias, $table->getPrimaryKey()))); |
||
252 | } |
||
253 | ) |
||
254 | ->group([$pk, $left]) |
||
255 | ->having(function (QueryExpression $exp, Query $query) use ($siblingAlias, $left): QueryExpression { |
||
256 | return $exp->notEq( |
||
257 | new Comparison($left, 1, null, '-'), |
||
258 | $query->func()->max(sprintf('%s.%s', $siblingAlias, $this->getConfigOrFail('right'))) |
||
259 | ); |
||
260 | }); |
||
261 | if ($exists($query)) { |
||
262 | $errors[] = sprintf('Found record where %s - 1 != MAX(previousSiblings.%s)', $this->getConfigOrFail('left'), $this->getConfigOrFail('right')); |
||
263 | } |
||
264 | |||
265 | return $errors; |
||
266 | } |
||
268 |