Conditions | 13 |
Paths | 516 |
Total Lines | 108 |
Code Lines | 46 |
Lines | 40 |
Ratio | 37.04 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php namespace Anomaly\SettingsModule\Setting\Form; |
||
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( |
|
|
|||
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( |
|
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( |
|
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( |
|
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 |
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.