Development BlogCleaning up code: key-value lookups

Post Reply
User avatar
Topic Starter
Restless Rancor
Great Britain

Site Admin
New Member
Posts: 48
Joined: October 15th, 2018, 3:14 pm
Age: 25
Location: Hampshire, England
Contact:

Cleaning up code: key-value lookups

#1

Post by Restless Rancor » October 30th, 2018, 6:10 pm

Pre-Release 0.2.3-beta of my Attachment Shields extension had some rather messy php if statements to push data to the templates.

I knew using so many if statements wasn't optimal:

Code: Select all

if ($this->config['ashields_file_style'] == 0)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=flat',
	));
}
if ($this->config['ashields_file_style'] == 1)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=plastic',
	));
}		
if ($this->config['ashields_file_style'] == 2)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=flat-square',
	));
}		
if ($this->config['ashields_file_style'] == 3)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=for-the-badge',
	));
}		
if ($this->config['ashields_file_style'] == 4)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=popout',
	));
}		
if ($this->config['ashields_file_style'] == 5)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=popout-square',
	));
}		
if ($this->config['ashields_file_style'] == 6)
{
	$template->assign_vars(array(
	'ASHIELDS_FILE_STYLE'	=> '?LongCache=true&style=social',
	));
}
// Image shields
if ($this->config['ashields_image_style'] == 0)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=flat',
	));
}
if ($this->config['ashields_image_style'] == 1)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=plastic',
	));
}		
if ($this->config['ashields_image_style'] == 2)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=flat-square',
	));
}		
if ($this->config['ashields_image_style'] == 3)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=for-the-badge',
	));
}		
if ($this->config['ashields_image_style'] == 4)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=popout',
	));
}		
if ($this->config['ashields_image_style'] == 5)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=popout-square',
	));
}		
if ($this->config['ashields_image_style'] == 6)
{
	$template->assign_vars(array(
	'ASHIELDS_IMAGE_STYLE'	=> '?LongCache=true&style=social',
	));
}
// Flash shields
if ($this->config['ashields_flash_style'] == 0)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=flat',
	));
}
if ($this->config['ashields_flash_style'] == 1)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=plastic',
	));
}		
if ($this->config['ashields_flash_style'] == 2)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=flat-square',
	));
}		
if ($this->config['ashields_flash_style'] == 3)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=for-the-badge',
	));
}		
if ($this->config['ashields_flash_style'] == 4)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=popout',
	));
}		
if ($this->config['ashields_flash_style'] == 5)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=popout-square',
	));
}		
if ($this->config['ashields_flash_style'] == 6)
{
	$template->assign_vars(array(
	'ASHIELDS_FLASH_STYLE'	=> '?LongCache=true&style=social',
	));
}
... but I couldn't figure out how to optimize it.

Thankfully I had some quick answers to my question at phpbb.com, the second being the one I've used:

Code: Select all

$ashields_file_style = [
0 => '?LongCache=true&style=flat',
1 => '?LongCache=true&style=plastic',
2 => '?LongCache=true&style=flat-square',
3 => '?LongCache=true&style=for-the-badge',
4 => '?LongCache=true&style=popout',
5 => '?LongCache=true&style=popout-square',
6 => '?LongCache=true&style=social',
];
$ashields_image_style = [
0 => '?LongCache=true&style=flat',
1 => '?LongCache=true&style=plastic',
2 => '?LongCache=true&style=flat-square',
3 => '?LongCache=true&style=for-the-badge',
4 => '?LongCache=true&style=popout',
5 => '?LongCache=true&style=popout-square',
6 => '?LongCache=true&style=social',
];
$ashields_flash_style = [
0 => '?LongCache=true&style=flat',
1 => '?LongCache=true&style=plastic',
2 => '?LongCache=true&style=flat-square',
3 => '?LongCache=true&style=for-the-badge',
4 => '?LongCache=true&style=popout',
5 => '?LongCache=true&style=popout-square',
6 => '?LongCache=true&style=social',
];
This pushes the numbered config output into the array which I'm then able to assign the shield style to before pushing to the template:

Code: Select all

'ASHIELDS_FILE_STYLE' 			=> $ashields_file_style[$this->config['ashields_file_style']],
'ASHIELDS_IMAGE_STYLE' 			=> $ashields_file_style[$this->config['ashields_image_style']],
'ASHIELDS_FLASH_STYLE' 			=> $ashields_file_style[$this->config['ashields_flash_style']],


These aren't the droids you're looking for.

Post Reply

Topic Subscribers

1 user subscribed to this topic: Restless Rancor