SeekquarrySeekquarry - Yioop Repo - Seekquarry

/ devlog / devscripts / check_deprecated.sh⤓
#!/bin/sh
# Finds what this PHP passes in silence and Chris's PHP reports.
#
#   sh devlog/devscripts/check_deprecated.sh src/library/Foo.php tests/FooTest.php
#   sh devlog/devscripts/check_deprecated.sh            (every php file that differs
#                                             from confirmed-head)
#
# The environment here runs 8.3; Chris runs 8.5. Anything deprecated in 8.4
# or 8.5 therefore passes every local run and lands on his machine as a wall
# of notices. Watching for these by eye failed three times, so they are
# looked for mechanically instead. Run this over every touched file as part
# of the audit, before cutting.
#
# Exits non-zero when anything is found.
WORK=${WORK:-/home/claude/work}
cd "$WORK" || exit 1
files="$*"
# The rules below are PHP's, so a file of any other kind is passed over
# rather than read as PHP. A shell script writing a default for a name
# is not the string form PHP deprecated, and reading it as one raised a
# fault that was not there.
if [ -n "$files" ]; then
    files=`echo "$files" | tr ' ' '\n' | grep '\.php$'`
fi
if [ -z "$files" ]; then
    files=`git diff confirmed-head..HEAD --name-only; git diff --name-only`
    files=`echo "$files" | sort -u | grep '\.php$'`
fi
if [ -z "$files" ]; then
    echo "check: no php files to look at"
    exit 0
fi
found=0
report() {
    if grep -n -E -e "$2" $files 2>/dev/null; then
        echo "check: $1"
        found=1
    fi
}
report "setAccessible does nothing since 8.1 and is deprecated in 8.5; \
delete the call" \
    '->setAccessible\('
report "a parameter defaulting to null must be marked as taking one: \
write ?int \$n = null" \
    'function [a-zA-Z_]+\((([^)?])*)(int|string|float|bool|array) [$][a-zA-Z_]+ = null'
report "E_STRICT names a level of error that no longer happens (8.4)" \
    '\bE_STRICT\b'
report "utf8_encode and utf8_decode are deprecated (8.2)" \
    '\butf8_(en|de)code\('
report 'write {$var} inside a string rather than ${var} (8.2)' \
    '\$\{[a-zA-Z_]'
report "a whole-number parameter is being handed something fractional; \
the value is cut down and the remainder lost (8.1)" \
    '\b(rand|mt_rand|random_int|str_repeat|array_fill)\([^;)]*(/|exp\(|sqrt\(|pow\(|log\(|fmod\(|microtime\(|M_PI)'
report "these calls have no effect and are deprecated; delete them" \
    '\b(imagedestroy|curl_close|xml_parser_free|finfo_close|shmop_close|openssl_pkey_free|openssl_x509_free|openssl_free_key)\('
# A csv call that leaves its escape character to the default is
# deprecated in 8.4, since that default will change. Every such call in
# Yioop names it as escape:, so a call line without the word is one that
# leaves it out. A tasklist reader in CrawlDaemon did, and Chris's unit
# run printed the notice three times.
csv=`grep -n -E '\b(str_getcsv|fgetcsv|fputcsv)\(' $files 2>/dev/null | \
    grep -v 'escape'`
if [ -n "$csv" ]; then
    echo "$csv"
    echo 'check: a csv call must give its escape character, such as'\
' escape: "" (8.4)'
    found=1
fi
# Taking apart false or null with list() or [...] = warns in 8.5:
# "Cannot use bool as array". Chris's unit run printed it from
# deleteGroupPage, where getGroupPageResourcesFolders gave false for a
# page with no folder. This names each statement that takes apart the
# result of a call that can give false, unless ?: or ?? gives an array
# in its place. A statement is read up to its semicolon, since such
# calls are often split over lines.
takes_apart=`awk '
    FNR == 1 { statement = ""; start = 0 }
    {
        if (statement == "") { start = FNR }
        statement = statement " " $0
        if ($0 ~ /;|[{}][ \t]*$/) {
            if (statement ~ /(list\(|^[ \t]*\[)[^;=]*\][ \t]*=|list\([^;=]*\)[ \t]*=/ &&
                statement ~ /(getGroupPageResourcesFolders|getimagesize|fgetcsv|parse_url|sscanf)\(/ &&
                statement !~ /\?:|\?\?/) {
                print FILENAME ":" start ":" statement
            }
            statement = ""
        }
    }' $files 2>/dev/null`
if [ -n "$takes_apart" ]; then
    echo "$takes_apart"
    echo "check: list() or [...] = takes apart a call that can give false,"\
' which warns in 8.5; add ?: [...] with the parts it should take'
    found=1
fi
if [ "$found" -eq 0 ]; then
    echo "check: nothing found in `echo $files | wc -w` file(s)"
fi
exit $found