Issues (56)

src/Traits/HasTreeRelationships.php (2 issues)

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 3
    final protected function belongsToAncestorsTree(
23
        string $related,
24
        string $throwRelation,
25
        ?string $foreignKey = null,
26
        $ownerKey = null
27
    ) {
28 3
        return $this->belongsToTree(BelongsToAncestorsTree::class, $related, $throwRelation, $foreignKey, $ownerKey);
29
    }
30
31
32 1
    final protected function belongsToDescendantsTree(
33
        string $related,
34
        string $throwRelation,
35
        ?string $foreignKey = null,
36
        $ownerKey = null
37
    ) {
38 1
        return $this->belongsToTree(
39 1
            BelongsToDescendantsTree::class,
40 1
            $related,
41 1
            $throwRelation,
42 1
            $foreignKey,
43 1
            $ownerKey
44 1
        );
45
    }
46
47 4
    final protected function belongsToTree(
48
        string $relationClass,
49
        string $related,
50
        string $throwRelation,
51
        ?string $foreignKey = null,
52
        $ownerKey = null
53
    ): AbstractBelongsToTree {
54 4
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
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

54
        /** @scrutinizer ignore-call */ 
55
        $instance = $this->newRelatedInstance($related);
Loading history...
55
56 4
        if (!$instance instanceof LTreeModelInterface) {
57 1
            throw new InvalidTraitInjectionClass(sprintf(
58 1
                'A class using this trait must implement an interface %s',
59 1
                LTreeModelInterface::class
60 1
            ));
61
        }
62
63 3
        if ($foreignKey === null) {
64 3
            $foreignKey = $this
65 3
                ->{$throwRelation}()
66 3
                ->getForeignKeyName();
67
        }
68
69 3
        $ownerKey = $ownerKey ?: $instance->getKeyName();
70
71 3
        return new $relationClass($instance->newQuery(), $this, $throwRelation, $foreignKey, $ownerKey);
0 ignored issues
show
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

71
        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...
72
    }
73
}
74