Passed
Push — master ( d8e957...a3e243 )
by Maksim
03:45
created

CompositeRelationships::belongsTo()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 30
rs 9.6111
c 0
b 0
f 0
cc 5
nc 6
nop 4
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Http\Traits;
4
5
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Str;
9
use MaksimM\CompositePrimaryKeys\Eloquent\Relationships\CompositeBelongsTo;
10
11
trait CompositeRelationships
12
{
13
14
    /**
15
     * @param      $related
16
     * @param null $foreignKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $foreignKey is correct as it would always require null to be passed?
Loading history...
17
     * @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...
18
     * @param null $relation
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $relation is correct as it would always require null to be passed?
Loading history...
19
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
20
     */
21
    public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
22
    {
23
        // If no relation name was given, we will use this debug backtrace to extract
24
        // the calling method's name and use that as the relationship name as most
25
        // of the time this will be what we desire to use for the relationships.
26
        if (is_null($relation)) {
0 ignored issues
show
introduced by
The condition is_null($relation) is always true.
Loading history...
27
            $relation = $this->guessBelongsToRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToRelation() 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

27
            /** @scrutinizer ignore-call */ 
28
            $relation = $this->guessBelongsToRelation();
Loading history...
28
        }
29
30
        /**
31
         * @var Model $instance
32
         */
33
        $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

33
        /** @scrutinizer ignore-call */ 
34
        $instance = $this->newRelatedInstance($related);
Loading history...
34
35
        // If no foreign key was supplied, we can use a backtrace to guess the proper
36
        // foreign key name by using the name of the relationship function, which
37
        // when combined with an "_id" should conventionally match the columns.
38
        if (is_null($foreignKey)) {
0 ignored issues
show
introduced by
The condition is_null($foreignKey) is always true.
Loading history...
39
            $foreignKey = is_array($instance->getKeyName()) ? array_map(function($key) use ($relation) {
0 ignored issues
show
introduced by
The condition is_array($instance->getKeyName()) is always false.
Loading history...
40
                return Str::snake($relation).'_'.$key;
41
            }, $instance->getKeyName()) : Str::snake($relation).'_'.$instance->getKeyName();
42
        }
43
44
        // Once we have the foreign key names, we'll just create a new Eloquent query
45
        // for the related models and returns the relationship instance which will
46
        // actually be responsible for retrieving and hydrating every relations.
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 $this->newBelongsTo(
50
            $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
0 ignored issues
show
Bug introduced by
$this of type MaksimM\CompositePrimary...\CompositeRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of MaksimM\CompositePrimary...onships::newBelongsTo(). ( Ignorable by Annotation )

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

50
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $foreignKey, $ownerKey, $relation
Loading history...
51
        );
52
    }
53
54
    /**
55
     * Instantiate a new BelongsTo relationship.
56
     *
57
     * @param  \Illuminate\Database\Eloquent\Builder  $query
58
     * @param  \Illuminate\Database\Eloquent\Model  $child
59
     * @param  string  $foreignKey
60
     * @param  string  $ownerKey
61
     * @param  string  $relation
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
65
    {
66
        return new CompositeBelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
67
    }
68
69
}