| Conditions | 8 |
| Paths | 1 |
| Total Lines | 133 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 3 | Features | 1 |
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 |
||
| 23 | public function configureOptions(OptionsResolver $resolver) |
||
| 24 | { |
||
| 25 | // enable or disable extra configuration listener |
||
| 26 | $resolver->setDefault('enable_extra_configuration', true); |
||
| 27 | $resolver->setAllowedTypes('enable_extra_configuration', 'boolean'); |
||
| 28 | |||
| 29 | // application title |
||
| 30 | $resolver->setDefault('title', 'AdminBundle application'); |
||
| 31 | $resolver->setAllowedTypes('title', 'string'); |
||
| 32 | |||
| 33 | // application description |
||
| 34 | $resolver->setDefault('description', ''); |
||
| 35 | $resolver->setAllowedTypes('description', 'string'); |
||
| 36 | |||
| 37 | // application locale (en by default) |
||
| 38 | $resolver->setDefault('locale', 'en'); |
||
| 39 | $resolver->setAllowedTypes('locale', 'string'); |
||
| 40 | |||
| 41 | // main base template |
||
| 42 | // as bundles are not loaded when reading the configuration, the kernel locateResources will always failed. |
||
| 43 | // So we must not check resource existance here. |
||
| 44 | $resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig'); |
||
| 45 | $resolver->setAllowedTypes('base_template', 'string'); |
||
| 46 | |||
| 47 | // form block template |
||
| 48 | $resolver->setDefault('block_template', 'LAGAdminBundle:Form:fields.html.twig'); |
||
| 49 | $resolver->setAllowedTypes('block_template', 'string'); |
||
| 50 | |||
| 51 | // use bootstrap or not |
||
| 52 | $resolver->setDefault('bootstrap', true); |
||
| 53 | $resolver->setAllowedTypes('bootstrap', 'boolean'); |
||
| 54 | |||
| 55 | // general date format |
||
| 56 | $resolver->setDefault('date_format', 'Y/m/d'); |
||
| 57 | $resolver->setAllowedTypes('date_format', 'string'); |
||
| 58 | |||
| 59 | // string length before truncation (0 means no truncation) |
||
| 60 | $resolver->setDefault('string_length', 0); |
||
| 61 | $resolver->setAllowedTypes('string_length', 'integer'); |
||
| 62 | |||
| 63 | $resolver->setDefault('string_length_truncate', '...'); |
||
| 64 | $resolver->setAllowedTypes('string_length_truncate', 'string'); |
||
| 65 | |||
| 66 | // routing configuration (route name pattern and url name pattern) |
||
| 67 | $resolver->setDefault('routing', [ |
||
| 68 | 'url_pattern' => '/{admin}/{action}', |
||
| 69 | 'name_pattern' => 'lag.admin.{admin}', |
||
| 70 | ]); |
||
| 71 | $resolver->setAllowedTypes('routing', 'array'); |
||
| 72 | $resolver->setNormalizer('routing', function (Options $options, $value) { |
||
| 73 | |||
| 74 | // url pattern should contain {admin} and {action} token |
||
| 75 | $urlPattern = $value['url_pattern']; |
||
| 76 | |||
| 77 | if (strstr($urlPattern, '{admin}') === false) { |
||
| 78 | throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder'); |
||
| 79 | } |
||
| 80 | if (strstr($urlPattern, '{action}') === false) { |
||
| 81 | throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder'); |
||
| 82 | } |
||
| 83 | |||
| 84 | // name pattern should contain {admin} token |
||
| 85 | $namePattern = $value['name_pattern']; |
||
| 86 | |||
| 87 | if (strstr($namePattern, '{admin}') === false) { |
||
| 88 | throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder'); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $value; |
||
| 92 | }); |
||
| 93 | |||
| 94 | // translation configuration |
||
| 95 | $resolver->setDefault('translation', [ |
||
| 96 | 'enabled' => true, |
||
| 97 | 'pattern' => 'lag.admin.{key}' |
||
| 98 | ]); |
||
| 99 | $resolver->setAllowedTypes('translation', 'array'); |
||
| 100 | $resolver->setNormalizer('translation', function (Options $options, $value) { |
||
| 101 | |||
| 102 | if (!is_bool($value['enabled'])) { |
||
| 103 | throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean'); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!array_key_exists('pattern', $value)) { |
||
| 107 | $value['pattern'] = '{key}'; |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($value['enabled'] && strstr($value['pattern'], '{key}') === false) { |
||
| 111 | throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder'); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $value; |
||
| 115 | }); |
||
| 116 | |||
| 117 | // maximum number of elements displayed |
||
| 118 | $resolver->setDefault('max_per_page', 25); |
||
| 119 | $resolver->setAllowedTypes('max_per_page', 'integer'); |
||
| 120 | |||
| 121 | // admin field type mapping |
||
| 122 | $defaultMapping = [ |
||
| 123 | Field::TYPE_STRING => StringField::class, |
||
| 124 | Field::TYPE_ARRAY => Field\ArrayField::class, |
||
| 125 | Field::TYPE_LINK => Field\Link::class, |
||
| 126 | Field::TYPE_DATE => Field\Date::class, |
||
| 127 | Field::TYPE_COUNT => Field\Count::class, |
||
| 128 | Field::TYPE_ACTION => Field\Action::class, |
||
| 129 | Field::TYPE_COLLECTION => Field\Collection::class, |
||
| 130 | Field::TYPE_BOOLEAN => Field\Boolean::class, |
||
| 131 | ]; |
||
| 132 | |||
| 133 | $resolver->setDefault('fields_mapping', $defaultMapping); |
||
| 134 | $resolver->setAllowedTypes('fields_mapping', 'array'); |
||
| 135 | $resolver->setNormalizer('fields_mapping', function (Options $options, $value) use ($defaultMapping) { |
||
| 136 | // merge with default mapping to allow override |
||
| 137 | $value = array_merge($defaultMapping, $value); |
||
| 138 | |||
| 139 | return $value; |
||
| 140 | }); |
||
| 141 | |||
| 142 | // fields templates mapping |
||
| 143 | $defaultMapping = [ |
||
| 144 | Field::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig', |
||
| 145 | ]; |
||
| 146 | |||
| 147 | $resolver->setDefault('fields_template_mapping', $defaultMapping); |
||
| 148 | $resolver->setAllowedTypes('fields_template_mapping', 'array'); |
||
| 149 | $resolver->setNormalizer('fields_template_mapping', function (Options $options, $value) use ($defaultMapping) { |
||
| 150 | // merge with default mapping to allow override |
||
| 151 | $value = array_merge($defaultMapping, $value); |
||
| 152 | |||
| 153 | return $value; |
||
| 154 | }); |
||
| 155 | } |
||
| 156 | } |
||
| 157 |