SpyGate::policy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\SpyClasses;
4
5
use Illuminate\Auth\Access\Gate;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
9
10
class SpyGate extends Gate
11
{
12
    public static $definedGatesNum = 0;
13
14
    public function define($ability, $callback)
15
    {
16
        self::$definedGatesNum++;
17
        if (is_string($callback)) {
18
            [$class, $method] = Str::parseCallback($callback, '__invoke');
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
19
20
            try {
21
                $policy = app()->make($class);
22
            } catch (\Exception $e) {
23
                return app(ErrorPrinter::class)->pended[] = ("The $callback callback for Gate, does not refer to a resolvable class, for ability: $ability");
24
            }
25
26
            if (! method_exists($policy, $method)) {
27
                return app(ErrorPrinter::class)->pended[] = ("The $callback callback for Gate, does not refer to a valid method, for ability: $ability");
28
            }
29
        }
30
31
        $t = $this->abilities[$ability] ?? null;
32
        if ($t) {
33
            $callback1 = is_string($callback) ? $callback : 'Closure';
34
            $callback2 = is_string($t) ? $t : 'Closure';
35
            app(ErrorPrinter::class)->pended[] = ("The Gate definition '$ability' is overridden. loser:".$callback1.' Winner: '.$callback2);
36
        }
37
38
        parent::define($ability, $callback);
39
    }
40
41
    public function policy($model, $policy)
42
    {
43
        if (! is_subclass_of($model, Model::class)) {
44
            return app(ErrorPrinter::class)->pended[] = ("The \"$model\" you are trying to define policy for, is not an eloquent model class.");
45
        }
46
    }
47
}
48