Test Failed
Pull Request — master (#48)
by
unknown
03:03
created

src/Collections/LTreeCollection.php (3 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Collections;
6
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
use Umbrellio\LTree\Helpers\LTreeBuilder;
10
use Umbrellio\LTree\Helpers\LTreeNode;
11
use Umbrellio\LTree\Interfaces\LTreeInterface;
12
use Umbrellio\LTree\Interfaces\ModelInterface;
13
14
/**
15
 * @method LTreeInterface|ModelInterface first()
16
 * @property LTreeInterface[]|ModelInterface[] $items
17
 */
18
class LTreeCollection extends Collection
19
{
20
    public function toTree(bool $usingSort = true, bool $loadMissing = true): LTreeNode
21
    {
22
        if (!$model = $this->first()) {
23
            return new LTreeNode();
24
        }
25
26
        if ($loadMissing) {
27
            $this->loadMissingNodes($model);
28
        }
29
30
        $builder = new LTreeBuilder(
31
            $model->getLtreePathColumn(),
32
            $model->getKeyName(),
33
            $model->getLtreeParentColumn()
34
        );
35
36
        return $builder->build($collection ?? $this, $usingSort);
37
    }
38
39
    /**
40
     * This method loads the missing nodes, making the tree branches correct.
41
     */
42
    private function loadMissingNodes($model): self
43
    {
44
        if ($this->hasMissingNodes($model)) {
45
            $this->appendAncestors($model);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param LTreeInterface|ModelInterface $model
53
     */
54
    private function hasMissingNodes($model): bool
55
    {
56
        $paths = collect();
57
58
        foreach ($this->items as $item) {
59
            $paths = $paths->merge($item->getLtreePath());
60
        }
61
62
        return $paths
63
            ->unique()
64
            ->diff($this->pluck($model->getKeyName()))
0 ignored issues
show
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

64
            ->diff($this->pluck($model->/** @scrutinizer ignore-call */ getKeyName()))
Loading history...
65
            ->isNotEmpty();
66
    }
67
68
    /**
69
     * @param LTreeInterface|ModelInterface $model
70
     */
71
    private function appendAncestors($model): void
72
    {
73
        $paths = $this
74
            ->pluck($model->getLtreePathColumn())
0 ignored issues
show
The method getLtreePathColumn() does not exist on Umbrellio\LTree\Interfaces\ModelInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\ModelInterface. ( Ignorable by Annotation )

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

74
            ->pluck($model->/** @scrutinizer ignore-call */ getLtreePathColumn())
Loading history...
75
            ->toArray();
76
        $ids = $this
77
            ->pluck($model->getKeyName())
78
            ->toArray();
79
80
        /** @var Model $model */
81
        $parents = $model::parentsOf($paths)
82
            ->whereKeyNot($ids)
0 ignored issues
show
The method whereKeyNot() does not exist on Illuminate\Database\Eloquent\Builder. ( Ignorable by Annotation )

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

82
            ->/** @scrutinizer ignore-call */ whereKeyNot($ids)

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...
83
            ->get();
84
85
        foreach ($parents as $item) {
86
            $this->add($item);
87
        }
88
    }
89
}
90