Test Failed
Push — master ( c3cd17...167a1a )
by Iman
13:05
created

BaseEloquentOverrides   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B morphToMany() 0 26 7
A getRelationValue() 0 16 4
1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
trait BaseEloquentOverrides
6
{
7
    /**
8
     * Define a polymorphic many-to-many relationship.
9
     *
10
     * @param  string  $related
11
     * @param  string  $name
12
     * @param  string  $table
13
     * @param  string  $foreignPivotKey
14
     * @param  string  $relatedPivotKey
15
     * @param  string  $parentKey
16
     * @param  string  $relatedKey
17
     * @param  bool  $inverse
18
     * @param  string  $caller
19
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
20
     */
21
    public function morphToMany($related, $name, $table = null, $foreignPivotKey = null,
22
        $relatedPivotKey = null, $parentKey = null,
23
        $relatedKey = null, $inverse = false, $caller = null)
24
    {
25
        $caller = $caller ?: $this->guessBelongsToManyRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToManyRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
27
        // First, we will need to determine the foreign key and "other key" for the
28
        // relationship. Once we have determined the keys we will make the query
29
        // instances, as well as the relationship instances we need for these.
30
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
32
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
33
34
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
35
36
        // Now we're ready to create a new query builder for this related model and
37
        // the relationship instances for this relation. This relations will set
38
        // appropriate query constraints then entirely manages the hydrations.
39
        $table = $table ?: Str::plural($name);
40
41
        return $this->newMorphToMany(
0 ignored issues
show
Bug introduced by
The method newMorphToMany() does not exist on Imanghafoori\Relativity\BaseEloquentOverrides. Did you maybe mean morphToMany()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
            $instance->newQuery(), $this, $name, $table,
43
            $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(),
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
            $relatedKey ?: $instance->getKeyName(), $caller, $inverse
45
        );
46
    }
47
48
    public function getRelationValue($key)
49
    {
50
        // If the key already exists in the relationships array, it just means the
51
        // relationship has already been loaded, so we'll just return it out of
52
        // here because there is no need to query within the relations twice.
53
        if ($this->relationLoaded($key)) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
            return $this->relations[$key];
0 ignored issues
show
Bug introduced by
The property relations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        }
56
57
        // If the "attribute" exists as a method on the model, we will just assume
58
        // it is a relationship and will load and return results from the query
59
        // and hydrate the relationship's value on the "relationships" array.
60
        if (method_exists($this, $key) or isset(static::$macros[$key])) {
61
            return $this->getRelationshipFromMethod($key);
0 ignored issues
show
Bug introduced by
It seems like getRelationshipFromMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
        }
63
    }
64
65
}