Passed
Pull Request — master (#232)
by
unknown
15:42
created

HasRecursiveRelationships::isChildOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 1
c 4
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
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
        return $this->children->contains($model);
17
    }
18
19
    public function isChildOf(Model $model)
20
    {
21
        return $this->parent ? $this->parent->is($model) : false;
22
    }
23
24
    public function depthRelatedTo(Model $model)
25
    {
26
        $currentModel = $this->bloodline->firstWhere('id', $this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

26
        $currentModel = $this->bloodline->firstWhere('id', $this->/** @scrutinizer ignore-call */ getKey());
Loading history...
27
        $relatedModel = $this->bloodline->firstWhere('id', $model->getKey());
28
29
        if ($currentModel && $relatedModel) {
30
            return $currentModel->depth - $relatedModel->depth;
31
        }
32
33
        return null;
34
    }
35
}
36