Passed
Pull Request — master (#40)
by Giacomo
05:03
created

EmbedsRelationships   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A embedsMany() 0 24 4
A embedsOne() 0 24 4
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Eloquent;
4
5
use Illuminate\Support\Str;
6
use OfflineAgency\MongoAutoSync\Relationships\EmbedsMany;
7
use OfflineAgency\MongoAutoSync\Relationships\EmbedsOne;
8
9
trait EmbedsRelationships
10
{
11
    /**
12
     * Define an embedded one-to-many relationship.
13
     * @param string $related
14
     * @param string $localKey
15
     * @param string $foreignKey
16
     * @param string $relation
17
     * @return EmbedsMany
18
     */
19
    protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
20
    {
21
        // If no relation name was given, we will use this debug backtrace to extract
22
        // the calling method's name and use that as the relationship name as most
23
        // of the time this will be what we desire to use for the relationships.
24
        if ($relation === null) {
25
            [, $caller] = debug_backtrace(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $options of debug_backtrace(). ( Ignorable by Annotation )

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

25
            [, $caller] = debug_backtrace(/** @scrutinizer ignore-type */ false);
Loading history...
26
27
            $relation = $caller['function'];
28
        }
29
30
        if ($localKey === null) {
31
            $localKey = $relation;
32
        }
33
34
        if ($foreignKey === null) {
35
            $foreignKey = Str::snake(class_basename($this));
36
        }
37
38
        $query = $this->newQuery();
0 ignored issues
show
Bug introduced by
It seems like newQuery() 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

38
        /** @scrutinizer ignore-call */ 
39
        $query = $this->newQuery();
Loading history...
39
40
        $instance = new $related;
41
42
        return new EmbedsMany($query, $this, $instance, $localKey, $foreignKey, $relation);
0 ignored issues
show
Bug introduced by
$this of type OfflineAgency\MongoAutoS...ent\EmbedsRelationships is incompatible with the type Jenssegers\Mongodb\Eloquent\Model expected by parameter $parent of OfflineAgency\MongoAutoS...bedsMany::__construct(). ( Ignorable by Annotation )

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

42
        return new EmbedsMany($query, /** @scrutinizer ignore-type */ $this, $instance, $localKey, $foreignKey, $relation);
Loading history...
43
    }
44
45
    /**
46
     * Define an embedded one-to-many relationship.
47
     * @param string $related
48
     * @param string $localKey
49
     * @param string $foreignKey
50
     * @param string $relation
51
     * @return EmbedsOne
52
     */
53
    protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null)
54
    {
55
        // If no relation name was given, we will use this debug backtrace to extract
56
        // the calling method's name and use that as the relationship name as most
57
        // of the time this will be what we desire to use for the relationships.
58
        if ($relation === null) {
59
            [, $caller] = debug_backtrace(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $options of debug_backtrace(). ( Ignorable by Annotation )

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

59
            [, $caller] = debug_backtrace(/** @scrutinizer ignore-type */ false);
Loading history...
60
61
            $relation = $caller['function'];
62
        }
63
64
        if ($localKey === null) {
65
            $localKey = $relation;
66
        }
67
68
        if ($foreignKey === null) {
69
            $foreignKey = Str::snake(class_basename($this));
70
        }
71
72
        $query = $this->newQuery();
73
74
        $instance = new $related;
75
76
        return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation);
0 ignored issues
show
Bug introduced by
$this of type OfflineAgency\MongoAutoS...ent\EmbedsRelationships is incompatible with the type Jenssegers\Mongodb\Eloquent\Model expected by parameter $parent of OfflineAgency\MongoAutoS...mbedsOne::__construct(). ( Ignorable by Annotation )

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

76
        return new EmbedsOne($query, /** @scrutinizer ignore-type */ $this, $instance, $localKey, $foreignKey, $relation);
Loading history...
77
    }
78
}
79