-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckDebugTask.php
More file actions
138 lines (119 loc) · 3.53 KB
/
CheckDebugTask.php
File metadata and controls
138 lines (119 loc) · 3.53 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
<?php
require_once "phing/Task.php";
class CheckDebugTask extends Task {
/**
* The message passed in the buildfile.
*/
private $inputFile = null;
private $outputFile = null;
private $pattern = null;
// Supposed to do this according to phing docs...?
// private $taskname = 'checkdebug';
/**
* The setter for the attribute "filepath"
*/
public function setInputFile($path) {
$this->inputFile = $path;
}
public function setOutputFile($path) {
$this->outputFile = $path;
}
public function setPattern($pattern) {
$this->pattern = (string) $pattern;
}
/**
* The init method: Do init steps.
*/
public function init() {
// nothing to do here
}
/**
* The main entry point method.
*/
public function main() {
// Prepare some variables
$pattern = $this->pattern;
$line_number = 1;
$errors = array();
// Open the input and output files
$handle = fopen($this->inputFile, 'r');
// If we have a pattern and file we can start reading
if ($handle && $pattern) {
while (($line = fgets($handle)) !== false) {
// process the line read.
$line = trim($line);
if (preg_match("/$pattern/", $line)) {
// Check if it's commented
if (preg_match("/\/\/.*$pattern|\/\*.*$pattern.*\*\/|\*.*$pattern/", $line)) {
$this->log("Commented debug code detected: $line [line:$line_number]");
$errors[] = array(
'line' => $line_number,
'column' => 1,
'severity' => 'warning',
'message' => "Commented debug code detected: $line",
'source' => 'CheckDebugTask',
);
}
else {
$this->log("Uncommented debug code detected: $line [line:$line_number]");
$errors[] = array(
'line' => $line_number,
'column' => 1,
'severity' => 'error',
'message' => "Uncommented debug code detected: $line",
'source' => 'CheckDebugTask',
);
}
}
$line_number++;
}
}
else {
throw new BuildException("Unable to read file: {$this->inputFile}");
}
// If there are errors we need to add them to our checkstyle output xml
if ($errors) {
// First get the SimpleXML Document
$xml = $this->getSimpleXML();
// We'll add a new <file> element
$output = $xml->addChild('file');
$output->addAttribute('name', $this->inputFile);
// Loop through the errors and add the error element
foreach ($errors as $error) {
$item = $output->addChild('error');
foreach ($error as $attribute => $value) {
$item->addAttribute($attribute, $value);
}
}
$this->outputXML($xml);
}
// Close the file
fclose($handle);
}
/**
* Prepares the XML output
* @return [type] [description]
*/
private function getSimpleXML() {
// First check if the outputFile exists
if (file_exists($this->outputFile)) {
$xml = simplexml_load_file($this->outputFile);
}
else {
$xml = simplexml_load_string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><checkstyle></checkstyle>");
}
return $xml;
}
/**
* Helper function to format the xml output and save the file
*/
private function outputXML($simpleXML) {
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXML->asXML());
$dom->saveXML();
$dom->save($this->outputFile);
}
}
?>