Passed
Push — master ( f53786...68dd01 )
by Iman
10:13
created

Consider::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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
    const methods = [
22
            'eventChecks'      => EventManager::class,
23 15
            'viewChecks'       => ViewEventManager::class,
24
            'routeChecks'      => RouterEventManager::class,
25 15
            'eloquentChecks'   => EloquentEventsManager::class,
26 15
            'validationChecks' => 'validation',
27
    ];
28 15
29
    private $mode;
30 15
31
    public function __construct($mode)
32 15
    {
33
        $this->mode = $mode;
34
    }
35 3
36
    public function __call($method, $args)
37 3
    {
38 3
        return $this->turn(self::methods[$method], ...$args);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->turn(self::methods[$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

38
        return $this->turn(self::methods[$method], /** @scrutinizer ignore-type */ ...$args);
Loading history...
39
    }
40 3
41
    public function allChecks()
42
    {
43
        foreach (self::methods as $method => $type) {
44
            $this->$method();
45
        }
46 15
    }
47
48 15
    /**
49
     * @param $key
50 15
     * @param callable|null $closure
51 15
     */
52
    private function turn($key, callable $closure = null)
53 15
    {
54 14
        $key = 'heyman_ignore_'.$key;
55
56 1
        $current = config($key);
57 1
        $this->changeMode($key);
58
59 1
        if (is_null($closure)) {
60
            return;
61
        }
62
        $result = $closure();
63
        config()->set($key, $current);
64
65 15
        return $result;
66
    }
67 15
68
    /**
69
     * @param $key
70 15
     */
71 15
    private function changeMode($key)
72
    {
73
        config()->set($key, [
74
            'turnOff' => true,
75
            'turnOn'  => false,
76 15
        ][$this->mode]);
77
    }
78
}
79