Nasıl bu kodu temizlemek ve basitleştirmek için?

2 Cevap php

This Question düşünmeye ve buna bir cevap verdikten sonra kendimi eğitmek için bu konuda daha fazla yapmak istedim.

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 */

sonra benim fonksiyonu $ part3 (başlangıç-{ ilginç fonksiyonu sonra) böyle başlamak gerekecektir:

    /* 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 == "\n") {
                    $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 Cevap

Muhtemelen küçük bir devlet değişkenlerin sayısını azaltmak olabilir, ama doğruyu ... evet, dağınık kod olacaktır. Herhalde $may_ml_comment_end kurtulmak ve ben bir yıldız karşılaştığınızda, örneğin, bir sonraki karakter için öncesinde peek olacaktır. Eğer düzenli for döngü olsa büyük bir karışıklık yaratmadan bunu yapabilmek için foreach döngü yeniden yazmak gerekir.

PS: Ben henüz kaçış karakterini ele görmüyorum. Yukarıdaki yaklaşım olmadan, başka bir boolean değişkeni tanıtmak istiyorum.

Geçerli kodu ile başka bir sorun olması gerektiği gibi hemen şu karakterleri bir / yorumlanır alamadım olmasıdır. Ancak olası

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

PHP geçerlidir ve ayrıştırıcı kıracak.

Tokenizer (Example) - Öğren, onu seviyorum.