#!/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
# 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 ${NAME:-default} 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)\('
if [ "$found" -eq 0 ]; then
    echo "check: nothing found in `echo $files | wc -w` file(s)"
fi
exit $found
