Passed
Pull Request — master (#232)
by
unknown
14:26
created

HasRecursiveRelationships::isParentOf()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
c 3
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Staudenmeir\LaravelAdjacencyList\Eloquent\Traits\HasAdjacencyList;
7
use Staudenmeir\LaravelCte\Eloquent\QueriesExpressions;
8
9
trait HasRecursiveRelationships
10
{
11
    use HasAdjacencyList;
12
    use QueriesExpressions;
13
14
    public function isParentOf(Model $model)
15
    {
16
        if (!$this->relationLoaded('children')) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

16
        if (!$this->/** @scrutinizer ignore-call */ relationLoaded('children')) {
Loading history...
17
            $this->load('children');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

17
            $this->/** @scrutinizer ignore-call */ 
18
                   load('children');
Loading history...
18
        }
19
        return $this->children ? $this->children->contains($model) : false;
20
    }
21
22
    public function isChildOf(Model $model)
23
    {
24
        if (!$this->relationLoaded('parent')) {
25
            $this->load('parent');
26
        }
27
        return $this->parent ? $this->parent->is($model) : false;
28
    }
29
30
    public function depthRelatedTo(Model $model)
31
    {
32
        if (!$this->relationLoaded('ancestors')) {
33
            $this->load('ancestors');
34
        }
35
36
        $index = $this->ancestors->search(function ($ancestor) use ($model) {
37
            return $ancestor->getKey() === $model->getKey();
38
        });
39
40
        return $index !== false ? $index + 1 : null;
41
    }
42
}
43