CustomRule::__get()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 1
1
<?php
2
3
namespace PerfectOblivion\Valid;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Contracts\Validation\Validator;
8
use PerfectOblivion\Valid\Contracts\ValidationService\ValidationService;
9
10
abstract class CustomRule implements Rule
11
{
12
    /**
13
     * The current validator instance.
14
     *
15
     * @var \Illuminate\Contracts\Validation\Validator
16
     */
17
    protected static $validator;
18
19
    /**
20
     * The current FormRequest instance.
21
     *
22
     * @var \Illuminate\Http\Request
23
     */
24
    protected static $request;
25
26
    /**
27
     * The current ValidationService instance.
28
     *
29
     * @var \PerfectOblivion\Valid\ValidationService\ValidationService
30
     */
31
    protected static $service;
32
33
    /**
34
     * Set the validator property.
35
     *
36
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
37
     */
38
    public static function validator(?Validator $validator = null)
39
    {
40
        if (! isset(static::$validator) && $validator) {
41
            static::$validator = $validator;
42
        }
43
44
        return static::$validator;
45
    }
46
47
    /**
48
     * Set the request property.
49
     *
50
     * @param  \Illuminate\Http\Request  $request
51
     */
52
    public static function request(?Request $request = null)
53
    {
54
        if (! isset(static::$request) && $request) {
55
            static::$request = $request;
56
        }
57
58
        return static::$request;
59
    }
60
61
    /**
62
     * Set the Validation Service property.
63
     *
64
     * @param  \PerfectOblivion\Valid\Contracts\ValidationService\ValidationService  $service
65
     */
66
    public static function service(? ValidationService $service = null)
67
    {
68
        if (! isset(static::$service) && $service) {
69
            static::$service = $service;
0 ignored issues
show
Documentation Bug introduced by
$service is of type object<PerfectOblivion\V...vice\ValidationService>, but the property $service was declared to be of type object<PerfectOblivion\V...vice\ValidationService>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
70
        }
71
72
        return static::$service;
73
    }
74
75
    /**
76
     * Determine if the validation rule passes.
77
     *
78
     * @param  string  $attribute
79
     * @param  mixed  $value
80
     *
81
     * @return bool
82
     */
83
    abstract public function passes($attribute, $value);
84
85
    /**
86
     * Get the validation error message.
87
     *
88
     * @return string
89
     */
90
    abstract public function message();
91
92
    /**
93
     * Handle property accessibility for CustomRule.
94
     *
95
     * @param  string  $property
96
     *
97
     * @return mixed
98
     */
99
    public function __get($property)
100
    {
101
        if ('validator' === $property || 'request' === $property || 'service' === $property) {
102
            return static::$property ?? static::$property();
103
        }
104
105
        return $this->$property;
106
    }
107
}
108