Completed
Push — master ( 29d79e...8aac64 )
by Iman
09:16
created

Consider::allChecks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Imanghafoori\HeyMan\Switching;
4
5
use Imanghafoori\HeyMan\WatchingStrategies\{EloquentEventsManager, EventManager, RouterEventManager, ViewEventManager};
6
7
/**
8
 * Class Consider
9
 *
10
 * @method null eventChecks(null|callable $closure = null)
11
 * @method null viewChecks(null|callable $closure = null)
12
 * @method null routeChecks(null|callable $closure = null)
13
 * @method null eloquentChecks(null|callable $closure = null)
14 15
 * @method null validationChecks(null|callable $closure = null)
15
 */
16 15
class Consider
17 15
{
18
    private $mode;
19 5
20
    public function __construct($mode)
21 5
    {
22
        $this->mode = $mode;
23
    }
24 4
25
    public function __call($method, $args)
26 4
    {
27
        $m = $this->methods();
28
29 10
        return $this->turn($m[$method], ...$args);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->turn($m[$method], $args) targeting Imanghafoori\HeyMan\Switching\Consider::turn() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$args is expanded, but the parameter $closure of Imanghafoori\HeyMan\Switching\Consider::turn() 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

29
        return $this->turn($m[$method], /** @scrutinizer ignore-type */ ...$args);
Loading history...
30
    }
31 10
32
    public function allChecks()
33
    {
34 4
        foreach($this->methods() as $method => $type){
35
            $this->$method();
36 4
        }
37
    }
38
39 4
    /**
40
     * @param $key
41 4
     * @param callable|null $closure
42
     */
43
    private function turn($key, callable $closure = null)
44 3
    {
45
        $key = 'heyman_ignore_'.$key;
46 3
47 3
        $current = config($key);
48 3
        $this->changeMode($key);
49 3
50 3
        if (is_null($closure)) {
51 3
            return;
52
        }
53
        $result = $closure();
54
        config()->set($key, $current);
55
56
        return $result;
57 15
    }
58
59 15
    /**
60
     * @param $key
61 15
     */
62 15
    private function changeMode($key)
63
    {
64 15
        config()->set($key, [
65 14
            'turnOff' => true,
66
            'turnOn'  => false,
67 1
        ][$this->mode]);
68 1
    }
69
70 1
    /**
71
     * @return array
72
     */
73
    private function methods(): array
74
    {
75
        return [
76 15
            'eventChecks' => EventManager::class,
77
            'viewChecks' => ViewEventManager::class,
78 15
            'routeChecks' => RouterEventManager::class,
79
            'eloquentChecks' => EloquentEventsManager::class,
80
            'validationChecks' => 'validation',
81 15
        ];
82 15
    }
83
}
84