How to sort after removing `goto` statements, maki

2019-03-07 03:21发布

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 how to format your 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:
    }
}

标签: php goto
1条回答
Fickle 薄情
2楼-- · 2019-03-07 04:01

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.

查看更多
登录 后发表回答