Passed
Push — master ( 14cd9e...aa754e )
by Iman
01:50
created

BaseEloquentOverrides::morphedByMany()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 6
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 8
crap 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
use Illuminate\Support\Str;
6
7
trait BaseEloquentOverrides
8
{
9
    /**
10
     * Define a polymorphic many-to-many relationship.
11
     *
12
     * @param  string  $related
13
     * @param  string  $name
14
     * @param  string  $table
15
     * @param  string  $foreignPivotKey
16
     * @param  string  $relatedPivotKey
17
     * @param  string  $parentKey
18
     * @param  string  $relatedKey
19
     * @param  bool  $inverse
20
     * @param  string  $caller
21
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
22
     */
23 1
    public function morphToMany($related, $name, $table = null, $foreignPivotKey = null,
24
        $relatedPivotKey = null, $parentKey = null,
25
        $relatedKey = null, $inverse = false, $caller = null)
26
    {
27 1
        $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...
28
29
        // First, we will need to determine the foreign key and "other key" for the
30
        // relationship. Once we have determined the keys we will make the query
31
        // instances, as well as the relationship instances we need for these.
32 1
        $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...
33
34 1
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
35
36 1
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
37
38
        // Now we're ready to create a new query builder for this related model and
39
        // the relationship instances for this relation. This relations will set
40
        // appropriate query constraints then entirely manages the hydrations.
41 1
        $table = $table ?: Str::plural($name);
42
43 1
        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...
44 1
            $instance->newQuery(), $this, $name, $table,
45 1
            $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...
46 1
            $relatedKey ?: $instance->getKeyName(), $caller, $inverse
47
        );
48
    }
49
50 5
    public function getRelationValue($key)
51
    {
52
        // If the key already exists in the relationships array, it just means the
53
        // relationship has already been loaded, so we'll just return it out of
54
        // here because there is no need to query within the relations twice.
55 5
        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...
56
            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...
57
        }
58
59
        // If the "attribute" exists as a method on the model, we will just assume
60
        // it is a relationship and will load and return results from the query
61
        // and hydrate the relationship's value on the "relationships" array.
62 5
        if (method_exists($this, $key) or isset(static::$macros[$key])) {
63 5
            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...
64
        }
65
    }
66
67
    /**
68
     * Define a polymorphic, inverse many-to-many relationship.
69
     *
70
     * @param  string  $related
71
     * @param  string  $name
72
     * @param  string  $table
73
     * @param  string  $foreignPivotKey
74
     * @param  string  $relatedPivotKey
75
     * @param  string  $parentKey
76
     * @param  string  $relatedKey
77
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
78
     */
79
    public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null,
80
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $caller = null)
81
    {
82
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() 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...
83
84
        // For the inverse of the polymorphic many-to-many relations, we will change
85
        // the way we determine the foreign and other keys, as it is the opposite
86
        // of the morph-to-many method since we're figuring out these inverses.
87
        $relatedPivotKey = $relatedPivotKey ?: $name.'_id';
88
89
        return $this->morphToMany(
90
            $related, $name, $table, $foreignPivotKey,
91
            $relatedPivotKey, $parentKey, $relatedKey, true, $caller
92
        );
93
    }
94
}