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