Created
October 11, 2020 18:01
-
-
Save adduc/34e74e9b79b550c20c6f23d89a6b08d4 to your computer and use it in GitHub Desktop.
Naive approach to finding unused files, classes, and functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <dir>" | |
exit 1 | |
fi | |
DIR="$(realpath $1)" | |
find_unused_classes() | |
{ | |
echo "Finding Suspected Unused Classes" | |
LOG_LIST=class.log | |
LOG_UNUSED=class.unused.log | |
if [ ! -f "$LOG_LIST" ]; then | |
grep -IiP '^\s{0,}class\s{1,}[a-zA-Z0-9]+' $DIR -r --exclude-dir=vendor \ | |
--include="*.php" --include="*.ctp" \ | |
> $LOG_LIST | |
fi | |
truncate -s0 "$LOG_UNUSED" | |
while read LINE; do | |
CLASS=$(echo "$LINE" | sed -re 's/^[^:]+:\s{0,}class\s{1,}([^ ]+)(\s{1,}.*)?$/\1/') | |
FILE=$(echo "$LINE" | sed -e 's_\(.*\):.*_\1_') | |
REFERENCES=$(grep -m 1 -Ii "$CLASS" $DIR -r --include="*.php" --include="*.ctp" | grep -iP ':\s{0,}class\s{1,}[a-zA-Z0-9]+' -v | head -n 1) | |
# If grep returns with no matches, output the file as potentially unused. | |
if [ -z "$REFERENCES" ]; then | |
echo "$CLASS in $FILE" >> "$LOG_UNUSED" | |
fi | |
done < $LOG_LIST | |
} | |
find_unused_files() | |
{ | |
echo "Finding Suspected Unused Files" | |
LOG_LIST=files.log | |
LOG_UNUSED=files.unused.log | |
if [ ! -f "$LOG_LIST" ]; then | |
find $BASE_DIR -type f -regextype awk \ | |
-regex '.*\.(php|ctp)' \ | |
-not -iregex '.*/(vendor|\.hg|\.git)/.*' \ | |
| sort > $LOG_LIST | |
fi | |
truncate -s0 "$LOG_UNUSED" | |
while read LINE; do | |
BASE=$(basename "$LINE") | |
FILE=$(echo "$BASE" | sed -r 's/([^.]+).*/\1/') | |
REFERENCES=$(grep -m 1 -Ii "$FILE" $DIR -r --include="*.php" --include="*.ctp" --exclude-dir=vendor --exclude="$BASE") | |
# If grep returns with no matches, output the file as potentially unused. | |
if [ -z "$REFERENCES" ]; then | |
echo "$LINE" >> "$LOG_UNUSED" | |
fi | |
done < $LOG_LIST | |
} | |
find_unused_functions() | |
{ | |
echo "Finding Suspected Unused Functions" | |
LOG_LIST=functions.log | |
LOG_UNUSED=functions.unused.log | |
if [ ! -f "$LOG_LIST" ]; then | |
grep -IiP '\s{0,}function\s{1,}[a-zA-Z0-9]+' $DIR -r --exclude-dir=vendor \ | |
--include="*.php" --include="*.ctp" \ | |
> $LOG_LIST | |
fi | |
truncate -s0 "$LOG_UNUSED" | |
while read LINE; do | |
FUNCTION=$(echo "$LINE" | sed -re 's/.*\s{0,}function\s{1,}([^ (]+).*([ (])?/\1/') | |
FILE=$(echo "$LINE" | sed -re 's/^([^:]+):.*/\1/') | |
REFERENCES=$(grep -Ii "$FUNCTION" $DIR -r --exclude-dir=vendor --include="*.php" --include="*.ctp" | grep -iP 'function\s{1,}[a-zA-Z0-9]+' -v | head -n 1) | |
# If grep returns with no matches, output the file as potentially unused. | |
if [ -z "$REFERENCES" ]; then | |
echo "$FUNCTION in $FILE" >> "$LOG_UNUSED" | |
fi | |
done < $LOG_LIST | |
} | |
main() | |
{ | |
find_unused_classes | |
find_unused_files | |
find_unused_functions | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a cleaned up version of a script I wrote and used frequently at the first few jobs I had in the tech industry. Since PHP 7, there have been multiple static analysis tools released that perform a better job of identifying unused features.