Conditions | 1 |
Paths | 1 |
Total Lines | 116 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 2 | 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 |
||
196 | public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { |
||
197 | $fields = []; |
||
198 | |||
199 | $fields['cid'] = BaseFieldDefinition::create('integer') |
||
200 | ->setLabel(t('Crop ID')) |
||
201 | ->setDescription(t('The crop ID.')) |
||
202 | ->setReadOnly(TRUE) |
||
203 | ->setSetting('unsigned', TRUE); |
||
204 | |||
205 | $fields['uuid'] = BaseFieldDefinition::create('uuid') |
||
206 | ->setLabel(t('UUID')) |
||
207 | ->setDescription(t('The crop UUID.')) |
||
208 | ->setReadOnly(TRUE); |
||
209 | |||
210 | $fields['vid'] = BaseFieldDefinition::create('integer') |
||
211 | ->setLabel(t('Revision ID')) |
||
212 | ->setDescription(t('The crop revision ID.')) |
||
213 | ->setReadOnly(TRUE) |
||
214 | ->setSetting('unsigned', TRUE); |
||
215 | |||
216 | $fields['type'] = BaseFieldDefinition::create('entity_reference') |
||
217 | ->setLabel(t('Type')) |
||
218 | ->setDescription(t('The crop type.')) |
||
219 | ->setSetting('target_type', 'crop_type') |
||
220 | ->setReadOnly(TRUE); |
||
221 | |||
222 | $fields['langcode'] = BaseFieldDefinition::create('language') |
||
223 | ->setLabel(t('Language code')) |
||
224 | ->setDescription(t('The node language code.')) |
||
225 | ->setRevisionable(TRUE); |
||
226 | |||
227 | $fields['entity_id'] = BaseFieldDefinition::create('integer') |
||
228 | ->setLabel(t('Entity ID')) |
||
229 | ->setDescription(t('ID of entity crop belongs to.')) |
||
230 | ->setSetting('unsigned', TRUE) |
||
231 | ->setRevisionable(TRUE) |
||
232 | ->setReadOnly(TRUE); |
||
233 | |||
234 | $fields['entity_type'] = BaseFieldDefinition::create('string') |
||
235 | ->setLabel(t('Entity type')) |
||
236 | ->setDescription(t('The type of entity crop belongs to.')) |
||
237 | ->setRevisionable(TRUE) |
||
238 | ->setReadOnly(TRUE); |
||
239 | |||
240 | // Denormalized information, which is calculated in storage plugin for a |
||
241 | // given entity type. Saved here for performance reasons in image effects. |
||
242 | // --- |
||
243 | // TODO - we are not enforcing uniqueness on this as we want to support more |
||
244 | // crops per same image/image_style combination. However, image effect |
||
245 | // operates with image URI only, which means we have no mechanism to |
||
246 | // distinguish between multiple crops in there. If we really want to |
||
247 | // support multiple crops we'll need to override core at least, |
||
248 | // in \Drupal\Core\Image\ImageFactory and \Drupal\Core\Image\Image. |
||
249 | // Let's leave this for now and simply load based on URI only. |
||
250 | // We can use some semi-smart approach in case there are multiple crops |
||
251 | // with same URI for now (first created, last created, ...). |
||
252 | $fields['uri'] = BaseFieldDefinition::create('uri') |
||
253 | ->setLabel(t('URI')) |
||
254 | ->setDescription(t('The URI of the image crop belongs to.')) |
||
255 | ->setRevisionable(TRUE) |
||
256 | ->setTranslatable(TRUE) |
||
257 | ->setSetting('max_length', 255); |
||
258 | |||
259 | $fields['height'] = BaseFieldDefinition::create('integer') |
||
260 | ->setLabel(t('Height')) |
||
261 | ->setDescription(t('The crop height.')) |
||
262 | ->setRevisionable(TRUE) |
||
263 | ->setTranslatable(TRUE) |
||
264 | ->setReadOnly(TRUE) |
||
265 | ->setSetting('unsigned', TRUE); |
||
266 | |||
267 | $fields['width'] = BaseFieldDefinition::create('integer') |
||
268 | ->setLabel(t('Width')) |
||
269 | ->setDescription(t('The crop width.')) |
||
270 | ->setRevisionable(TRUE) |
||
271 | ->setTranslatable(TRUE) |
||
272 | ->setReadOnly(TRUE) |
||
273 | ->setSetting('unsigned', TRUE); |
||
274 | |||
275 | $fields['x'] = BaseFieldDefinition::create('integer') |
||
276 | ->setLabel(t('X coordinate')) |
||
277 | ->setDescription(t("The crop's X coordinate.")) |
||
278 | ->setRevisionable(TRUE) |
||
279 | ->setTranslatable(TRUE) |
||
280 | ->setReadOnly(TRUE) |
||
281 | ->setSetting('unsigned', TRUE); |
||
282 | |||
283 | $fields['y'] = BaseFieldDefinition::create('integer') |
||
284 | ->setLabel(t('Y coordinate')) |
||
285 | ->setDescription(t("The crop's Y coordinate.")) |
||
286 | ->setRevisionable(TRUE) |
||
287 | ->setTranslatable(TRUE) |
||
288 | ->setReadOnly(TRUE) |
||
289 | ->setSetting('unsigned', TRUE); |
||
290 | |||
291 | $fields['revision_timestamp'] = BaseFieldDefinition::create('created') |
||
292 | ->setLabel(t('Revision timestamp')) |
||
293 | ->setDescription(t('The time that the current revision was created.')) |
||
294 | ->setQueryable(FALSE) |
||
295 | ->setRevisionable(TRUE); |
||
296 | |||
297 | $fields['revision_uid'] = BaseFieldDefinition::create('entity_reference') |
||
298 | ->setLabel(t('Revision author ID')) |
||
299 | ->setDescription(t('The user ID of the author of the current revision.')) |
||
300 | ->setSetting('target_type', 'user') |
||
301 | ->setQueryable(FALSE) |
||
302 | ->setRevisionable(TRUE); |
||
303 | |||
304 | $fields['revision_log'] = BaseFieldDefinition::create('string_long') |
||
305 | ->setLabel(t('Revision Log')) |
||
306 | ->setDescription(t('The log entry explaining the changes in this revision.')) |
||
307 | ->setRevisionable(TRUE) |
||
308 | ->setTranslatable(TRUE); |
||
309 | |||
310 | return $fields; |
||
311 | } |
||
312 | |||
314 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: