GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FieldWithRelation::beforeValidation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Akibatech\Crud\Traits;
4
5
use Akibatech\Crud\Exceptions\InvalidRelationException;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Fluent;
8
use Illuminate\Validation\Validator;
9
10
/**
11
 * Class FieldWithRelation
12
 *
13
 * @package Akibatech\Crud\Traits
14
 */
15
trait FieldWithRelation
16
{
17
    /**
18
     * @var string $relation
19
     */
20
    protected $relation;
21
22
    /**
23
     * @var array
24
     */
25
    protected $options;
26
27
    /**
28
     * @param   string $relation
29
     * @return  self
30
     */
31
    public function withRelation($relation)
32
    {
33
        $this->relation = $relation;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @param   void
40
     * @return  Relation
41
     */
42
    public function getRelation()
43
    {
44
        if (empty($this->relation))
45
        {
46
            throw new InvalidRelationException("{$this->getIdentifier()} does not contain any relation.");
0 ignored issues
show
Bug introduced by
It seems like getIdentifier() 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...
47
        }
48
49
        $relation = $this->relation;
50
51
        return $this->getFields()->getEntry()->getModel()->$relation();
0 ignored issues
show
Bug introduced by
It seems like getFields() 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...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getOptions()
58
    {
59
        if (is_null($this->options))
60
        {
61
            $related = get_class($this->getRelation()->getRelated());
62
            $options = $related::get()->pluck('name', 'id')->toArray();
63
            
64
            $this->options = $options;
65
        }
66
67
        return $this->options;
68
    }
69
70
    /**
71
     * @param   Validator $validator
72
     * @return  Validator
73
     */
74
    public function beforeValidation(Validator $validator)
75
    {
76
        $related_class = $this->getRelation()->getRelated();
77
        $related_instance = new $related_class;
78
        $related_table = $related_class->getTable();
79
        $related_pk = $related_instance->getKeyName();
80
81
        $validator->mergeRules($this->getIdentifier(), [
0 ignored issues
show
Bug introduced by
It seems like getIdentifier() 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...
82
            "exists:$related_table,$related_pk"
83
        ]);
84
85
        return $validator;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getTableValue()
92
    {
93
        $relation = $this->relation;
94
        return $this->getFields()->getEntry()->getModel()->$relation->name;
0 ignored issues
show
Bug introduced by
It seems like getFields() 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...
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getViewVariables()
101
    {
102
        return [
103
            'options' => $this->getOptions(),
104
        ];
105
    }
106
}
107