Passed
Push — master ( a59bea...a966ee )
by
unknown
06:43
created

HasTreeRelationships   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 1
b 0
f 0
dl 0
loc 57
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A belongsToAncestorsTree() 0 7 1
A belongsToTree() 0 25 4
A belongsToDescendantsTree() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Traits;
6
7
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
8
use Illuminate\Database\Eloquent\Model;
9
use Umbrellio\LTree\Exceptions\InvalidTraitInjectionClass;
10
use Umbrellio\LTree\Interfaces\LTreeModelInterface;
11
use Umbrellio\LTree\Relations\AbstractBelongsToTree;
12
use Umbrellio\LTree\Relations\BelongsToAncestorsTree;
13
use Umbrellio\LTree\Relations\BelongsToDescendantsTree;
14
15
/**
16
 * @mixin HasRelationships
17
 * @mixin LTreeModelTrait
18
 * @mixin Model
19
 */
20
trait HasTreeRelationships
21
{
22
    /**
23
     * @throws InvalidTraitInjectionClass
24
     */
25 3
    final protected function belongsToAncestorsTree(
26
        string $related,
27
        string $throwRelation,
28
        ?string $foreignKey = null,
29
        $ownerKey = null
30
    ) {
31 3
        return $this->belongsToTree(BelongsToAncestorsTree::class, $related, $throwRelation, $foreignKey, $ownerKey);
32
    }
33
34
    /**
35
     * @throws InvalidTraitInjectionClass
36
     */
37 1
    final protected function belongsToDescendantsTree(
38
        string $related,
39
        string $throwRelation,
40
        ?string $foreignKey = null,
41
        $ownerKey = null
42
    ) {
43 1
        return $this->belongsToTree(
44 1
            BelongsToDescendantsTree::class,
45
            $related,
46
            $throwRelation,
47
            $foreignKey,
48
            $ownerKey
49
        );
50
    }
51
52 4
    final private function belongsToTree(
53
        string $relationClass,
54
        string $related,
55
        string $throwRelation,
56
        ?string $foreignKey = null,
57
        $ownerKey = null
58
    ): AbstractBelongsToTree {
59 4
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() 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

59
        /** @scrutinizer ignore-call */ 
60
        $instance = $this->newRelatedInstance($related);
Loading history...
60
61 4
        if (!$instance instanceof LTreeModelInterface) {
62 1
            throw new InvalidTraitInjectionClass(sprintf(
63 1
                'A class using this trait must implement an interface %s',
64 1
                LTreeModelInterface::class
65
            ));
66
        }
67
68 3
        if ($foreignKey === null) {
69
            $foreignKey = $this
70 3
                ->{$throwRelation}()
71 3
                ->getForeignKeyName();
72
        }
73
74 3
        $ownerKey = $ownerKey ?: $instance->getKeyName();
75
76 3
        return new $relationClass($instance->newQuery(), $this, $throwRelation, $foreignKey, $ownerKey);
0 ignored issues
show
Bug introduced by
The method newQuery() does not exist on Umbrellio\LTree\Interfaces\LTreeModelInterface. ( Ignorable by Annotation )

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

76
        return new $relationClass($instance->/** @scrutinizer ignore-call */ newQuery(), $this, $throwRelation, $foreignKey, $ownerKey);

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...
77
    }
78
}
79