Answer the question
In order to leave comments, you need to log in
How can this piece of PHP shit code be improved?
There is a shit code, it's mine, I want to improve it with making it more pleasant, and reduce the number of lines, but I can't think of what can be done to bring it into human form. I hope for your help.
$leadimage = get_field('lead_image');
$bot_first_img = get_field('image_#1');
$bot_second_img = get_field('image_#2');
$bot_third_img = get_field('image_#3');
//Lead Image
$url = $leadimage['url'];
$title = $leadimage['title'];
$alt = $leadimage['alt'];
$caption = $leadimage['caption'];
//Bot Img 1
$url1 = $first_img['url'];
$title1 = $first_img['title'];
$alt1 = $first_img['alt'];
$caption1 = $first_img['caption'];
//Bot Img 2
$url2 = $second_img['url'];
$title2 = $second_img['title'];
$alt2 = $second_img['alt'];
$caption2 = $second_img['caption'];
//Bot Img 3
$url2 = $second_img['url'];
$title2 = $second_img['title'];
$alt2 = $second_img['alt'];
$caption2 = $second_img['caption'];
//Lead Image
$size = 'lead-custom-size';
$thumb = $leadimage['sizes'][ $size ];
$width = $leadimage['sizes'][ $size . '-width' ];
$height = $leadimage['sizes'][ $size . '-height' ];
//Bot Img 1
$size3 = 'circle-thumbnail-small';
$thumb3 = $bot_first_img['sizes'][ $size3 ];
$width3 = $bot_first_img['sizes'][ $size3 . '-width' ];
$height3 = $bot_first_img['sizes'][ $size3 . '-height' ];
//Bot Img 2
$size4 = 'circle-thumbnail-small';
$thumb4 = $bot_second_img['sizes'][ $size4 ];
$width4 = $bot_second_img['sizes'][ $size4 . '-width' ];
$height4 = $bot_second_img['sizes'][ $size4 . '-height' ];
//Bot Img 3
$size5 = 'circle-thumbnail-small';
$thumb5 = $bot_third_img['sizes'][ $size5 ];
$width5 = $bot_third_img['sizes'][ $size5 . '-width' ];
$height5 = $bot_third_img['sizes'][ $size5 . '-height' ];
Answer the question
In order to leave comments, you need to log in
function getImageData($name, $size) {
$data = get_field($name);
return array_merge($data, [
'thumb' => $data['sizes'][$size],
'width' => $data['sizes'][$size . '-width'],
'height' => $data['sizes'][$size . '-height'],
]);
}
$image = getImageData('lead_image', 'lead-custom-size');
// view.php
<img src="<?= $image['url'] ?>" height="<?= $image['height'] ?>" width="<?= $image['width'] ?>">
// sarcasm begin
// Refactored!
_
_( )_
(_(%)_)
(_)\
| __
|/_/
|
|
$leadimage = get_field('lead_image');
$bot_first_img = get_field('image_#1');
$bot_second_img = get_field('image_#2');
...
// sarcasm end
as a joke.
Shorten shit code ;D
for $i = 0; i <= 3; $i ++) {
${'image'.$i} = get_field('image_#' . $i);
${'url'.$i} = ${'image'.$i}['url'];
${'title'.$i} = ${'image'.$i}['title'];
${'alt'.$i} = ${'image'.$i}['alt'];
${'caption'.$i} = ${'image'.$i}['caption'];
${'size'.$i} = ($i === 0) ? 'lead-custom-size' : 'circle-thumbnail-small';
${'thumb'.$i} = ${'image'.$i}['sizes'][ ${'size'.$i} ];
${'width'.$i} = ${'image'.$i}['sizes'][ ${'size'.$i} . '-width' ];
${'width'.$i} = ${'image'.$i}['sizes'][ ${'size'.$i} . '-height' ];
}
to begin with, it is not clear why you translate each element of the array into a separate variable.
I would try using extract .
Or would make each image an instance of a class that would encapsulate all this nonsense in properties.
This code is hard to refactor. It's easier to rewrite.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question