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

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



  1. param type which save in session;

  2. instagram profile exist;

  3. 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?







share|improve this question

























    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:



    1. param type which save in session;

    2. instagram profile exist;

    3. 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?







    share|improve this question





















      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:



      1. param type which save in session;

      2. instagram profile exist;

      3. 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?







      share|improve this question











      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:



      1. param type which save in session;

      2. instagram profile exist;

      3. 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?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 19 at 7:01









      nepe

      133




      133




















          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.






          share|improve this answer





















          • shouldn't user in user = $instagramProfile->profile->user; if (!user) be $user
            – jmarkmurphy
            Apr 19 at 12:55










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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.






          share|improve this answer





















          • shouldn't user in user = $instagramProfile->profile->user; if (!user) be $user
            – jmarkmurphy
            Apr 19 at 12:55














          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.






          share|improve this answer





















          • shouldn't user in user = $instagramProfile->profile->user; if (!user) be $user
            – jmarkmurphy
            Apr 19 at 12:55












          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.






          share|improve this answer













          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.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Apr 19 at 7:35









          vnp

          36.5k12991




          36.5k12991











          • 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















          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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods