-
Notifications
You must be signed in to change notification settings - Fork 0
/
codechecker.php
147 lines (120 loc) · 3.43 KB
/
codechecker.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
144
145
146
147
<?php
/**
* The class provides a basic tool to check whether a source code you
* work with does not keep disallowed expressions. It is very helpful
* while you are working on a big part of a system and you are just before
* releasing the work to a test environment.
*
* @author andrew@itma.pl
**/
namespace itma\code;
class Checker {
/**
* An absolute path to project
* @var $path
**/
public $path;
/**
* An array with pathes to exclude during the check is being done
* @var $excludePaths
**/
public $exclude = [];
/**
* An array with strings to check if they exist in the source code
* @var $strings
**/
public $strings = [];
/**
* Things to do before the object take off
* @var $strings
**/
public function init() {
if (!is_array($this->strings) || count($this->strings) == 0) {
throw new \Exception("You have to give a list of disallowed words to check if exist.");
}
if (empty($this->path) || !is_dir($this->path)) {
throw new \Exception("The given path to grep through is incorrect.");
}
}
/**
* Checks the given source code against disallowed words
* @return string
**/
public function check() {
$this->init();
$exclude = implode('| grep -v ', $this->exclude);
// Basic stats
$msg = "\nThe list of words with numbers\n";
$msg .= "==================================\n\n";
foreach ($this->strings as $string) {
$output = array();
exec("grep -r -o '{$string}' {$this->path} | grep -v {$exclude} | wc -l", $output);
if ($output[0] > 0) {
$msg .= "String: {$string} appears \033[31m{$output[0]}\033[0m times.\n";
} else {
$msg .= "String: {$string} appears \033[32m{$output[0]}\033[0m times.\n";
}
}
// Files
$msg .= "\nThe list of files containing disallowed words:\n";
$msg .= "==================================\n\n";
foreach ($this->strings as $string) {
$output = array();
exec("grep -r -l '{$string}' {$this->path} | grep -v {$exclude}", $output);
if (count($output) > 0) {
$msg .= "==================================\n";
$msg .= $string . "\n";
$msg .= "==================================\n";
foreach ($output as $file) {
$msg .= $file . "\n";
}
$msg .= "\n";
}
}
return $msg;
}
}
// Create an instance
$codeChecker = new \itma\code\Checker();
// Set the path to grep through
$codeChecker->path = '/your/path/to/source/code/';
// Skip these pathes
$codeChecker->exclude = [
'/your/path/to/source/code/vendor/lib/',
];
// Disallowed words
$codeChecker->strings = [
'1==0',
'1== 0',
'1 ==0',
'1 == 0',
'0 == 1',
'0==1',
'0 ==1',
'0== 1',
'print_r',
'var_dump',
'& false',
'& true',
'if(true)',
'if( true )',
'if( true)',
'if(true )',
'if(false)',
'if( false )',
'if( false)',
'if(false )',
'if (true)',
'if ( true )',
'if ( true)',
'if (true )',
'if (false)',
'if ( false )',
'if ( false)',
'if (false )',
'dupa',
'debug_backtrace',
];
// Check!
echo $codeChecker->check();
?>