These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\ValidationRules\Rules; |
||
4 | |||
5 | use Illuminate\Support\Facades\Auth; |
||
6 | use Illuminate\Contracts\Validation\Rule; |
||
7 | |||
8 | class Authorized implements Rule |
||
9 | { |
||
10 | /** @var string */ |
||
11 | protected $ability; |
||
12 | |||
13 | /** @var array */ |
||
14 | protected $arguments; |
||
15 | |||
16 | 24 | /** @var string */ |
|
17 | protected $className; |
||
18 | 24 | ||
19 | /** @var string */ |
||
20 | 24 | protected $attribute; |
|
21 | 24 | ||
22 | public function __construct(string $ability, string $className) |
||
23 | 24 | { |
|
24 | $this->ability = $ability; |
||
25 | 24 | ||
26 | 18 | $this->className = $className; |
|
27 | } |
||
28 | |||
29 | 6 | public function passes($attribute, $value): bool |
|
30 | { |
||
31 | $this->attribute = $attribute; |
||
32 | |||
33 | 6 | if (! $user = Auth::user()) { |
|
34 | return false; |
||
35 | } |
||
36 | |||
37 | if (! $model = $this->className::find($value)) { |
||
0 ignored issues
–
show
|
|||
38 | return false; |
||
39 | } |
||
40 | |||
41 | return $user->can($this->ability, $model); |
||
42 | } |
||
43 | |||
44 | public function message(): string |
||
45 | { |
||
46 | $classBasename = class_basename($this->className); |
||
47 | |||
48 | return __('validation.authorized', [ |
||
49 | 'attribute' => $this->attribute, |
||
50 | 'ability' => $this->ability, |
||
51 | 'className' => $classBasename, |
||
52 | ]); |
||
53 | } |
||
54 | } |
||
55 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.