SettingFormFields   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 29.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 14
c 10
b 0
f 0
lcom 1
cbo 1
dl 40
loc 134
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F handle() 40 108 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\SettingsModule\Setting\Form;
2
3
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
4
use Illuminate\Config\Repository;
5
6
/**
7
 * Class SettingFormFields
8
 *
9
 * @link          http://pyrocms.com/
10
 * @author        PyroCMS, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 */
13
class SettingFormFields
14
{
15
16
    /**
17
     * The config repository.
18
     *
19
     * @var Repository
20
     */
21
    protected $config;
22
23
    /**
24
     * Create a new SettingFormFields instance.
25
     *
26
     * @param Repository $config
27
     */
28
    public function __construct(Repository $config)
29
    {
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * Return the form fields.
35
     *
36
     * @param SettingFormBuilder $builder
37
     */
38
    public function handle(SettingFormBuilder $builder, SettingRepositoryInterface $settings)
39
    {
40
        $namespace = $builder->getFormEntry() . '::';
41
42
        /*
43
         * Get the fields from the config system. Sections are
44
         * optionally defined the same way.
45
         */
46
        if (!$fields = $this->config->get($namespace . 'settings/settings')) {
47
            $fields = $fields = $this->config->get($namespace . 'settings', []);
48
        }
49
50
        if ($sections = $this->config->get($namespace . 'settings/sections')) {
51
            $builder->setSections($sections);
52
        }
53
54
        /*
55
         * Finish each field.
56
         */
57
        foreach ($fields as $slug => &$field) {
58
59
            /*
60
             * Force an array. This is done later
61
             * too in normalization but we need it now
62
             * because we are normalizing / guessing our
63
             * own parameters somewhat.
64
             */
65
            if (is_string($field)) {
66
                $field = [
67
                    'type' => $field,
68
                ];
69
            }
70
71
            // Make sure we have a config property.
72
            $field['config'] = array_get($field, 'config', []);
73
74 View Code Duplication
            if (trans()->has(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                $label = array_get(
76
                    $field,
77
                    'label',
78
                    $namespace . 'setting.' . $slug . '.label'
79
                )
80
            )
81
            ) {
82
                $field['label'] = $label;
83
            }
84
85
            // Default the label.
86
            $field['label'] = array_get(
87
                $field,
88
                'label',
89
                $namespace . 'setting.' . $slug . '.name'
90
            );
91
92
            // Default the warning.
93 View Code Duplication
            if (trans()->has(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
                $warning = array_get(
95
                    $field,
96
                    'warning',
97
                    $namespace . 'setting.' . $slug . '.warning'
98
                )
99
            )
100
            ) {
101
                $field['warning'] = $warning;
102
            }
103
104
            // Default the placeholder.
105 View Code Duplication
            if (trans()->has(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
                $placeholder = array_get(
107
                    $field,
108
                    'placeholder',
109
                    $namespace . 'setting.' . $slug . '.placeholder'
110
                )
111
            )
112
            ) {
113
                $field['placeholder'] = $placeholder;
114
            }
115
116
            // Default the instructions.
117 View Code Duplication
            if (trans()->has(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
                $instructions = array_get(
119
                    $field,
120
                    'instructions',
121
                    $namespace . 'setting.' . $slug . '.instructions'
122
                )
123
            )
124
            ) {
125
                $field['instructions'] = $instructions;
126
            }
127
128
            // Get the value defaulting to the default value.
129
            if (!isset($field['value'])) {
130
                $field['value'] = $settings->value($namespace . $slug, array_get($field['config'], 'default_value'));
131
            }
132
133
            /*
134
             * Disable the field if it
135
             * has a set env value.
136
             */
137
            if (isset($field['env']) && isset($field['bind']) && env($field['env']) !== null) {
138
                $field['disabled'] = true;
139
                $field['warning']  = 'module::message.env_locked';
140
                $field['value']    = $this->config->get($field['bind']);
141
            }
142
        }
143
144
        $builder->setFields($fields);
145
    }
146
}
147