Test Failed
Push — master ( fdb79d...2e4512 )
by Chris
19:35
created

Taxonomy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 62
rs 9.456
c 1
b 0
f 0
cc 1
nc 1
nop 28

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
namespace Leonidas\Library\System\Taxonomy;
4
5
use Leonidas\Contracts\System\Taxonomy\TaxonomyInterface;
6
use Leonidas\Library\System\AbstractSystemModelType;
7
8
class Taxonomy extends AbstractSystemModelType implements TaxonomyInterface
9
{
10
    protected array $objectTypes;
11
12
    protected bool $isShownInTagCloud;
13
14
    protected bool $isShownInQuickEdit;
15
16
    protected bool $showsAdminColumn;
17
18
    /**
19
     * @var bool|callable
20
     */
21
    protected $metaBoxCb;
22
23
    /**
24
     * @var null|callable
25
     */
26
    protected $metaBoxSanitizeCb;
27
28
    /**
29
     * @var null|callable
30
     */
31
    protected $updateCountCallback;
32
33
    /**
34
     * @var null|string|array
35
     */
36
    protected $defaultTerm;
37
38
    protected bool $shouldBeSorted;
39
40
    /**
41
     * @return null|array
42
     */
43
    protected $args;
44
45
    public function __construct(
46
        string $name,
47
        array $objectTypes,
48
        string $pluralLabel,
49
        ?string $singularLabel = null,
50
        string $description = '',
51
        array $labels = [],
52
        bool $isPublic = false,
53
        bool $isHierarchical = false,
54
        ?bool $isPubliclyQueryable = null,
55
        ?bool $isShownInUi = null,
56
        $shownInMenu = null,
57
        ?bool $isShownInNavMenus = null,
58
        array $capabilities = [],
59
        $rewrite = true,
60
        $queryVar = true,
61
        bool $isShownInRest = false,
62
        $restBase = false,
63
        $restNamespace = false,
64
        $restControllerClass = false,
65
        ?bool $isShownInTagCloud = null,
66
        ?bool $isShownInQuickEdit = null,
67
        bool $showsAdminColumn = false,
68
        $metaBoxCb = null,
69
        ?callable $metaBoxSanitizeCb = null,
70
        ?callable $updateCountCallback = null,
71
        $defaultTerm = null,
72
        $shouldBeSorted = null,
73
        array $options = []
74
    ) {
75
        parent::__construct(
76
            $name,
77
            $pluralLabel,
78
            $singularLabel,
0 ignored issues
show
Bug introduced by
It seems like $singularLabel can also be of type null; however, parameter $singularLabel of Leonidas\Library\System\...odelType::__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

78
            /** @scrutinizer ignore-type */ $singularLabel,
Loading history...
79
            $description,
80
            $labels,
81
            $isPublic,
82
            $isHierarchical,
83
            $isPubliclyQueryable,
84
            $isShownInUi,
85
            $shownInMenu,
86
            $isShownInNavMenus,
87
            $capabilities,
88
            $rewrite,
89
            $queryVar,
90
            $isShownInRest,
91
            $restBase,
92
            $restNamespace,
93
            $restControllerClass,
94
            $options
95
        );
96
97
        $this->objectTypes = $objectTypes;
98
        $this->showsAdminColumn = $showsAdminColumn;
99
        $this->metaBoxCb = $metaBoxCb;
100
        $this->metaBoxSanitizeCb = $metaBoxSanitizeCb;
101
        $this->updateCountCallback = $updateCountCallback;
102
        $this->defaultTerm = $defaultTerm;
103
        $this->shouldBeSorted = $shouldBeSorted;
104
105
        $this->isShownInTagCloud = $isShownInTagCloud ?? $this->isShownInUi;
106
        $this->isShownInQuickEdit = $isShownInQuickEdit ?? $this->isShownInUi;
107
    }
108
109
    public function getObjectTypes(): array
110
    {
111
        return $this->objectTypes;
112
    }
113
114
    public function isShownInTagCloud(): bool
115
    {
116
        return $this->isShownInTagCloud;
117
    }
118
119
    public function isShownInQuickEdit(): bool
120
    {
121
        return $this->isShownInQuickEdit;
122
    }
123
124
    public function showsAdminColumn(): bool
125
    {
126
        return $this->showsAdminColumn;
127
    }
128
129
    public function getMetaBoxCb()
130
    {
131
        return $this->metaBoxCb;
132
    }
133
134
    public function getMetaBoxSanitizeCb(): ?callable
135
    {
136
        return $this->metaBoxSanitizeCb;
137
    }
138
139
    public function getUpdateCountCallback(): ?callable
140
    {
141
        return $this->updateCountCallback;
142
    }
143
144
    public function getDefaultTerm()
145
    {
146
        return $this->defaultTerm;
147
    }
148
149
    public function shouldBeSorted(): bool
150
    {
151
        return $this->shouldBeSorted;
152
    }
153
154
    public function getArgs(): array
155
    {
156
        return $this->args;
157
    }
158
159
    protected function defaultLabels(): array
160
    {
161
        $singleUpper = $this->getSingularLabel();
162
        $pluralUpper = $this->getPluralLabel();
163
        $pluralLower = strtolower($pluralUpper);
164
165
        return [
166
            'name' => $pluralUpper,
167
            'singular_name' => $singleUpper,
168
            'search_items' => "Search {$pluralUpper}",
169
            'popular_items' => "Popular {$pluralUpper}",
170
            'all_items' => "All {$pluralUpper}",
171
            'parent_item' => "Parent {$singleUpper}",
172
            'parent_item_colon' => "Parent {$singleUpper}:",
173
            'edit_item' => "Edit {$singleUpper}",
174
            'view_item' => "View {$singleUpper}",
175
            'update_item' => "Update {$pluralUpper}",
176
            'add_new_item' => "Add New {$singleUpper}",
177
            'new_item_name' => "New {$singleUpper} Name",
178
            'separate_items_with_commas' => "Separate {$pluralLower} with commas",
179
            'add_or_remove_items' => "Add or remove {$pluralLower}",
180
            'choose_from_most_used' => "Choose from the most used {$pluralLower}",
181
            'not_found' => "No {$pluralLower} found",
182
            'no_terms' => "No {$pluralLower}",
183
            'items_list_navigation' => "{$pluralUpper} list navigation",
184
            'items_list' => "{$pluralUpper} list",
185
            'back_to_items' => "&larr; Back to {$pluralUpper}",
186
        ];
187
    }
188
}
189