douan2907 2010-05-03 22:21
浏览 29
已采纳

如何清理和简化此代码?

After thinking about This Question and giving an answer to it I wanted to do more about that to train myself.

So I wrote a function which will calc the length of an given function. Th given php-file has to start at the beginning of the needed function. Example: If the function is in a big phpfile with lots of functions, like

/* lots of functions */
function f_interesting($arg) {
    /* function */
}
/* lots of other functions */

then $part3 of my function will require to begin like that (after the starting-{ of the interesting function):

    /* function */
}
/* lots of other functions */

Now that's not the problem, but I would like to know if there are an cleaner or simplier ways to do this. Here's my function: (I already cleaned a lot of testing-echo-commands) (The idea behind it is explained here)

function f_analysis ($part3) {
    if(isset($part3)) {
        $char_array = str_split($part3); //get array of chars
        $end_key = false; //length of function
        $depth = 0; //How much of unclosed '{'
        $in_sstr = false; //is next char inside in ''-String?
        $in_dstr = false; //is nect char inside an ""-String?
        $in_sl_comment = false; //inside an //-comment?
        $in_ml_comment = false; //inside an /* */-comment?
        $may_comment = false; //was the last char an '/' which can start a comment?
        $may_ml_comment_end = false; //was the last char an '*' which may end a /**/-comment?
        foreach($char_array as $key=>$char) {
            if($in_sstr) {
                if ($char == "'") {
                    $in_sstr = false;
                }
            }
            else if($in_dstr) {
                if($char == '"') {
                    $in_dstr = false;
                }
            }
            else if($in_sl_comment) {
                if($char == "
") {
                    $in_sl_comment = false;
                }
            }
            else if($in_ml_comment) {
                if($may_ml_comment_end) {
                    $may_ml_comment_end = false;
                    if($char == '/') {
                        $in_ml_comment = false;
                    }
                }
                if($char == '*') {
                    $may_ml_comment_end = true;
                }
            }
            else if ($may_comment) {
                if($char == '/') {
                    $in_sl_comment = true;
                }
                else if($char == '*') {
                    $in_ml_comment = true;
                }
                $may_comment = false;
            }
            else {
                switch ($char) {
                    case '{':
                        $depth++;
                        break;
                    case '}':
                        $depth--;
                        break;
                    case '/':
                        $may_comment = true;
                        break;
                    case '"':
                        $in_dstr = true;
                        break;
                    case "'":
                        $in_sstr = true;
                        break;
                }
            }

            if($depth < 0) {
                $last_key = $key;
                break;
            }
        }
    } else echo '<br>$part3 of f_analysis not set!';
    return ($last_key===false) ? false : $last_key+1; //will be false or the length of the function
}
  • 写回答

2条回答 默认 最新

  • doumeng1089 2010-05-03 22:45
    关注

    You could probably reduce the number of state variables a little, but truthfully... yes, it will be messy code. I would probably get rid of $may_ml_comment_end and peek ahead for the next character when I encounter an asterisk, for example. You will need to rewrite your foreach loop to a regular for loop be able to do that without creating a bigger mess though.

    PS: I don't see you handling the escape character yet. Without the above approach, that would introduce another boolean variable.

    Another problem with your current code is that characters immediately following a / don't get interpreted as they should. However unlikely

    echo 5/'2';  // NB: no space in between
    

    is valid in PHP and would break your parser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)