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.
Completed
Pull Request — development (#846)
by
unknown
08:14
created

HasUniqueValidation::addUniqueValidation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Related;
4
5
use Validator;
6
7
trait HasUniqueValidation
8
{
9
    protected $unique;
10
11
    /**
12
     * @param array $columns
13
     * @param string|null $message
14
     *
15
     * @return static
16
     */
17
    public function unique(array $columns, $message = null)
18
    {
19
        $this->initializeUniqueValidator($message);
20
        $this->unique = $columns;
21
22
        return $this;
23
    }
24
25
    protected function initializeUniqueValidator($message = null)
26
    {
27
        Validator::extendImplicit('unique_related', function () {
28
            return false;
29
        }, $message);
30
    }
31
32
    public function getValidationRules()
33
    {
34
        $this->addUniqueValidation();
35
36
        return parent::getValidationRules();
37
    }
38
39
    protected function addUniqueValidation()
40
    {
41
        if (count((array) $this->unique) > 0) {
42
            $pairs = [];
43
            $parameters = array_unique(array_merge([
44
                $mainKey = $this->getModel()->getForeignKey(),
0 ignored issues
show
Bug introduced by
It seems like getModel() 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

44
                $mainKey = $this->/** @scrutinizer ignore-call */ getModel()->getForeignKey(),
Loading history...
45
            ], $this->unique));
46
47
            $errorColumns = array_filter($parameters, function ($param) use ($mainKey) {
48
                return $param !== $mainKey;
49
            });
50
51
            $relations = (array) request($this->relationName);
52
53
            foreach ($relations as $index => $relation) {
54
                $relation[$mainKey] = $this->getModel()->getKey();
55
                $key = $this->getCompositeKey($relation, (array) $parameters);
0 ignored issues
show
Bug introduced by
It seems like getCompositeKey() 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

55
                /** @scrutinizer ignore-call */ 
56
                $key = $this->getCompositeKey($relation, (array) $parameters);
Loading history...
56
57
                if (array_key_exists($key, $pairs)) {
58
                    foreach ($errorColumns as $column) {
59
                        $this->validationRules[$this->relationName.'.'.$index.'.'.$column] = 'unique_related';
0 ignored issues
show
Bug Best Practice introduced by
The property validationRules does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
                    }
61
                } else {
62
                    $pairs[$key] = true;
63
                }
64
            }
65
        }
66
    }
67
}
68