TYPO free

home

fighting for TYPO free code

Check your PHP code for deprecated features

12 May 2011

In the latest stable version of PHP (5.3 at the time of writing) a number of ini directives and functions have been deprecated. Love it or hate it, the code that uses these ini directives and functions will behave badly when run in PHP 5.3. The TYPO3 core has been fine with PHP 5.3 for a while now, but the majority of the 5000+ extensions still uses deprecated functions.

PHP 5.3 has been out for a while now in several distributions. With the release of Debian Squeeze it also became available in Debian stable. This meant it was time for me to go through all the PHP code on my server to check for deprecated functions and ini directives.

This would be a huge task to do by hand, but luckily we can can use some great shell tools to do the heavy lifting for us. I wrote a small script to find all the deprecated stuff in the directory it is executed in.

If you use this script on your TYPO3 extension dir, please notify the authors of the extensions if you find any deprecated code. Provide patches and help fix the extensions.

This is very basic. Please extend it as you wish so the result can be mailed out for example.

(=- Doubleclick the code to select -=)

#!/bin/bash
#
# PHP 5.3 Deprecated function checker
#
# Version: 0.0.3
#
# Author: Michiel Roos <michiel@donationbasedhosting.org>
#
# www.php.net/manual/en/migration53.deprecated.php
#
# Please note that there will be some false positives. Some PHP code is mixed
# with JS code. In JS 'split' is still a valid function.
#

deprecatedFunctions=(
    call_user_method
    call_user_method_array
    define_syslog_variables
    dl
    ereg
    ereg_replace
    eregi
    eregi_replace
    set_magic_quotes_runtime
    session_register
    session_unregister
    session_is_registered
    set_socket_blocking
    split
    spliti
    sql_regcase
    mysql_db_query
    mysql_escape_string
)

deprecatedIniDirectives=(
    define_syslog_variables
    register_globals
    register_long_arrays
    safe_mode
    magic_quotes_gpc
    magic_quotes_runtime
    magic_quotes_sybase
)

len=${#deprecatedFunctions[*]}
i=0

echo "Checking for deprectated functions ______________________________________"
echo ""

while [ $i -lt $len ]; do
    echo "  // checking for '${deprecatedFunctions[$i]}()'"
    grep -rn --color --include=*.php "^[^#]*[^a-zA-Z_]${deprecatedFunctions[$i]}[[:space:]]*(" *;
    echo ""
    let i++
done

len=${#deprecatedIniDirectives[*]}
i=0

echo "Checking for deprectated ini directives _________________________________"
echo ""

while [ $i -lt $len ]; do
    echo "  // checking for '${deprecatedIniDirectives[$i]}()'"
    grep -rn --color --include=*.php "ini_set[[:space:]]*(['|\"]${deprecatedIniDirectives[$i]}" *;
    echo ""
    let i++
done

Update, 11 may @10:31

Added check for optional space before function opening tags. Thanks Steffen!

Steffen 12 May 2011, 10:18
Thanks for this article and script. I think it might be a good idea to also check for a white space between function name and opening brace. Many programmers don't stick to the CGL.

Greets Steffen
Fabrizio 12 May 2011, 10:29
Nice! Thank you.

Bye, Fabrizio
lolli 12 May 2011, 17:25
Nice script, thanks! It only seemed that the --include=*.php statement didn't work for me, but script went fine after I removed this argument.
Sebastian 2 Sep 2011, 11:44
THX
Commenting is closed for this item