Passed
Push — master ( c88fe1...950be2 )
by Iman
12:12 queued 09:25
created

Consider::turn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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

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