| Conditions | 9 |
| Paths | 36 |
| Total Lines | 78 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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\PreferencesModule\Preference\Form; |
||
| 39 | public function handle(PreferenceFormBuilder $builder, PreferenceRepositoryInterface $preferences) |
||
| 40 | { |
||
| 41 | $namespace = $builder->getEntry() . '::'; |
||
| 42 | |||
| 43 | /* |
||
| 44 | * Get the fields from the config system. Sections are |
||
| 45 | * optionally defined the same way. |
||
| 46 | */ |
||
| 47 | if (!$fields = $this->config->get($namespace . 'preferences/preferences')) { |
||
| 48 | $fields = $fields = $this->config->get($namespace . 'preferences', []); |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($sections = $this->config->get($namespace . 'preferences/sections')) { |
||
| 52 | $builder->setSections($sections); |
||
| 53 | } |
||
| 54 | |||
| 55 | /* |
||
| 56 | * Finish each field. |
||
| 57 | */ |
||
| 58 | foreach ($fields as $slug => &$field) { |
||
| 59 | |||
| 60 | /* |
||
| 61 | * Force an array. This is done later |
||
| 62 | * too in normalization but we need it now |
||
| 63 | * because we are normalizing / guessing our |
||
| 64 | * own parameters somewhat. |
||
| 65 | */ |
||
| 66 | if (is_string($field)) { |
||
| 67 | $field = [ |
||
| 68 | 'type' => $field, |
||
| 69 | ]; |
||
| 70 | } |
||
| 71 | |||
| 72 | // Make sure we have a config property. |
||
| 73 | $field['config'] = array_get($field, 'config', []); |
||
| 74 | |||
| 75 | // Default the label. |
||
| 76 | $field['label'] = array_get( |
||
| 77 | $field, |
||
| 78 | 'label', |
||
| 79 | $namespace . 'preference.' . $slug . '.label' |
||
| 80 | ); |
||
| 81 | |||
| 82 | // Default the placeholder. |
||
| 83 | $field['config']['placeholder'] = array_get( |
||
| 84 | $field['config'], |
||
| 85 | 'placeholder', |
||
| 86 | $namespace . 'preference.' . $slug . '.placeholder' |
||
| 87 | ); |
||
| 88 | |||
| 89 | // Default the instructions. |
||
| 90 | $field['instructions'] = array_get( |
||
| 91 | $field, |
||
| 92 | 'instructions', |
||
| 93 | $namespace . 'preference.' . $slug . '.instructions' |
||
| 94 | ); |
||
| 95 | |||
| 96 | // Get the value defaulting to the default value. |
||
| 97 | |||
| 98 | if ($preference = $preferences->get($namespace . $slug)) { |
||
| 99 | $field['value'] = $preference->getValue(); |
||
| 100 | } else { |
||
| 101 | $field['value'] = array_get($field['config'], 'default_value'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /* |
||
| 105 | * Disable the field if it |
||
| 106 | * has a set env value. |
||
| 107 | */ |
||
| 108 | if (isset($field['env']) && isset($field['bind']) && env($field['env']) !== null) { |
||
| 109 | $field['disabled'] = true; |
||
| 110 | $field['warning'] = 'module::message.env_locked'; |
||
| 111 | $field['value'] = $this->config->get($field['bind']); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $builder->setFields($fields); |
||
| 116 | } |
||
| 117 | } |
||
| 118 |