RelationParameters::toArray()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 58
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 52
c 0
b 0
f 0
nc 9
nop 0
dl 0
loc 58
ccs 53
cts 53
cp 1
crap 5
rs 8.7361

How to fix   Long Method   

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\ConfigStorage;
6
7
use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
8
use PhpMyAdmin\ConfigStorage\Features\BrowserTransformationFeature;
9
use PhpMyAdmin\ConfigStorage\Features\CentralColumnsFeature;
10
use PhpMyAdmin\ConfigStorage\Features\ColumnCommentsFeature;
11
use PhpMyAdmin\ConfigStorage\Features\ConfigurableMenusFeature;
12
use PhpMyAdmin\ConfigStorage\Features\DatabaseDesignerSettingsFeature;
13
use PhpMyAdmin\ConfigStorage\Features\DisplayFeature;
14
use PhpMyAdmin\ConfigStorage\Features\ExportTemplatesFeature;
15
use PhpMyAdmin\ConfigStorage\Features\FavoriteTablesFeature;
16
use PhpMyAdmin\ConfigStorage\Features\NavigationItemsHidingFeature;
17
use PhpMyAdmin\ConfigStorage\Features\PdfFeature;
18
use PhpMyAdmin\ConfigStorage\Features\RecentlyUsedTablesFeature;
19
use PhpMyAdmin\ConfigStorage\Features\RelationFeature;
20
use PhpMyAdmin\ConfigStorage\Features\SavedQueryByExampleSearchesFeature;
21
use PhpMyAdmin\ConfigStorage\Features\SqlHistoryFeature;
22
use PhpMyAdmin\ConfigStorage\Features\TrackingFeature;
23
use PhpMyAdmin\ConfigStorage\Features\UiPreferencesFeature;
24
use PhpMyAdmin\ConfigStorage\Features\UserPreferencesFeature;
25
use PhpMyAdmin\Identifiers\DatabaseName;
0 ignored issues
show
Bug introduced by
The type PhpMyAdmin\Identifiers\DatabaseName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use PhpMyAdmin\Identifiers\InvalidDatabaseName;
27
use PhpMyAdmin\Identifiers\TableName;
0 ignored issues
show
Bug introduced by
The type PhpMyAdmin\Identifiers\TableName was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use PhpMyAdmin\Version;
29
30
use function is_string;
31
32
/** @psalm-immutable */
33
final class RelationParameters
34
{
35
    /** @param non-empty-string|null $user */
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
36 48
    public function __construct(
37
        public readonly string|null $user,
38
        public readonly DatabaseName|null $db = null,
39
        public readonly BookmarkFeature|null $bookmarkFeature = null,
40
        public readonly BrowserTransformationFeature|null $browserTransformationFeature = null,
41
        public readonly CentralColumnsFeature|null $centralColumnsFeature = null,
42
        public readonly ColumnCommentsFeature|null $columnCommentsFeature = null,
43
        public readonly ConfigurableMenusFeature|null $configurableMenusFeature = null,
44
        public readonly DatabaseDesignerSettingsFeature|null $databaseDesignerSettingsFeature = null,
45
        public readonly DisplayFeature|null $displayFeature = null,
46
        public readonly ExportTemplatesFeature|null $exportTemplatesFeature = null,
47
        public readonly FavoriteTablesFeature|null $favoriteTablesFeature = null,
48
        public readonly NavigationItemsHidingFeature|null $navigationItemsHidingFeature = null,
49
        public readonly PdfFeature|null $pdfFeature = null,
50
        public readonly RecentlyUsedTablesFeature|null $recentlyUsedTablesFeature = null,
51
        public readonly RelationFeature|null $relationFeature = null,
52
        public readonly SavedQueryByExampleSearchesFeature|null $savedQueryByExampleSearchesFeature = null,
53
        public readonly SqlHistoryFeature|null $sqlHistoryFeature = null,
54
        public readonly TrackingFeature|null $trackingFeature = null,
55
        public readonly UiPreferencesFeature|null $uiPreferencesFeature = null,
56
        public readonly UserPreferencesFeature|null $userPreferencesFeature = null,
57
    ) {
58 48
    }
59
60
    /** @param mixed[] $params */
61 48
    public static function fromArray(array $params): self
62
    {
63 48
        $user = null;
64 48
        if (isset($params['user']) && is_string($params['user']) && $params['user'] !== '') {
65 8
            $user = $params['user'];
66
        }
67
68
        try {
69 48
            $db = DatabaseName::from($params['db'] ?? null);
70 20
        } catch (InvalidDatabaseName) {
71 20
            return new self($user, null);
72
        }
73
74 32
        $bookmarkFeature = null;
75 32
        if (isset($params['bookmarkwork'], $params['bookmark']) && $params['bookmarkwork']) {
76 16
            $bookmark = TableName::tryFrom($params['bookmark']);
77 16
            if ($bookmark !== null) {
78 12
                $bookmarkFeature = new BookmarkFeature($db, $bookmark);
79
            }
80
        }
81
82 32
        $columnInfo = TableName::tryFrom($params['column_info'] ?? null);
83 32
        $browserTransformationFeature = null;
84 32
        if (isset($params['mimework']) && $params['mimework'] && $columnInfo !== null) {
85 20
            $browserTransformationFeature = new BrowserTransformationFeature($db, $columnInfo);
86
        }
87
88 32
        $columnCommentsFeature = null;
89 32
        if (isset($params['commwork']) && $params['commwork'] && $columnInfo !== null) {
90 16
            $columnCommentsFeature = new ColumnCommentsFeature($db, $columnInfo);
91
        }
92
93 32
        $centralColumnsFeature = null;
94 32
        if (isset($params['centralcolumnswork'], $params['central_columns']) && $params['centralcolumnswork']) {
95 12
            $centralColumns = TableName::tryFrom($params['central_columns']);
96 12
            if ($centralColumns !== null) {
97 12
                $centralColumnsFeature = new CentralColumnsFeature($db, $centralColumns);
98
            }
99
        }
100
101 32
        $configurableMenusFeature = null;
102 32
        if (isset($params['menuswork'], $params['usergroups'], $params['users']) && $params['menuswork']) {
103 16
            $userGroups = TableName::tryFrom($params['usergroups']);
104 16
            $users = TableName::tryFrom($params['users']);
105 16
            if ($userGroups !== null && $users !== null) {
106 12
                $configurableMenusFeature = new ConfigurableMenusFeature($db, $userGroups, $users);
107
            }
108
        }
109
110 32
        $databaseDesignerSettingsFeature = null;
111 32
        if (isset($params['designersettingswork'], $params['designer_settings']) && $params['designersettingswork']) {
112 12
            $designerSettings = TableName::tryFrom($params['designer_settings']);
113 12
            if ($designerSettings !== null) {
114 12
                $databaseDesignerSettingsFeature = new DatabaseDesignerSettingsFeature($db, $designerSettings);
115
            }
116
        }
117
118 32
        $relation = TableName::tryFrom($params['relation'] ?? null);
119 32
        $displayFeature = null;
120 32
        if (isset($params['displaywork'], $params['table_info']) && $params['displaywork'] && $relation !== null) {
121 24
            $tableInfo = TableName::tryFrom($params['table_info']);
122 24
            if ($tableInfo !== null) {
123 20
                $displayFeature = new DisplayFeature($db, $relation, $tableInfo);
124
            }
125
        }
126
127 32
        $exportTemplatesFeature = null;
128 32
        if (isset($params['exporttemplateswork'], $params['export_templates']) && $params['exporttemplateswork']) {
129 12
            $exportTemplates = TableName::tryFrom($params['export_templates']);
130 12
            if ($exportTemplates !== null) {
131 12
                $exportTemplatesFeature = new ExportTemplatesFeature($db, $exportTemplates);
132
            }
133
        }
134
135 32
        $favoriteTablesFeature = null;
136 32
        if (isset($params['favoritework'], $params['favorite']) && $params['favoritework']) {
137 12
            $favorite = TableName::tryFrom($params['favorite']);
138 12
            if ($favorite !== null) {
139 12
                $favoriteTablesFeature = new FavoriteTablesFeature($db, $favorite);
140
            }
141
        }
142
143 32
        $navigationItemsHidingFeature = null;
144 32
        if (isset($params['navwork'], $params['navigationhiding']) && $params['navwork']) {
145 12
            $navigationHiding = TableName::tryFrom($params['navigationhiding']);
146 12
            if ($navigationHiding !== null) {
147 12
                $navigationItemsHidingFeature = new NavigationItemsHidingFeature($db, $navigationHiding);
148
            }
149
        }
150
151 32
        $pdfFeature = null;
152 32
        if (isset($params['pdfwork'], $params['pdf_pages'], $params['table_coords']) && $params['pdfwork']) {
153 16
            $pdfPages = TableName::tryFrom($params['pdf_pages']);
154 16
            $tableCoords = TableName::tryFrom($params['table_coords']);
155 16
            if ($pdfPages !== null && $tableCoords !== null) {
156 12
                $pdfFeature = new PdfFeature($db, $pdfPages, $tableCoords);
157
            }
158
        }
159
160 32
        $recentlyUsedTablesFeature = null;
161 32
        if (isset($params['recentwork'], $params['recent']) && $params['recentwork']) {
162 12
            $recent = TableName::tryFrom($params['recent']);
163 12
            if ($recent !== null) {
164 12
                $recentlyUsedTablesFeature = new RecentlyUsedTablesFeature($db, $recent);
165
            }
166
        }
167
168 32
        $relationFeature = null;
169 32
        if (isset($params['relwork']) && $params['relwork'] && $relation !== null) {
170 16
            $relationFeature = new RelationFeature($db, $relation);
171
        }
172
173 32
        $savedQueryByExampleSearchesFeature = null;
174 32
        if (isset($params['savedsearcheswork'], $params['savedsearches']) && $params['savedsearcheswork']) {
175 12
            $savedSearches = TableName::tryFrom($params['savedsearches']);
176 12
            if ($savedSearches !== null) {
177 12
                $savedQueryByExampleSearchesFeature = new SavedQueryByExampleSearchesFeature($db, $savedSearches);
178
            }
179
        }
180
181 32
        $sqlHistoryFeature = null;
182 32
        if (isset($params['historywork'], $params['history']) && $params['historywork']) {
183 12
            $history = TableName::tryFrom($params['history']);
184 12
            if ($history !== null) {
185 12
                $sqlHistoryFeature = new SqlHistoryFeature($db, $history);
186
            }
187
        }
188
189 32
        $trackingFeature = null;
190 32
        if (isset($params['trackingwork'], $params['tracking']) && $params['trackingwork']) {
191 12
            $tracking = TableName::tryFrom($params['tracking']);
192 12
            if ($tracking !== null) {
193 12
                $trackingFeature = new TrackingFeature($db, $tracking);
194
            }
195
        }
196
197 32
        $uiPreferencesFeature = null;
198 32
        if (isset($params['uiprefswork'], $params['table_uiprefs']) && $params['uiprefswork']) {
199 12
            $tableUiPrefs = TableName::tryFrom($params['table_uiprefs']);
200 12
            if ($tableUiPrefs !== null) {
201 12
                $uiPreferencesFeature = new UiPreferencesFeature($db, $tableUiPrefs);
202
            }
203
        }
204
205 32
        $userPreferencesFeature = null;
206 32
        if (isset($params['userconfigwork'], $params['userconfig']) && $params['userconfigwork']) {
207 12
            $userConfig = TableName::tryFrom($params['userconfig']);
208 12
            if ($userConfig !== null) {
209 12
                $userPreferencesFeature = new UserPreferencesFeature($db, $userConfig);
210
            }
211
        }
212
213 32
        return new self(
214 32
            $user,
215 32
            $db,
216 32
            $bookmarkFeature,
217 32
            $browserTransformationFeature,
218 32
            $centralColumnsFeature,
219 32
            $columnCommentsFeature,
220 32
            $configurableMenusFeature,
221 32
            $databaseDesignerSettingsFeature,
222 32
            $displayFeature,
223 32
            $exportTemplatesFeature,
224 32
            $favoriteTablesFeature,
225 32
            $navigationItemsHidingFeature,
226 32
            $pdfFeature,
227 32
            $recentlyUsedTablesFeature,
228 32
            $relationFeature,
229 32
            $savedQueryByExampleSearchesFeature,
230 32
            $sqlHistoryFeature,
231 32
            $trackingFeature,
232 32
            $uiPreferencesFeature,
233 32
            $userPreferencesFeature,
234 32
        );
235
    }
236
237
    /**
238
     * @return array<string, bool|string|null>
239
     * @psalm-return array{
240
     *   version: string,
241
     *   user: (string|null),
242
     *   db: (string|null),
243
     *   bookmark: (string|null),
244
     *   central_columns: (string|null),
245
     *   column_info: (string|null),
246
     *   designer_settings: (string|null),
247
     *   export_templates: (string|null),
248
     *   favorite: (string|null),
249
     *   history: (string|null),
250
     *   navigationhiding: (string|null),
251
     *   pdf_pages: (string|null),
252
     *   recent: (string|null),
253
     *   relation: (string|null),
254
     *   savedsearches: (string|null),
255
     *   table_coords: (string|null),
256
     *   table_info: (string|null),
257
     *   table_uiprefs: (string|null),
258
     *   tracking: (string|null),
259
     *   userconfig: (string|null),
260
     *   usergroups: (string|null),
261
     *   users: (string|null),
262
     *   bookmarkwork: bool,
263
     *   mimework: bool,
264
     *   centralcolumnswork: bool,
265
     *   commwork: bool,
266
     *   menuswork: bool,
267
     *   designersettingswork: bool,
268
     *   displaywork: bool,
269
     *   exporttemplateswork: bool,
270
     *   favoritework: bool,
271
     *   navwork: bool,
272
     *   pdfwork: bool,
273
     *   recentwork: bool,
274
     *   relwork: bool,
275
     *   savedsearcheswork: bool,
276
     *   historywork: bool,
277
     *   trackingwork: bool,
278
     *   uiprefswork: bool,
279
     *   userconfigwork: bool,
280
     *   allworks: bool
281
     * }
282
     */
283 32
    public function toArray(): array
284
    {
285 32
        $columnInfo = null;
286 32
        if ($this->columnCommentsFeature !== null) {
287 4
            $columnInfo = $this->columnCommentsFeature->columnInfo->getName();
288 28
        } elseif ($this->browserTransformationFeature !== null) {
289 4
            $columnInfo = $this->browserTransformationFeature->columnInfo->getName();
290
        }
291
292 32
        $relation = null;
293 32
        if ($this->relationFeature !== null) {
294 4
            $relation = $this->relationFeature->relation->getName();
295 28
        } elseif ($this->displayFeature !== null) {
296 4
            $relation = $this->displayFeature->relation->getName();
297
        }
298
299 32
        return [
300 32
            'version' => Version::VERSION,
301 32
            'user' => $this->user,
302 32
            'db' => $this->db?->getName(),
303 32
            'bookmark' => $this->bookmarkFeature?->bookmark->getName(),
304 32
            'central_columns' => $this->centralColumnsFeature?->centralColumns->getName(),
305 32
            'column_info' => $columnInfo,
306 32
            'designer_settings' => $this->databaseDesignerSettingsFeature?->designerSettings->getName(),
307 32
            'export_templates' => $this->exportTemplatesFeature?->exportTemplates->getName(),
308 32
            'favorite' => $this->favoriteTablesFeature?->favorite->getName(),
309 32
            'history' => $this->sqlHistoryFeature?->history->getName(),
310 32
            'navigationhiding' => $this->navigationItemsHidingFeature?->navigationHiding->getName(),
311 32
            'pdf_pages' => $this->pdfFeature?->pdfPages->getName(),
312 32
            'recent' => $this->recentlyUsedTablesFeature?->recent->getName(),
313 32
            'relation' => $relation,
314 32
            'savedsearches' => $this->savedQueryByExampleSearchesFeature?->savedSearches->getName(),
315 32
            'table_coords' => $this->pdfFeature?->tableCoords->getName(),
316 32
            'table_info' => $this->displayFeature?->tableInfo->getName(),
317 32
            'table_uiprefs' => $this->uiPreferencesFeature?->tableUiPrefs->getName(),
318 32
            'tracking' => $this->trackingFeature?->tracking->getName(),
319 32
            'userconfig' => $this->userPreferencesFeature?->userConfig->getName(),
320 32
            'usergroups' => $this->configurableMenusFeature?->userGroups->getName(),
321 32
            'users' => $this->configurableMenusFeature?->users->getName(),
322 32
            'bookmarkwork' => $this->bookmarkFeature !== null,
323 32
            'mimework' => $this->browserTransformationFeature !== null,
324 32
            'centralcolumnswork' => $this->centralColumnsFeature !== null,
325 32
            'commwork' => $this->columnCommentsFeature !== null,
326 32
            'menuswork' => $this->configurableMenusFeature !== null,
327 32
            'designersettingswork' => $this->databaseDesignerSettingsFeature !== null,
328 32
            'displaywork' => $this->displayFeature !== null,
329 32
            'exporttemplateswork' => $this->exportTemplatesFeature !== null,
330 32
            'favoritework' => $this->favoriteTablesFeature !== null,
331 32
            'navwork' => $this->navigationItemsHidingFeature !== null,
332 32
            'pdfwork' => $this->pdfFeature !== null,
333 32
            'recentwork' => $this->recentlyUsedTablesFeature !== null,
334 32
            'relwork' => $this->relationFeature !== null,
335 32
            'savedsearcheswork' => $this->savedQueryByExampleSearchesFeature !== null,
336 32
            'historywork' => $this->sqlHistoryFeature !== null,
337 32
            'trackingwork' => $this->trackingFeature !== null,
338 32
            'uiprefswork' => $this->uiPreferencesFeature !== null,
339 32
            'userconfigwork' => $this->userPreferencesFeature !== null,
340 32
            'allworks' => $this->hasAllFeatures(),
341 32
        ];
342
    }
343
344 36
    public function hasAllFeatures(): bool
345
    {
346 36
        return $this->bookmarkFeature !== null
347 36
            && $this->browserTransformationFeature !== null
348 36
            && $this->centralColumnsFeature !== null
349 36
            && $this->columnCommentsFeature !== null
350 36
            && $this->configurableMenusFeature !== null
351 36
            && $this->databaseDesignerSettingsFeature !== null
352 36
            && $this->displayFeature !== null
353 36
            && $this->exportTemplatesFeature !== null
354 36
            && $this->favoriteTablesFeature !== null
355 36
            && $this->navigationItemsHidingFeature !== null
356 36
            && $this->pdfFeature !== null
357 36
            && $this->recentlyUsedTablesFeature !== null
358 36
            && $this->relationFeature !== null
359 36
            && $this->savedQueryByExampleSearchesFeature !== null
360 36
            && $this->sqlHistoryFeature !== null
361 36
            && $this->trackingFeature !== null
362 36
            && $this->uiPreferencesFeature !== null
363 36
            && $this->userPreferencesFeature !== null;
364
    }
365
}
366