Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — 2.0 ( e6a123 )
by Henrique
05:00
created

KeyValue   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 86
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A overwriteExceptionParams() 0 15 3
A __construct() 0 6 1
A getRule() 0 19 4
A assert() 0 12 2
A check() 0 12 2
A validate() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ComponentException;
15
use Respect\Validation\Exceptions\ValidationException;
16
use Respect\Validation\Validator;
17
18
class KeyValue extends AbstractRule
19
{
20
    public $comparedKey;
21
    public $ruleName;
22
    public $baseKey;
23
24
    public function __construct($comparedKey, $ruleName, $baseKey)
25
    {
26
        $this->comparedKey = $comparedKey;
27
        $this->ruleName = $ruleName;
28
        $this->baseKey = $baseKey;
29
    }
30
31
    private function getRule($input)
32
    {
33
        if (!isset($input[$this->comparedKey])) {
34
            throw $this->reportError($this->comparedKey);
35
        }
36
37
        if (!isset($input[$this->baseKey])) {
38
            throw $this->reportError($this->baseKey);
39
        }
40
41
        try {
42
            $rule = Validator::__callStatic($this->ruleName, [$input[$this->baseKey]]);
43
            $rule->setName($this->comparedKey);
0 ignored issues
show
Documentation Bug introduced by
The method setName does not exist on object<Respect\Validation\Validator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
        } catch (ComponentException $exception) {
45
            throw $this->reportError($input, ['component' => true]);
46
        }
47
48
        return $rule;
49
    }
50
51
    private function overwriteExceptionParams(ValidationException $exception)
52
    {
53
        $params = [];
54
        foreach ($exception->getParams() as $key => $value) {
55
            if (in_array($key, ['template', 'translator'])) {
56
                continue;
57
            }
58
59
            $params[$key] = $this->baseKey;
60
        }
61
62
        $exception->configure($this->comparedKey, $params);
63
64
        return $exception;
65
    }
66
67
    public function assert($input)
68
    {
69
        $rule = $this->getRule($input);
70
71
        try {
72
            $rule->assert($input[$this->comparedKey]);
0 ignored issues
show
Documentation Bug introduced by
The method assert does not exist on object<Respect\Validation\Validator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
        } catch (ValidationException $exception) {
74
            throw $this->overwriteExceptionParams($exception);
75
        }
76
77
        return true;
78
    }
79
80
    public function check($input)
81
    {
82
        $rule = $this->getRule($input);
83
84
        try {
85
            $rule->check($input[$this->comparedKey]);
86
        } catch (ValidationException $exception) {
87
            throw $this->overwriteExceptionParams($exception);
88
        }
89
90
        return true;
91
    }
92
93
    public function validate($input)
94
    {
95
        try {
96
            $rule = $this->getRule($input);
97
        } catch (ValidationException $e) {
98
            return false;
99
        }
100
101
        return $rule->validate($input[$this->comparedKey]);
102
    }
103
}
104