LTreeHelper::dropDescendants()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 8
c 2
b 1
f 0
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Helpers;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\DB;
9
use Umbrellio\LTree\Interfaces\LTreeInterface;
10
use Umbrellio\LTree\Interfaces\LTreeModelInterface;
11
use Umbrellio\LTree\Types\LTreeType;
12
13
class LTreeHelper
14
{
15
    /**
16
     * @param LTreeInterface|Model $model
17
     */
18 3
    public function buildPath($model): void
19
    {
20 3
        $pathValue = [];
21 3
        if ($model->getLtreeParentId()) {
0 ignored issues
show
Bug introduced by
The method getLtreeParentId() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        if ($model->/** @scrutinizer ignore-call */ getLtreeParentId()) {
Loading history...
Bug Best Practice introduced by
The expression $model->getLtreeParentId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
22 1
            $parent = $model->ltreeParent;
23 1
            $pathValue = array_merge($pathValue, $parent->getLtreePath());
24
        }
25 3
        $pathValue[] = $model->getKey();
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        $pathValue[] = $model->getKey();
Loading history...
Bug introduced by
The method getKey() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        /** @scrutinizer ignore-call */ 
26
        $pathValue[] = $model->getKey();
Loading history...
26 3
        DB::statement(sprintf(
27 3
            "UPDATE %s SET %s = text2ltree('%s') WHERE %s = %s",
28 3
            $model->getTable(),
0 ignored issues
show
Bug introduced by
The method getTable() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Illuminate\Database\Eloquent\Relations\Pivot. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $model->/** @scrutinizer ignore-call */ 
29
                    getTable(),
Loading history...
Bug introduced by
The method getTable() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            $model->/** @scrutinizer ignore-call */ 
29
                    getTable(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29 3
            $model->getLtreePathColumn(),
0 ignored issues
show
Bug introduced by
The method getLtreePathColumn() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $model->/** @scrutinizer ignore-call */ 
30
                    getLtreePathColumn(),
Loading history...
30 3
            implode(LTreeType::TYPE_SEPARATE, $pathValue),
31 3
            $model->getKeyName(),
0 ignored issues
show
Bug introduced by
The method getKeyName() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $model->/** @scrutinizer ignore-call */ 
32
                    getKeyName(),
Loading history...
Bug introduced by
The method getKeyName() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $model->/** @scrutinizer ignore-call */ 
32
                    getKeyName(),
Loading history...
32 3
            $model->getKey()
33 3
        ));
34 3
        $model->refresh();
0 ignored issues
show
Bug introduced by
The method refresh() does not exist on Illuminate\Database\Eloquent\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $model->/** @scrutinizer ignore-call */ 
35
                refresh();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method refresh() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $model->/** @scrutinizer ignore-call */ 
35
                refresh();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
    }
36
37
    /**
38
     * @param LTreeInterface|Model $model
39
     * @param LTreeInterface|Model|null $to
40
     */
41 2
    public function moveNode($model, $to = null, array $columns = []): void
42
    {
43 2
        $pathName = $model->getLtreePathColumn();
44 2
        $oldPath = $model->getLtreePath(LTreeModelInterface::AS_STRING);
0 ignored issues
show
Bug introduced by
The method getLtreePath() does not exist on Illuminate\Database\Eloquent\Model. It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $oldPath = $model->getLtreePath(LTreeModelInterface::AS_STRING);
Loading history...
45 2
        $newPath = $to ? $to->getLtreePath(LTreeModelInterface::AS_STRING) : '';
46 2
        $expressions = static::wrapExpressions($columns);
0 ignored issues
show
Bug Best Practice introduced by
The method Umbrellio\LTree\Helpers\...lper::wrapExpressions() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $expressions = static::wrapExpressions($columns);
Loading history...
47 2
        $expressions[] = "
48 2
            \"${pathName}\" = (text2ltree('${newPath}') || subpath(\"${pathName}\", (nlevel(text2ltree('${oldPath}')) - 1)))
49 2
        ";
50
51 2
        DB::statement(sprintf(
52 2
            "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true",
53 2
            $model->getTable(),
54 2
            implode(', ', $expressions),
55 2
            $pathName,
56 2
            $oldPath
57 2
        ));
58 2
        $model->refresh();
59
    }
60
61
    /**
62
     * @param LTreeInterface|Model $model
63
     */
64 1
    public function dropDescendants($model, array $columns = []): void
65
    {
66 1
        $sql = sprintf(
67 1
            "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true",
68 1
            $model->getTable(),
69 1
            implode(', ', static::wrapExpressions($columns)),
0 ignored issues
show
Bug Best Practice introduced by
The method Umbrellio\LTree\Helpers\...lper::wrapExpressions() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            implode(', ', static::/** @scrutinizer ignore-call */ wrapExpressions($columns)),
Loading history...
70 1
            $model->getLtreePathColumn(),
71 1
            $model->getLtreePath(LTreeModelInterface::AS_STRING)
72 1
        );
73 1
        DB::statement($sql);
74 1
        $model->refresh();
75
    }
76
77 3
    private function wrapExpressions(array $columns): array
78
    {
79 3
        $expressions = [];
80 3
        foreach ($columns as $column => $value) {
81
            switch (true) {
82
                case $value === null:
83 1
                    $expressions[] = sprintf('%s = null', (string) $column);
84 1
                    break;
85 3
                case is_string($value):
86 3
                    $expressions[] = sprintf("%s = '%s'", (string) $column, (string) $value);
87 3
                    break;
88
                default:
89 1
                    $expressions[] = sprintf('%s = %s', (string) $column, (string) $value);
90
            }
91
        }
92 3
        return $expressions;
93
    }
94
}
95