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.
Passed
Pull Request — new (#864)
by Dave
07:45
created

HasUniqueValidation::initializeUniqueValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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
     * Columns, that should be unique.
13
     *
14
     * @param array $columns
15
     * @param string|null $message
16
     *
17
     * @return static
18
     */
19
    public function unique(array $columns, $message = null)
20
    {
21
        $this->initializeUniqueValidator($message);
22
        $this->unique = $columns;
23
24
        return $this;
25
    }
26
27
    protected function initializeUniqueValidator($message = null)
28
    {
29
        Validator::extendImplicit('unique_related', function () {
30
            return false;
31
        }, $message);
32
    }
33
34
    public function getValidationRules()
35
    {
36
        $this->addUniqueValidation();
37
38
        return parent::getValidationRules();
39
    }
40
41
    protected function addUniqueValidation()
42
    {
43
        if (count((array) $this->unique) > 0) {
44
            $pairs = [];
45
            $parameters = array_unique(array_merge([
46
                $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

46
                $mainKey = $this->/** @scrutinizer ignore-call */ getModel()->getForeignKey(),
Loading history...
47
            ], $this->unique));
48
49
            $errorColumns = array_filter($parameters, function ($param) use ($mainKey) {
50
                return $param !== $mainKey;
51
            });
52
53
            $relations = (array) request($this->relationName);
54
55
            foreach ($relations as $index => $relation) {
56
                $relation[$mainKey] = $this->getModel()->getKey();
57
                $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

57
                /** @scrutinizer ignore-call */ 
58
                $key = $this->getCompositeKey($relation, (array) $parameters);
Loading history...
58
59
                if (array_key_exists($key, $pairs)) {
60
                    foreach ($errorColumns as $column) {
61
                        $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...
62
                    }
63
                } else {
64
                    $pairs[$key] = true;
65
                }
66
            }
67
        }
68
    }
69
}
70