Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp to detect call time pass by references in PHP source code

Tags:

regex

php

eclipse

I'm looking for a regular expression that will accurately identify any PHP call time pass by references in source code in order to aid migration to PHP 5.3.

Currently, I have [^=&]\s*&\s*\$, but this doesn't filter out assignment cases ($var = &$othervar;).

This regexp should be compatible with eclipse (sorry, not sure what flavor of regexp eclipse parses).

Edit: This one is a little bit closer (although a bit of a hack): (?<!([&=]\s{0,15}))&\s*\$

like image 551
Kenaniah Avatar asked Oct 27 '10 17:10

Kenaniah


3 Answers

You can use phpcs for this. It has a rule to detect Call Time Pass by References:

Ensures that variables are not passed by reference when calling a function.

There is also a plugin to integrate phpcs into Eclipse

Generating rulesets for PHPCS (and PMD) is easy with this online generator:

  • http://edorian.github.io/php-coding-standard-generator/#phpcs
like image 74
Gordon Avatar answered Nov 09 '22 14:11

Gordon


php -l (php-linter) finds call-time pass-by-reference errors, I used

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

in linux

like image 36
Hoffmann Avatar answered Nov 09 '22 14:11

Hoffmann


You can't get those with regex. Use the Tokenizer instead. You will need to look for '&' where the next '(' to the left (resolve brackets while walking there) is preceded by T_STRING but not by 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;
            }
        }
    }
}

This utilizes my TokenStream wrapper. With the native output it will get quite a bit harder ;)

like image 23
NikiC Avatar answered Nov 09 '22 15:11

NikiC