71367d98df2bf14cbf704e7741eb402d

<?php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
	function username($user_id = null) {

		$user_id = ($user_id != null) ? $user_id : current_user(); // if inserted ID to function, else return logged user ID

		if ( !User::get_user_details($user_id) ) // if user does not exists
		return $id; // return only ID
		
		else {
			
			$inner = User::get_user_details($user_id); // if user exist
			return $inner['username']; // return ID username
		}
	}
?>

Refactorings

No refactoring yet !

F666265b69d7510f44a918ee3bc2dcf9

DevP

May 1, 2008, May 01, 2008 04:49, permalink

No rating. Login to rate!

Are you sure you want to return the ID if the user does not exist? I'll assume that's what you want...

1
2
3
4
5
6
7
8
9
10
11
<?php
	function username($user_id = null) {
		$user_id = is_null($user_id) ? $user_id : current_user();
		$user_details = User::get_user_details($user_id);
		if ($user_details) {
				return $user_details['username'];
		} else {
				return $user_id;
		}
	}
?>
36d905a90bc68c7b416bde73cc924575

Eljay

May 1, 2008, May 01, 2008 21:53, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
7
<?php
function username($user_id = null) {
	if(is_null($user_id)) $user_id = current_user();
	$user_details = User::get_user_details($user_id);
	return $user_details ? $user_details['username'] : $user_id;
}
?>
Avatar

WHF

May 16, 2008, May 16, 2008 12:50, permalink

No rating. Login to rate!

Untested but this is a funny way to do some sort of isset() without the if.

But I'm not sure if it will work with this code.

1
2
3
4
5
6
7
<?php
function username($user_id = null) {
	!is_null($user_id) OR $user_id = current_user();
	$user_details = User::get_user_details($user_id);
	return $user_details ? $user_details['username'] : $user_id;
}
?>

Your refactoring





Format Copy from initial code

or Cancel