-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_stats.php
More file actions
162 lines (143 loc) · 4.43 KB
/
code_stats.php
File metadata and controls
162 lines (143 loc) · 4.43 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* A very simple code analysis stored in a single php file.
*
* Edit The variables just below to customize to your needs.
*/
// Variables. Change these to meet your needs.
$file_types = array('php','js','scss');
$skip_directories = array('.git', 'files', 'external', 'scripts');
$starting_directory = '../';
// Initialize. No need to change anything below this line.
$stats = array();
$stats['gen'] = array();
$stats['gen']['commented_lines'] = 0;
$stats['gen']['blank_lines'] = 0;
$stats['gen']['bracket_lines'] = 0;
$stats['gen']['comment_blocks'] = 0;
$stats['gen']['classes'] = 0;
$stats['gen']['functions'] = 0;
$stats['included_files'] = array();
$stats['excluded_files'] = array();
$stats['skip'] = $skip_directories;
$stats['file_types'] = '(' . implode('|', $file_types) . ')';
// Execute.
$totalLines = countLines($starting_directory, $stats);
// Print results.
echo 'Total lines: ' . $totalLines . '<br />';
echo 'Adjusted lines: ' . ($totalLines - $stats['gen']['commented_lines'] - $stats['gen']['blank_lines'] - $stats['gen']['bracket_lines']) . '<br />';
foreach($stats['gen'] as $key=>$val)
{
echo ucfirst($key) . ": " . $val . "<br>";
}
echo "<br />";
echo 'Included Files (' . count($stats['included_files']) . '): <br />';
foreach($stats['included_files'] as $file_name)
{
echo $file_name . '<br />';
}
echo "<br />";
echo 'Excluded Files & Directories (' . count($stats['excluded_files']) . '): <br />';
foreach($stats['excluded_files'] as $file_name)
{
echo $file_name . '<br />';
}
function countLines($dir, &$stats)
{
$lineCounter = 0;
$dirHandle = opendir($dir);
$path = realpath($dir);
$nextLineIsComment = false;
if($dirHandle)
{
while($file = readdir($dirHandle))
{
if(is_dir($path."/".$file) && ($file !== '.' && $file !== '..') && !in_array($file, $stats['skip']))
{
$lineCounter += countLines($path . '/' . $file, $stats);
}
elseif(in_array($file, $stats['skip']))
{
$stats['excluded_files'][] = $path . '/' . $file;
}
elseif($file !== '.' && $file !== '..')
{
// Check if we have a valid file
$ext = _findExtension($file);
if(preg_match("/" . $stats['file_types'] . "$/i", $ext))
{
$realFile = realpath($path)."/".$file;
$fileArray = file($realFile);
// Check content of file:
for($i=0; $i<count($fileArray); $i++)
{
if($nextLineIsComment)
{
$stats['gen']['commented_lines']++;
// Look for the end of the comment block
if(strpos($fileArray[$i], '*/'))
{
$nextLineIsComment = false;
}
}
else
{
// Look for a function
if(strpos($fileArray[$i], 'function'))
{
$stats['gen']['functions']++;
}
// Look for a commented line
if(strpos(trim($fileArray[$i]), '//') === 0)
{
$stats['gen']['commented_lines']++;
}
// Look for a class
if(substr(trim($fileArray[$i]), 0, 5) == 'class')
{
$stats['gen']['classes']++;
}
// Look for a comment block
if(strpos($fileArray[$i], '/*'))
{
$nextLineIsComment = true;
$stats['gen']['commented_lines']++;
$stats['gen']['comment_blocks']++;
}
//Look for a blank line
if(trim($fileArray[$i]) == '')
{
$stats['gen']['blank_lines']++;
}
// Look for lines that have an open or close bracket and nothing else.
if (trim($fileArray[$i]) == '{' || trim($fileArray[$i]) == '}')
{
$stats['gen']['bracket_lines']++;
}
}
}
$lineCounter += count($fileArray);
// Mark as an included file.
$stats['included_files'][] = $path . '/' . $file;
}
else
{
$stats['excluded_files'][] = $path . '/' . $file;
}
}
}
}
else
{
echo 'Could not enter folder: ' . $dir;
}
return $lineCounter;
}
function _findExtension($filename)
{
$filename = strtolower($filename) ;
$exts = preg_split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}