How optimize set of nested 'if' in one function?

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have such part of function:
$token = $this->getToken($user);
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
else
if (!$instagramProfile = $this->findInstagramProfile($user))
$profile = create_instagram_profile($user);
$user = $this->createUser($profile);
$token = $user->api_token;
else
if($instagramProfile->profile->user)
$token = $instagramProfile->profile->user->api_token;
else
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
;
I have many scenarios:
- param
typewhich save in session; - instagram profile exist;
- user exists.
In worlds - very simple, but in code it stay not understandable.
How I can optimize such part of code?
Maybe create more abstract function?
php laravel
add a comment |Â
up vote
2
down vote
favorite
I have such part of function:
$token = $this->getToken($user);
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
else
if (!$instagramProfile = $this->findInstagramProfile($user))
$profile = create_instagram_profile($user);
$user = $this->createUser($profile);
$token = $user->api_token;
else
if($instagramProfile->profile->user)
$token = $instagramProfile->profile->user->api_token;
else
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
;
I have many scenarios:
- param
typewhich save in session; - instagram profile exist;
- user exists.
In worlds - very simple, but in code it stay not understandable.
How I can optimize such part of code?
Maybe create more abstract function?
php laravel
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have such part of function:
$token = $this->getToken($user);
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
else
if (!$instagramProfile = $this->findInstagramProfile($user))
$profile = create_instagram_profile($user);
$user = $this->createUser($profile);
$token = $user->api_token;
else
if($instagramProfile->profile->user)
$token = $instagramProfile->profile->user->api_token;
else
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
;
I have many scenarios:
- param
typewhich save in session; - instagram profile exist;
- user exists.
In worlds - very simple, but in code it stay not understandable.
How I can optimize such part of code?
Maybe create more abstract function?
php laravel
I have such part of function:
$token = $this->getToken($user);
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
else
if (!$instagramProfile = $this->findInstagramProfile($user))
$profile = create_instagram_profile($user);
$user = $this->createUser($profile);
$token = $user->api_token;
else
if($instagramProfile->profile->user)
$token = $instagramProfile->profile->user->api_token;
else
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
;
I have many scenarios:
- param
typewhich save in session; - instagram profile exist;
- user exists.
In worlds - very simple, but in code it stay not understandable.
How I can optimize such part of code?
Maybe create more abstract function?
php laravel
asked Apr 19 at 7:01
nepe
133
133
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I am not sure I understand what is supposed to happen in
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
if by any chance $instagramProfile->profile->user happens to be null. Otherwise, refactoring is very straightforward. Just lift the common actions out of the if/else one by one:
$instagramProfile = this->findInstagramProfile($user);
if (!$instagramProfile)
$instagramProfile = create_instagram_profile($user);
user = $instagramProfile->profile->user;
if (!user)
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
Observe that it is just a mechanical rewrite.
PS: I presume that create_instagram_profile can be safely called with null argument; you are doing it anyway.
shouldn'tuserinuser = $instagramProfile->profile->user; if (!user)be$user
â jmarkmurphy
Apr 19 at 12:55
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I am not sure I understand what is supposed to happen in
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
if by any chance $instagramProfile->profile->user happens to be null. Otherwise, refactoring is very straightforward. Just lift the common actions out of the if/else one by one:
$instagramProfile = this->findInstagramProfile($user);
if (!$instagramProfile)
$instagramProfile = create_instagram_profile($user);
user = $instagramProfile->profile->user;
if (!user)
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
Observe that it is just a mechanical rewrite.
PS: I presume that create_instagram_profile can be safely called with null argument; you are doing it anyway.
shouldn'tuserinuser = $instagramProfile->profile->user; if (!user)be$user
â jmarkmurphy
Apr 19 at 12:55
add a comment |Â
up vote
0
down vote
accepted
I am not sure I understand what is supposed to happen in
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
if by any chance $instagramProfile->profile->user happens to be null. Otherwise, refactoring is very straightforward. Just lift the common actions out of the if/else one by one:
$instagramProfile = this->findInstagramProfile($user);
if (!$instagramProfile)
$instagramProfile = create_instagram_profile($user);
user = $instagramProfile->profile->user;
if (!user)
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
Observe that it is just a mechanical rewrite.
PS: I presume that create_instagram_profile can be safely called with null argument; you are doing it anyway.
shouldn'tuserinuser = $instagramProfile->profile->user; if (!user)be$user
â jmarkmurphy
Apr 19 at 12:55
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I am not sure I understand what is supposed to happen in
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
if by any chance $instagramProfile->profile->user happens to be null. Otherwise, refactoring is very straightforward. Just lift the common actions out of the if/else one by one:
$instagramProfile = this->findInstagramProfile($user);
if (!$instagramProfile)
$instagramProfile = create_instagram_profile($user);
user = $instagramProfile->profile->user;
if (!user)
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
Observe that it is just a mechanical rewrite.
PS: I presume that create_instagram_profile can be safely called with null argument; you are doing it anyway.
I am not sure I understand what is supposed to happen in
if ('main' !== session('type') && is_null($token))
$instagramProfile = $this->findInstagramProfile($user);
$token = $instagramProfile->profile->user->api_token ?? null;
if by any chance $instagramProfile->profile->user happens to be null. Otherwise, refactoring is very straightforward. Just lift the common actions out of the if/else one by one:
$instagramProfile = this->findInstagramProfile($user);
if (!$instagramProfile)
$instagramProfile = create_instagram_profile($user);
user = $instagramProfile->profile->user;
if (!user)
$user = $this->createUser($instagramProfile->profile);
$token = $user->api_token;
Observe that it is just a mechanical rewrite.
PS: I presume that create_instagram_profile can be safely called with null argument; you are doing it anyway.
answered Apr 19 at 7:35
vnp
36.5k12991
36.5k12991
shouldn'tuserinuser = $instagramProfile->profile->user; if (!user)be$user
â jmarkmurphy
Apr 19 at 12:55
add a comment |Â
shouldn'tuserinuser = $instagramProfile->profile->user; if (!user)be$user
â jmarkmurphy
Apr 19 at 12:55
shouldn't
user in user = $instagramProfile->profile->user; if (!user) be $userâ jmarkmurphy
Apr 19 at 12:55
shouldn't
user in user = $instagramProfile->profile->user; if (!user) be $userâ jmarkmurphy
Apr 19 at 12:55
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192435%2fhow-optimize-set-of-nested-if-in-one-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password