Test Failed
Push — master ( 788073...933e0a )
by Iman
02:31
created

Validator::otherwise()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 9.8666
cc 3
nc 1
nop 0
crap 3
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Imanghafoori\HeyMan\Core\Reaction;
6
use Illuminate\Contracts\Validation\Factory;
7
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
8
9
final class Validator
10
{
11 8
    private $validationData;
12
13 8
    private $modifier;
14 8
15
    public function __construct(array $validationData)
16 1
    {
17
        $this->validationData = $validationData;
18 1
    }
19 1
20
    public function otherwise()
21 8
    {
22
        $rules = $this->validationData;
23 8
        $modifier = $this->modifier ?: function ($args) {
24 8
            return $args;
25 4
        };
26 8
        $validator = function () use ($modifier, $rules) {
27 8
            if (is_callable($rules[0])) {
28 8
                $rules[0] = call_user_func($rules[0]);
29 8
            }
30 8
31 8
            $newData = app()->call($modifier, [request()->all()]);
32
            $validator = resolve(Factory::class)->make($newData, ...$rules);
0 ignored issues
show
Bug introduced by
$rules is expanded, but the parameter $rules of Illuminate\Validation\Factory::make() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            $validator = resolve(Factory::class)->make($newData, /** @scrutinizer ignore-type */ ...$rules);
Loading history...
Bug introduced by
It seems like $newData can also be of type callable; however, parameter $data of Illuminate\Validation\Factory::make() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            $validator = resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $newData, ...$rules);
Loading history...
33
34
            return ! $validator->fails();
35
        };
36
37
        $result = resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
38
39
        resolve('heyman.chain')->set('condition', $result);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        resolve('heyman.chain')->/** @scrutinizer ignore-call */ set('condition', $result);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
41
        return resolve(Reaction::class);
42
    }
43
44
    public function beforeValidationModifyData($callable)
45
    {
46
        $this->modifier = $callable;
47
    }
48
49
    public function __destruct()
50
    {
51
        $data = $this->validationData;
52
        $modifier = $this->modifier ?: function ($args) {
53
            return $args;
54
        };
55
        $chain = resolve('heyman.chain');
56
        $condition = $chain->get('condition');
57
        if (! $condition) {
58
            $condition = resolve(ResponderFactory::class)->validatorCallback($modifier, $data);
59
            $chain->set('condition', $condition);
60
        }
61
        resolve('heyman.chains')->commitChain();
0 ignored issues
show
Bug introduced by
The method commitChain() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        resolve('heyman.chains')->/** @scrutinizer ignore-call */ commitChain();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
}
64