Projeto

Geral

Perfil

find_construct_with_classname.sh

Alexandre Rocha Wendling Cassol, 24/06/2022 09:38 h

Baixar (4,78 KB)

 
1
#!/bin/bash
2

    
3
N="\033[0m"
4
R="\033[1;31m"
5
G="\033[0;32m"
6
B="\033[1;34m"
7
Y="\033[1;33m"
8

    
9
fmt='s#&[ \t]*\$#\&\$#g;s#=# = #g;s#,#, #g;s#(#& #g;s#)# &#g;s#[ \t]\+# #g2;s#( )#()#g'
10

    
11
flist=()
12
while (( "$#" )); do case "$1" in
13
	-x|--debug) debug=1;                                                    shift 1;;
14
	--no-save)  nosave=1;                                                   shift 1;;
15
	-f|--file)  flist+=("$2");                                              shift 2;;
16
	--)                                                              shift 1; break;;
17
	-*|--*=)    echo "Error: Unsupported flag $1" >&2;                       exit 1;;
18
	*) flist+=("$1");                                                       shift 1;;
19
esac; done
20

    
21
function run_construct_check() {
22
	echo -ne "${Y}";
23
	fname="$1"
24
	if [[ ! -f "${fname}" ]]; then echo "file ${fname} not found"; return; fi
25

    
26
	while read line; do
27
		class=$(php -r '
28
			$buf = explode( " ", trim( preg_replace( array( "/\t/", "/ +/", "/[\n\r{]*/" ), array( " ", " ", "" ), $argv[1] ) ) );
29
			if ( ( !isset( $buf[2] ) ) || $buf[2] == "extends" || $buf[2] == "implements" ) echo $buf[1].PHP_EOL;
30
		' "${line}")
31
		if [[ -z "${class}" ]]; then continue; fi
32

    
33
		range=$(cat -n "${fname}" | sed -n '/^[ ]*[0-9]\+[ \t]*class[ \t]\+'"${class}"'\r\?$/,/^[ ]*[0-9]\+[ \t]*class[ \t]/ {s#^[ \t]*##g;s#[ \t]#: p;}' |sed -n '1{s#:.*##g;p;};${s#:.*##g;p;};'|paste -d, -s)
34
		if [[ -z "${range}" ]]; then range=$(cat -n "${fname}" | sed -n '/^[ ]*[0-9]\+[ \t]*class[ \t]\+'"${class}"'[ \t{]/,/^[ ]*[0-9]\+[ \t]*class[ \t]/ {s#^[ \t]*##g;s#[ \t]#: p;}' |sed -n '1{s#:.*##g;p;};${s#:.*##g;p;};'|paste -d, -s); fi
35
	
36
		constr=$(cat -n "${fname}" | sed -n "${range}p" | grep -P '^[ ]*[0-9]+[ \t]*[\w ]*function[ \t]+__construct[ \t]*\('|sed 's#//.*##g')
37
		funct=$(cat -n "${fname}" | sed -n "${range}p" |grep -P '^[ ]*[0-9]+[ \t]*[\w ]*function[ \t]+'"${class}"'[ \t]*\('|sed 's#//.*##g')
38
		if [[ $(echo "${funct}"|wc -l) -ge 2 ]]; then
39
			funct=$(cat -n "${fname}" | sed -n "${range}p" | sed '/^ *[0-9]*[ \t]*\/\*/,/\*\// d' | grep -P '^[ ]*[0-9]+[ \t]*[\w ]*function[ \t]+'"${class}"'[ \t]*\('|sed 's#//.*##g')
40
		fi
41

    
42
		if [[ $(echo "${constr}"|wc -l) -ge 2 ]] || [[ $(echo "${funct}"|wc -l) -ge 2 ]]; then echo "ERROR: ${fname}";	return; fi
43

    
44
		if [[ -z "${constr}" ]] && [[ ! -z "${funct}" ]]; then
45
			lnum=$(echo "${funct}"|sed 's#^[ ]*##g'|grep -o '^[0-9]\+')
46
			sig=$(sed -n "${lnum}$(echo "${funct}"|grep -q '{'|| echo ',/{/')"'p' "${fname}"|sed '2,$ s#^[ \t]*##g;s#//.*##g'|sed ':a;N;$!ba;s#[\r\n]# #g'|sed "${fmt}"|sed 's#)[^)]*$#)#')
47
			proto=$(echo "${sig}"|sed "s#${class}[^(]*(#__construct(#"|sed "${fmt}")
48
			call=$(echo "${sig}"|sed 's#&\$#\$#'|sed 's#\$[A-Za-z0-9_]+#&\n#g'|sed "s#.*${class}[^(]*(#\$this->${class}(#"|sed 's#(.*##g')
49
			params=$(echo "${sig}"|sed 's#&\$#\$#'|sed 's#\$[A-Za-z0-9_]\+#\n&\n#g'|grep '^\$'|sed ':a;N;$!ba;s#[\r\n]#, #g')
50
			#dump="error_log(__FILE__.':'.__LINE__.' ${class}()'.PHP_EOL,3,'/tmp/log'); "
51
			if [[ ! -z "${params}" ]]; then params=" ${params} ";fi
52
			echo -n "${lnum}."; ec=""
53
			if [[ -z "${nosave}" ]]; then sed -i "${lnum}"' i\'"${proto}"' { '"${dump}"''"${call}"'('"${params}"'); }' "${fname}"; fi
54
		fi
55
	done < <(grep -Po '^[ \t]*class[ \t]+[^ /(<]+( [\w]+)*' "${fname}")
56
	if [[ -z "${ec}" ]]; then echo -ne "${N} "; fi
57
}
58

    
59
function run_construct_format() {
60
	echo -ne "${B}";
61
	fname="$1"
62
	if [[ ! -f "${fname}" ]]; then echo "file ${fname} not found"; return; fi
63

    
64
	while IFS= read -r line; do
65
		buf=$(php -r '
66
			$buf = explode( ":",$argv[1] );
67
			preg_match( "/^([ \t]*)(.*)function/", $buf[1], $matchs );
68
			$final   = preg_match( "/final/",   $matchs[2])? "final "   : "";
69
			$static  = preg_match( "/static/",  $matchs[2])? "static "  : "";
70
			$context = preg_match( "/private/", $matchs[2])? "private " : ( preg_match( "/protected/", $matchs[2] )? "protected " : "public " );
71
			echo $buf[0].PHP_EOL.$matchs[1].$context.$final.$static."function __construct(".PHP_EOL;
72
		' "${line}")
73
		if [[ -z "${buf}" ]]; then continue; fi
74
		lnum=$(echo "${buf}"|head -n 1)
75
		exp=$(echo "${buf}"|tail -n 1)
76
		if [[ ! "${line}" == "${lnum}:${exp}" ]]; then
77
			echo -n "${lnum}."; ec="";
78
			if [[ -z "${nosave}" ]]; then sed -i ${lnum}' s#^[^(]*(#'"${exp}"'#' "${fname}"; fi
79
		fi
80
	done < <(grep -Pno '^[\w\d\t ]*function[ \t]+__construct[ \t]*\(' "${fname}")
81

    
82
	if [[ -z "${ec}" ]]; then echo -ne "${N} "; fi
83
}
84

    
85
if [[ "${#flist[@]}" -eq 0 ]]; then
86
	echo -n 'Searching files... ';
87
	while IFS= read -r -d $'\0' line; do flist+=("${line}"); done < <(find -type f -iname '*.php' -print0)
88
	echo -e "${R}${#flist[@]}${N} found. done."
89
fi
90

    
91
if [[ ! -z "${debug}" ]]; then set -x; fi
92

    
93
for filename in "${flist[@]}"; do
94
	ec="n"
95
	echo -ne "${G}${filename}${N}: ";
96

    
97
	run_construct_check "${filename}";
98
	run_construct_format "${filename}";
99

    
100
	if [[ -z "${ec}" ]]; then echo -e "${N}";
101
	else echo -ne "\r\033[0K\r"; fi
102
done
103
echo -e "${N}";