-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphpcs_handler.js
174 lines (145 loc) · 6.14 KB
/
phpcs_handler.js
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
163
164
165
166
167
168
169
170
171
172
173
174
// @see https://cloud9-sdk.readme.io/v0.1/docs/existing-tools
define(function(require, exports, module) {
var baseHandler = require("plugins/c9.ide.language/base_handler");
var handler = module.exports = Object.create(baseHandler);
var workerUtil = require("plugins/c9.ide.language/worker_util");
/***** Overriding handler methods *****/
// Register the language.
handler.handlesLanguage = function( language ) {
return language === "php";
};
// Set up the analyzer.
handler.analyze = function( value, ast, callback ) {
// Get the full path to the plugin directory. Might need to find a better way to do this.
var handlerSource = handler.$source.split("/");
var pluginPath = handler.workspaceDir.replace("workspace", ".c9") + "/" + handlerSource[0] + "/" + handlerSource[1] + "/";
var phpcsPath = pluginPath + "server/phpcs/";
// Set options to use with workerUtil.execAnalysis.
var options = {
mode: "stdin",
json: true,
maxCallInterval: 500,
timeout: 10000,
maxBuffer: 1024 * 500, // Increase the stdout max buffer to 500KB.
args: [
phpcsPath + "phpcs.phar",
"-q",
"-w",
"--severity=1",
"--report=json",
"--runtime-set", "ignore_errors_on_exit", "1",
"--runtime-set", "ignore_warnings_on_exit", "1",
"--runtime-set", "installed_paths", phpcsPath + "rules",
"--runtime-set", "encoding", "utf-8",
"--runtime-set", "report_width", "auto",
"--runtime-set", "testVersion", "5.2-",
// "--runtime-set", "severity", "0",
// "--tab-width=4",
"--standard=WordPress-Extra,WordPress-Docs,PHPCompatibilityWP"
],
};
// The callback used to generate the markers.
function generateMarkers ( err, stdout, stderr ) {
// If an error happened while retrieving data.
if (err && err.code !== 255) return callback(err);
// Parse error messages and generate markers.
var markers = [];
if ( typeof stdout.files.STDIN.messages !== 'undefined' ) {
var messages = stdout.files.STDIN.messages;
messages.forEach( function parseMessage( msg ) {
var marker = getMarker(msg); // Generate the marker.
markers.push(marker);
});
}
callback( null, markers );
}
// Now run execAnalysis.
workerUtil.execAnalysis( "php", options, generateMarkers );
};
/***** Helper Functions *****/
// Get a marker to display based on an error message generated by PHPCS.
function getMarker( msg ) {
// Defining error severity.
var severity = getSeverity( msg );
// Defining message type: error or warning?
var type = msg.type.match(/error/i) ? "🔴" : "🔶";
// Can the error be fixed with PHPCBF? Please note that even though it'll display the error can be fixed with PHPCF, nothing has been implemented yet in order to do it automatically.
var quickfix = msg.fixable ? " ✔" : "";
// Defining the position of the error.
var pos = getPosition( msg );
// Defining from which coding standards this error has been found between WPCS and PHPCompatibility.
var source = getSource( msg );
// Defining the text to display in the marker.
var message = msg.message.replace(/\.\s*$/, ""); // Strip the last '.' of the string if any.
var text = type + " " + severity + " " + source + " " + pos + quickfix + " " + message + ".";
// Generating the marker with all the previous data combined.
var marker = {
pos: { sl: msg.line - 1, el: msg.line - 1, sc: msg.column - 1, ec: msg.column + 1 },
message: text,
level: 'warning',
quickfix: msg.fixable
};
return marker;
}
function getSeverity( msg ) {
// Defining array of numbers to be replaced with more distinctive unicode characters.
var severityLevels = [
"⓪",
"➀",
"➁",
"➂",
"➃",
"➄",
"➅",
"➆",
"➇",
"➈",
"➉"
];
var severity = msg.severity;
if ( typeof severityLevels[msg.severity] !== 'undefined' ) {
severity = 5 !== severity ? severityLevels[msg.severity] : ""; // Hide the default severity (5).
}
return severity;
}
function getPosition( msg ) {
// Defining array of numbers to be replaced with more distinctive unicode characters.
var unicodeNumbers = [
"𝟎",
"𝟏",
"𝟐",
"𝟑",
"𝟒",
"𝟓",
"𝟔",
"𝟕",
"𝟖",
"𝟗"
];
var line = replaceKeyWithValue( msg.line, unicodeNumbers );
var col = replaceKeyWithValue( msg.column, unicodeNumbers );
var pos = "(" +line + ":" + col + ")";
return pos;
}
function getSource( msg ) {
var source = msg.source;
if ( source.match(/(wordpress)|(wp)|(squiz)|(generic)|(pear)|(psr)/i) || msg.message.match(/(wordpress)|(wp)/i) ) {
source = "𝗪𝗣𝗖𝗦";
} else if ( source.match(/compatibility/i) || msg.message.match(/compatibility/i) ) {
source = "𝗣𝗛𝗣𝗖𝗼𝗺𝗽𝗮𝘁𝗶𝗯𝗶𝗹𝗶𝘁𝘆";
} else {
source = "[" + source.substr(0, source.indexOf('.')) + "]";
}
return source;
}
// In a string, replace all occurences of keys in an array with their corresponding value.
function replaceKeyWithValue( str, arr ) {
if ( typeof str !== 'string' ) {
str = str.toString();
}
for ( var key in arr ) {
str = str.split(key).join(arr[key]);
}
return str;
}
});