-
Notifications
You must be signed in to change notification settings - Fork 23
/
analyze_code.py
executable file
·73 lines (60 loc) · 2.24 KB
/
analyze_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
from pprint import pprint
import subprocess
import sys
# cmd = ['grep', '-irZ', '--include=*.php', 'function', 'modules/ProvVoipEnvia']
cmd = ['grep', '-irZ', '--include=*.php', 'function', 'app', 'database', 'modules', 'tests', 'nmsprime_testsuite.php']
keywords = ('public', 'protected', 'private')
print(' '.join(cmd))
print()
called = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = called.communicate()
result_raw = stdout.decode('utf-8').split('\n')
tmp = [r.split('\x00')[1].strip() for r in result_raw if r]
funcnames = [r.split('function')[1].strip().split('(')[0] for r in tmp if r.split(' ')[0] in keywords]
funcname_analysis = {
'camel_case': [],
'mixed_case': [],
'snake_case': [],
'single_word': [],
}
for f in funcnames:
tmp = f.lstrip('_')
if any(c.isupper() for c in tmp) and '_' in tmp:
funcname_analysis['mixed_case'].append(f)
elif any(c.isupper() for c in tmp):
funcname_analysis['camel_case'].append(f)
elif '_' in tmp:
funcname_analysis['snake_case'].append(f)
else:
funcname_analysis['single_word'].append(f)
funcname_analysis['camel_case'].sort()
funcname_analysis['mixed_case'].sort()
funcname_analysis['snake_case'].sort()
funcname_analysis['single_word'].sort()
pprint(funcname_analysis)
print()
print()
print('Function name analysis')
print('----------------------')
print('Total: {:5}'.format(len(funcnames)))
print('Camel case: {:5} ({:5.2f}%) (including overridden Laravel methods)'.format(
len(funcname_analysis['camel_case']),
round((100 * len(funcname_analysis['camel_case']) / len(funcnames)), 2)
))
print('Snake case: {:5} ({:5.2f}%)'.format(
len(funcname_analysis['snake_case']),
round((100 * len(funcname_analysis['snake_case']) / len(funcnames)), 2)
))
print('Single word: {:5} ({:5.2f}%)'.format(
len(funcname_analysis['single_word']),
round((100 * len(funcname_analysis['single_word']) / len(funcnames)), 2)
))
print('Mixed case: {:5} ({:5.2f}%) ({})'.format(
len(funcname_analysis['mixed_case']),
round((100 * len(funcname_analysis['mixed_case']) / len(funcnames)), 2),
', '.join(funcname_analysis['mixed_case'])
))
print()