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
Pull Request — master (#1079)
by Henrique
03:58
created

Call::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.1406
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Throwable;
17
use Respect\Validation\Exceptions\ValidationException;
18
use Respect\Validation\Validatable;
19
use function call_user_func;
20
use function restore_error_handler;
21
use function set_error_handler;
22
23
/**
24
 * Validates the return of a callable for a given input.
25
 *
26
 * @author Alexandre Gomes Gaigalas <[email protected]>
27
 * @author Emmerson Siqueira <[email protected]>
28
 * @author Henrique Moody <[email protected]>
29
 */
30
final class Call extends AbstractRule
31
{
32
    /**
33
     * @var callable
34
     */
35
    private $callable;
36
37
    /**
38
     * @var Validatable
39
     */
40
    private $rule;
41
42
    /**
43
     * Initializes the rule with the callable to be executed after the input is passed.
44
     *
45
     * @param callable $callable
46
     * @param Validatable $rule
47
     */
48 13
    public function __construct(callable $callable, Validatable $rule)
49
    {
50 13
        $this->callable = $callable;
51 13
        $this->rule = $rule;
52 13
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 8
    public function assert($input): void
58
    {
59 8
        $this->setErrorHandler($input);
60
61
        try {
62 8
            $this->rule->assert(call_user_func($this->callable, $input));
63 7
        } catch (ValidationException $exception) {
64 5
            throw $exception;
65 2
        } catch (Throwable $throwable) {
66 2
            throw $this->reportError($input);
67
        }
68
69 1
        restore_error_handler();
70 1
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 6
    public function check($input): void
76
    {
77 6
        $this->setErrorHandler($input);
78
79
        try {
80 6
            $this->rule->check(call_user_func($this->callable, $input));
81 4
        } catch (ValidationException $exception) {
82 4
            throw $exception;
83
        } catch (Throwable $throwable) {
84
            throw $this->reportError($input);
85
        }
86
87 3
        restore_error_handler();
88 3
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 4
    public function validate($input): bool
94
    {
95
        try {
96 4
            $this->check($input);
97 2
        } catch (ValidationException $exception) {
98 2
            return false;
99
        }
100
101 2
        return true;
102
    }
103
104
    private function setErrorHandler($input): void
105
    {
106 13
        set_error_handler(function () use ($input): void {
107 4
            throw $this->reportError($input);
108 13
        });
109 13
    }
110
}
111