代码之家  ›  专栏  ›  技术社区  ›  Kenaniah

Regexp在PHP源代码中检测通过引用的调用时间传递

  •  7
  • Kenaniah  · 技术社区  · 16 年前

    我正在寻找一个正则表达式,它将准确地标识源代码中通过引用传递的任何PHP调用时间,以帮助迁移到PHP 5.3。

    [^=&]\s*&\s*\$ ,但这不会筛选出分配情况( $var = &$othervar; ).

    这个regexp应该与eclipse兼容(抱歉,不确定eclipse解析的regexp是什么风格)。

    编辑: (?<!([&=]\s{0,15}))&\s*\$

    5 回复  |  直到 16 年前
        1
  •  7
  •   Gordon Haim Evgi    12 年前

    你可以用 phpcs 为了这个。它有一个 rule to detect Call Time Pass by References

    确保在调用函数时变量不会通过引用传递。

    plugin to integrate phpcs into Eclipse

    使用此在线生成器可以轻松为phpc(和PMD)生成规则集:

        2
  •  4
  •   Hoffmann    12 年前

    php -l (php linter)查找通过引用传递的调用时间错误,我使用

    find -name '*.php' -exec php -l '{}' \; >/dev/null
    

    在linux中

        3
  •  3
  •   NikiC    16 年前

    你不能用regex来买。使用 Tokenizer '&' 下一个 '(' 在左边(边走边解括号)前面是 T_STRING 但不是通过 T_FUNCTION .

    $tokens = new TokenStream($source);
    foreach ($tokens as $i => $token) {
        if ($token->is(T_AMP)) {
            while ($i--) {
                if ($tokens[$i]->is(T_CLOSE_ROUND, T_CLOSE_SQUARE, T_CLOSE_CURLY)) {
                    $i = $tokens->complementaryBracket($i);
                } elseif ($tokens[$i]->is(T_OPEN_ROUND)) {
                    if ((($tokens[--$i]->is(T_WHITESPACE) && $tokens[--$i]->is(T_STRING))
                         || $tokens[$i]->is(T_STRING))
                        && !$tokens[--$i]->is(T_WHITESPACE)
                        && !$tokens[--$i]->is(T_FUNCTION)
                    ) {
                        throw new Exception('Call-time pass by reference');
                    }
                    break;
                }
            }
        }
    }
    

    这利用了 my TokenStream wrapper . 有了本机输出,它将变得更加困难;)

        4
  •  0
  •   user5141486    11 年前
    ^(?!^.*(function|foreach|array)).*\(.*\&\$.*\)
    

        5
  •  0
  •   magic_al    11 年前

    您可以使用以下模式:

    /(->|::|call_user_func|call_user_func_callable).*\(.*\&\$/
    

    '->($arg1, &$arg2)'
    '->(&$arg1, $arg2)'
    '::($arg1, &$arg2)'
    '::(&$arg1, $arg2)'
    'call_user_func($callback, &$arg2)'
    '$callback, &$arg2)'
    'call_user_func_callable $callback, &$param_arr)'
    

    万一 call_user_func_callable

    推荐文章