Passed
Push — master ( 0adf1b...6f6aed )
by Jonas
20:09 queued 15:42
created

HasRecursiveRelationshipHelpers::isChildOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
trait HasRecursiveRelationshipHelpers
8
{
9
    /**
10
     * Determine if the model is a child of the given model.
11
     *
12
     * @param \Illuminate\Database\Eloquent\Model $model
13
     * @return bool
14
     */
15
    public function isChildOf(Model $model): bool
16
    {
17
        return $this->parent ? $this->parent->is($model) : false;
18
    }
19
20
    /**
21
     * Determine if the model is the parent of the given model.
22
     *
23
     * @param \Illuminate\Database\Eloquent\Model $model
24
     * @return bool
25
     */
26
    public function isParentOf(Model $model): bool
27
    {
28
        return $this->children->contains($model);
29
    }
30
31
    /**
32
     * Get the depth of the model related to the given model.
33
     *
34
     * @param \Illuminate\Database\Eloquent\Model $model
35
     * @return int|null
36
     */
37
    public function getDepthRelatedTo(Model $model): ?int
38
    {
39
        $thisModel = $this->bloodline->find($this);
40
        $relatedModel = $this->bloodline->find($model);
41
42
        if ($thisModel && $relatedModel) {
43
            $depthName = $this->getDepthName();
0 ignored issues
show
Bug introduced by
It seems like getDepthName() 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

43
            /** @scrutinizer ignore-call */ 
44
            $depthName = $this->getDepthName();
Loading history...
44
45
            return $thisModel->$depthName - $relatedModel->$depthName;
46
        }
47
48
        return null;
49
    }
50
}
51