Request::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 4
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Mohammad Shamaseen
5
 * Date: 09/10/18
6
 * Time: 01:01 م.
7
 */
8
9
namespace Shamaseen\Repository\Utility;
10
11
use Illuminate\Foundation\Http\FormRequest;
12
use Illuminate\Support\Str;
13
use ReflectionClass;
14
15
/**
16
 * Class BaseRequests.
17
 */
18
class Request extends FormRequest
19
{
20
    public AbstractRepository $repository;
21
22
    public function __construct(AbstractRepository $repository, array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
23
    {
24
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
25
        $this->repository = $repository;
26
    }
27
28
    /**
29
     * Define all the global rules for this request here.
30
     */
31
    protected array $rules = [
32
    ];
33
34
    public function rules(): array
35
    {
36
        $rules = [] + $this->rules;
37
38
        $method = $this->method();
39
40
        $httpValidatorName = 'Http'.ucfirst(Str::lower($method)).'Rules';
41
42
        if (method_exists($this, $httpValidatorName)) {
43
            $rules += $this->{$httpValidatorName}();
44
        }
45
46
        $route = $this->route();
47
        // make sure that it is accessed by a route and not command or job
48
        if ($route) {
49
            $routeName = $route->action['uses'];
50
            if ($routeName) {
51
                $lastPart = last(explode('@', $routeName));
52
                $validatorName = $lastPart.'Rules';
53
54
                $reflection = new ReflectionClass($this);
55
                // make sure the method is exists in the user defined request
56
                if ($reflection->hasMethod($validatorName) && $reflection->getMethod($validatorName)->class === $reflection->getName()) {
57
                    $rules += $this->{$validatorName}();
58
                }
59
            }
60
        }
61
62
        return $rules;
63
    }
64
65
    /**
66
     * Define all the rules for every get HTTP method on this request.
67
     */
68
    public function getMethodRules(): array
69
    {
70
        return [];
71
    }
72
73
    /**
74
     * Define all the rules for every post HTTP method on this request.
75
     */
76
    public function postMethodRules(): array
77
    {
78
        return [];
79
    }
80
81
    /**
82
     * Define all the rules for every patch HTTP method on this request.
83
     */
84
    public function patchMethodRules(): array
85
    {
86
        return [];
87
    }
88
89
    /**
90
     * Define all the rules for every put HTTP method on this request.
91
     */
92
    public function putMethodRules(): array
93
    {
94
        return [];
95
    }
96
97
    /**
98
     * Define all the rules for every delete HTTP method on this request.
99
     */
100
    public function deleteMethodRules(): array
101
    {
102
        return [];
103
    }
104
}
105