Failed Conditions
Pull Request — master (#59)
by Dallas
13:19
created

HasTreeRelationships   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
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 63
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A belongsToParentsTree() 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\BelongsToDescendantsTree;
13
use Umbrellio\LTree\Relations\BelongsToParentsTree;
14
15
/**
16
 * @mixin HasRelationships
17
 * @mixin LTreeModelTrait
18
 * @mixin Model
19
 */
20
trait HasTreeRelationships
21
{
22
    /**
23
     * @param null $ownerKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ownerKey is correct as it would always require null to be passed?
Loading history...
24
     * @return AbstractBelongsToTree
25
     *
26
     * @throws InvalidTraitInjectionClass
27
     */
28 3
    final protected function belongsToParentsTree(
29
        string $related,
30
        string $throwRelation,
31
        ?string $foreignKey = null,
32
        $ownerKey = null
33
    ) {
34 3
        return $this->belongsToTree(BelongsToParentsTree::class, $related, $throwRelation, $foreignKey, $ownerKey);
35
    }
36
37
    /**
38
     * @param null $ownerKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ownerKey is correct as it would always require null to be passed?
Loading history...
39
     * @return AbstractBelongsToTree
40
     *
41
     * @throws InvalidTraitInjectionClass
42
     */
43 1
    final protected function belongsToDescendantsTree(
44
        string $related,
45
        string $throwRelation,
46
        ?string $foreignKey = null,
47
        $ownerKey = null
48
    ) {
49 1
        return $this->belongsToTree(
50 1
            BelongsToDescendantsTree::class,
51
            $related,
52
            $throwRelation,
53
            $foreignKey,
54
            $ownerKey
55
        );
56
    }
57
58 4
    final private function belongsToTree(
59
        string $relationClass,
60
        string $related,
61
        string $throwRelation,
62
        ?string $foreignKey = null,
63
        $ownerKey = null
64
    ): AbstractBelongsToTree {
65 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

65
        /** @scrutinizer ignore-call */ 
66
        $instance = $this->newRelatedInstance($related);
Loading history...
66
67 4
        if (!$instance instanceof LTreeModelInterface) {
68 1
            throw new InvalidTraitInjectionClass(sprintf(
69 1
                'A class using this trait must implement an interface %s',
70 1
                LTreeModelInterface::class
71
            ));
72
        }
73
74 3
        if ($foreignKey === null) {
75
            $foreignKey = $this
76 3
                ->{$throwRelation}()
77 3
                ->getForeignKeyName();
78
        }
79
80 3
        $ownerKey = $ownerKey ?: $instance->getKeyName();
81
82 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

82
        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...
83
    }
84
}
85