Test Failed
Push — master ( 2c2c87...ed1c51 )
by Chris
25:05 queued 37s
created

SettingsField::__construct()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
rs 9.6111
cc 5
nc 16
nop 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
namespace Leonidas\Library\Admin\Page\SettingsField;
4
5
use Leonidas\Contracts\Admin\Components\SettingsFieldInterface;
6
use Leonidas\Contracts\Http\ConstrainerCollectionInterface;
7
use Leonidas\Library\Admin\Page\SettingsField\Traits\HasSettingsFieldDataTrait;
8
use Leonidas\Traits\CanBeRestrictedTrait;
9
use Psr\Http\Message\ServerRequestInterface;
10
use WebTheory\Html\Html;
11
use WebTheory\Saveyour\Contracts\DataFormatterInterface;
12
use WebTheory\Saveyour\Contracts\FormFieldInterface;
13
use WebTheory\Saveyour\Fields\Text;
14
15
class SettingsField implements SettingsFieldInterface
16
{
17
    use CanBeRestrictedTrait;
18
    use HasSettingsFieldDataTrait;
19
20
    public function __construct(
21
        string $setting,
22
        string $id,
23
        string $title,
24
        string $page,
25
        string $section,
26
        ?string $inputId = null,
27
        ?array $args = null,
28
        ?FormFieldInterface $input = null,
29
        ?DataFormatterInterface $formatter = null,
30
        ?ConstrainerCollectionInterface $constraints = null
31
    ) {
32
        $this->id = $id;
33
        $this->title = $title;
34
        $this->page = $page;
35
        $this->section = $section;
36
        $this->setting = $setting;
37
38
        $this->inputId = $inputId ?? $this->id;
39
40
        $args && $this->args = $args;
41
        $input && $this->input = $input;
42
        $formatter && $this->formatter = $formatter;
43
        $constraints && $this->constraints = $constraints;
44
    }
45
46
    public function renderComponent(ServerRequestInterface $request): string
47
    {
48
        $output = '';
49
        $settingData = $this->getSettingData();
50
        $value = get_option($this->getSetting(), $settingData['default'] ?? null);
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
        $value = /** @scrutinizer ignore-call */ get_option($this->getSetting(), $settingData['default'] ?? null);
Loading history...
51
52
        $output .= ($this->input ?? $this->getDefaultInput())
53
            ->setName($this->getSetting())
54
            ->setValue($this->formatValue($value))
55
            ->setId($this->getInputId())
56
            ->toHtml() . "\n";
57
58
        if ($description = $settingData['description'] ?? false) {
59
            $output .= $this->renderDescription($description) . "\n";
60
        }
61
62
        return $output;
63
    }
64
65
    protected function getSettingData(): array
66
    {
67
        return get_registered_settings()[$this->getSetting()];
0 ignored issues
show
Bug introduced by
The function get_registered_settings was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        return /** @scrutinizer ignore-call */ get_registered_settings()[$this->getSetting()];
Loading history...
68
    }
69
70
    protected function getDefaultInput(): FormFieldInterface
71
    {
72
        $input = new Text();
73
        $input->addClass('regular-text');
74
75
        return $input;
76
    }
77
78
    protected function renderDescription($description)
79
    {
80
        $this->args['description_attr']['class'][] = 'description';
81
82
        return Html::tag('p', [$this->args['description_attr']], $description);
83
    }
84
85
    protected function formatValue($value)
86
    {
87
        return $this->formatter ? $this->formatter->formatData($value) : $value;
88
    }
89
}
90