-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.php
143 lines (95 loc) · 2.94 KB
/
check.php
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
<?php
$path = __DIR__.'/../../';
$files = getAllFiles($path);
$currentFile = $files[0];
$currentLine = 1;
foreach ($files as $file) {
$currentFile = $file;
$handle = fopen($path.$file, "r");
$currentLine = 1;
while (($line = fgets($handle)) !== false) {
++$currentLine;
checkForVarDump($line);
}
fclose($handle);
}
echo "SUCCESS";
/*******************/
/* FUNCTIONS */
/*******************/
function getAllFiles($path) {
$files = array();
$appPath = "app/controllers";
$ctrlPath = $path.'/'.$appPath;
$folders = scandir($ctrlPath);
foreach ($folders as $folder) {
if($folder !== "." && $folder !== "..") {
if(substr($folder, -4) === '.php') {
$files[] = $appPath.'/'.$folder;
} else {
$flFiles = scandir($ctrlPath.'/'.$folder);
foreach ($flFiles as $file) {
if($file !== "." && $file !== "..") {
$files[] = $appPath.'/'.$folder.'/'.$file;
}
}
}
}
}
return $files;
}
function errorFound($line, $dump) {
global $currentFile;
global $currentLine;
echo "Error in ".$currentFile." at line ".$currentLine."\n";
echo "Found ".$dump;
die;
}
function checkForDeb($line) {
$dump = strpos($line, 'deb(');
if(is_int($dump) && !dumpIsCommented($dump, $line)) { // Then we check if it is commented) {
errorFound($dump, $line);
}
}
function checkForVarDump($line) {
$dump = strpos($line, 'var_dump(');
if(is_int($dump)) { // If var_dump has been found
if(!dumpIsCommented($dump, $line)) { // Then we check if it is commented
errorFound($dump, $line);
} else {
checkForDeb($line);
}
} else {
checkForDeb($line);
}
}
function dumpIsCommented($dump, $line) {
/*
There are three case where a dump is excusable.
Case 1 : When it is commented in code (by finding // before the dump)
Case 2 : When there is a function statement before (example for function deb() in functions.php)
Case 3 : When the dump is flagged as authorized (in function deb, the var_dump is normal so flagged //dump_authorized)
*/
if($dump === 0) { // If the dump is first in line, we assert it is not commented
return false;
}
$searchCommentInLine = strpos($line, "//");
if(is_int($searchCommentInLine)) {
if($searchCommentInLine < $dump) {
return true;
}
}
$searchIsFunction = strpos($line, "function");
if(is_int($searchIsFunction)) {
if($searchIsFunction < $dump) {
return true;
}
}
$searchAuthorizedDump = strpos($line, "//dump_authorized");
if(is_int($searchAuthorizedDump)) {
if($searchAuthorizedDump > $dump) {
return true;
}
}
return false;
}