Failed Conditions
Branch master (e9fe4e)
by Dallas
02:46
created

HasTreeRelationships::belongsToTree()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 24
rs 9.9
cc 4
nc 3
nop 4
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\BelongsToTree;
12
13
/**
14
 * @mixin HasRelationships
15
 * @mixin LTreeModelTrait
16
 * @mixin Model
17
 */
18
trait HasTreeRelationships
19
{
20
    /**
21
     * @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...
22
     * @return BelongsToTree
23
     *
24
     * @throws InvalidTraitInjectionClass
25
     */
26
    protected function belongsToTree(
27
        string $related,
28
        string $throwRelation,
29
        ?string $foreignKey = null,
30
        $ownerKey = null
31
    ) {
32
        $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

32
        /** @scrutinizer ignore-call */ 
33
        $instance = $this->newRelatedInstance($related);
Loading history...
33
34
        if (!$instance instanceof LTreeModelInterface) {
35
            throw new InvalidTraitInjectionClass(sprintf(
36
                'A class using this trait must implement an interface %s',
37
                LTreeModelInterface::class
38
            ));
39
        }
40
41
        if ($foreignKey === null) {
42
            $foreignKey = $this
43
                ->{$throwRelation}()
44
                ->getForeignKeyName();
45
        }
46
47
        $ownerKey = $ownerKey ?: $instance->getKeyName();
0 ignored issues
show
introduced by
$ownerKey is of type null, thus it always evaluated to false.
Loading history...
48
49
        return new BelongsToTree($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

49
        return new BelongsToTree($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...
Bug introduced by
$this of type Umbrellio\LTree\Traits\HasTreeRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of Umbrellio\LTree\Relation...gsToTree::__construct(). ( Ignorable by Annotation )

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

49
        return new BelongsToTree($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $throwRelation, $foreignKey, $ownerKey);
Loading history...
50
    }
51
}
52