How to sort after removing `goto` statements, maki

2019-03-07 04:03发布

问题:

Maintenance of the company's previous project, the staff who wrote the project has left, now I need to maintain, but now the project uses a lot of'goto', I want to know how to read it. How to sort after removing'goto'statements, making it easier for people to understand. For short, I want to format the messy code in the left side to be like the right side in the picture

//**this code
defined('IN_IA') or exit('Access Denied');
class Md_daojiaModuleSite extends WeModuleSite
{
    public function doPageUploadmap3()
    {
        goto A25dm;
        ADcuT:
        ZLMND:
        goto wJ44l;
        z_M2Z:
        if (empty($_FILES["file"]["tmp_name"])) {
            goto ZLMND;
        }
        goto RnW8Z;
        M61ng:
        $tempfile = ATTACHMENT_ROOT . "/audios/" . $name;
        goto ReGaV;
        A25dm:
        global $_GPC, $_W;
        goto z_M2Z;
        RnW8Z:
        $exname = strtolower(substr($_FILES["file"]["name"], strrpos($_FILES["file"]["name"], ".") + 1));
        goto BO3TT;
        wJ44l:
        return $this->result(0, '', "attachment/audios/" . $name);
        goto Qx8PP;
        BO3TT:
        $name = md5(time()) . "." . $exname;
        goto M61ng;
        ReGaV:
        move_uploaded_file($_FILES["file"]["tmp_name"], $tempfile);
        goto ADcuT;
        Qx8PP:
    }
}

回答1:

You basically just reproduce what the interpreter would do:

  1. From top to bottom, look at each goto statement.

    goto A25dm;
    …
    
  2. Shuffle the according jump marker and code section directly behind it.

    A25dm:                       # ← MARKER: starts section
    global $_GPC, $_W;
    goto z_M2Z;                  # ← next goto ends it
    
  3. So you end up with:

    //goto A25dm;                # → no longer needed
    A25dm:                       
    global $_GPC, $_W;
    goto z_M2Z;                  # → proceed with step 1
    

Once you have associated each marker: right behind the goto, just remove or comment those out.



标签: php goto