Completed
Push — master ( 20a6e3...887da8 )
by Abdelrahman
04:09 queued 02:12
created

AdminAttributesFormRequest::rules()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.4444
c 0
b 0
f 0
cc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Rinvex\Support\Traits\Escaper;
8
use Illuminate\Foundation\Http\FormRequest;
9
10
class AdminAttributesFormRequest extends FormRequest
11
{
12
    use Escaper;
13
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize(): bool
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * Configure the validator instance.
26
     *
27
     * @param \Illuminate\Validation\Validator $validator
28
     *
29
     * @return void
30
     */
31
    public function withValidator($validator): void
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        // Sanitize input data before submission
34
        $this->replace($this->escape($this->all()));
35
    }
36
37
    /**
38
     * Get the validation rules that apply to the request.
39
     *
40
     * @return array
41
     */
42
    public function rules(): array
43
    {
44
        $admin = $this->route('admin') ?? app('cortex.auth.admin');
45
46
        // Attach attribute rules
47
        $admin->getEntityAttributes()->each(function ($attribute, $attributeName) use (&$rules) {
48
            switch ($attribute->type) {
49
                case 'datetime':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
50
                    $type = 'date';
51
                    break;
52
                case 'text':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
                case 'check':
54
                case 'select':
55
                case 'varchar':
56
                    $type = 'string';
57
                    break;
58
                default:
59
                    $type = $attribute->type;
60
                    break;
61
            }
62
63
            $rule = ($attribute->is_required ? 'required|' : 'nullable|').$type;
64
            $rules[$attributeName.($attribute->is_collection ? '.*' : '')] = $rule;
65
        });
66
67
        return $rules ?? [];
68
    }
69
}
70