Passed
Push — master ( 48a296...673f30 )
by Arnaud
14:17 queued 09:16
created

CollectionOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 22
c 1
b 0
f 1
nc 1
nop 28
dl 0
loc 52
rs 9.568

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Metadata;
6
7
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\ORMDataProcessor;
8
use LAG\AdminBundle\Bridge\Doctrine\ORM\State\ORMDataProvider;
9
use LAG\AdminBundle\Metadata\Filter\FilterInterface;
10
11
abstract class CollectionOperation extends Operation implements CollectionOperationInterface
12
{
13
    public function __construct(
14
        ?string $name = null,
15
        ?string $title = null,
16
        ?string $description = null,
17
        ?string $icon = null,
18
        ?string $template = null,
19
        array $permissions = [],
20
        ?string $controller = null,
21
        ?string $route = null,
22
        array $routeParameters = [],
23
        array $methods = [],
24
        string $path = null,
25
        ?string $targetRoute = null,
26
        array $targetRouteParameters = [],
27
        array $properties = [],
28
        ?string $formType = null,
29
        array $formOptions = [],
30
        string $processor = ORMDataProcessor::class,
31
        string $provider = ORMDataProvider::class,
32
        array $identifiers = ['id'],
33
        ?array $contextualActions = null,
34
        ?array $itemActions = null,
35
        private bool $pagination = true,
36
        private int $itemPerPage = 25,
37
        private string $pageParameter = 'page',
38
        private array $criteria = [],
39
        private array $orderBy = [],
40
        private ?array $filters = null,
41
        private ?string $gridTemplate = '@LAGAdmin/grid/table_grid.html.twig',
42
    ) {
43
        parent::__construct(
44
            $name,
45
            $title,
46
            $description,
47
            $icon,
48
            $template,
49
            $permissions,
50
            $controller,
51
            $route,
52
            $routeParameters,
53
            $methods,
54
            $path,
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type null; however, parameter $path of LAG\AdminBundle\Metadata\Operation::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            /** @scrutinizer ignore-type */ $path,
Loading history...
55
            $targetRoute,
56
            $targetRouteParameters,
57
            $properties,
58
            $formType,
59
            $formOptions,
60
            $processor,
61
            $provider,
62
            $identifiers,
63
            $contextualActions,
64
            $itemActions,
65
        );
66
    }
67
68
    public function hasPagination(): bool
69
    {
70
        return $this->pagination;
71
    }
72
73
    public function setPagination(bool $pagination): self
74
    {
75
        $self = clone $this;
76
        $self->pagination = $pagination;
77
78
        return $self;
79
    }
80
81
    public function getItemPerPage(): int
82
    {
83
        return $this->itemPerPage;
84
    }
85
86
    public function withItemPerPage(int $itemPerPage): self
87
    {
88
        $self = clone $this;
89
        $self->itemPerPage = $itemPerPage;
90
91
        return $self;
92
    }
93
94
    public function getPageParameter(): string
95
    {
96
        return $this->pageParameter;
97
    }
98
99
    public function withPageParameter(string $pageParameter): self
100
    {
101
        $self = clone $this;
102
        $self->pageParameter = $pageParameter;
103
104
        return $self;
105
    }
106
107
    public function getCriteria(): array
108
    {
109
        return $this->criteria;
110
    }
111
112
    public function withCriteria(array $criteria): self
113
    {
114
        $self = clone $this;
115
        $self->criteria = $criteria;
116
117
        return $self;
118
    }
119
120
    public function getOrderBy(): array
121
    {
122
        return $this->orderBy;
123
    }
124
125
    public function withOrderBy(array $orderBy): self
126
    {
127
        $self = clone $this;
128
        $self->orderBy = $orderBy;
129
130
        return $self;
131
    }
132
133
    public function getFilters(): ?array
134
    {
135
        return $this->filters;
136
    }
137
138
    public function getFilter(string $name): ?FilterInterface
139
    {
140
        foreach ($this->filters as $filter) {
141
            if ($filter->getName() === $name) {
142
                return $filter;
143
            }
144
        }
145
146
        return null;
147
    }
148
149
    public function hasFilters(): bool
150
    {
151
        return $this->filters !== null && \count($this->filters) > 0;
152
    }
153
154
    public function withFilters(array $filters): self
155
    {
156
        $self = clone $this;
157
        $self->filters = $filters;
158
159
        return $self;
160
    }
161
162
    public function getGridTemplate(): ?string
163
    {
164
        return $this->gridTemplate;
165
    }
166
167
    public function withGridTemplate(?string $gridTemplate): self
168
    {
169
        $self = clone $this;
170
        $self->gridTemplate = $gridTemplate;
171
172
        return $self;
173
    }
174
}
175