22
22
import org .eclipse .core .runtime .QualifiedName ;
23
23
import org .eclipse .dltk .compiler .problem .DefaultProblemIdentifier ;
24
24
import org .eclipse .dltk .compiler .problem .IProblem ;
25
- import org .eclipse .dltk .compiler .problem .ProblemSeverities ;
26
25
import org .eclipse .dltk .compiler .problem .ProblemSeverity ;
27
26
import org .phpsrc .eclipse .pti .core .launching .OperatingSystem ;
28
27
import org .phpsrc .eclipse .pti .core .launching .PHPToolLauncher ;
45
44
46
45
public class PHPCodeSniffer extends AbstractPHPTool {
47
46
48
- public final static QualifiedName QUALIFIED_NAME = new QualifiedName (PHPCodeSnifferPlugin . PLUGIN_ID ,
49
- "phpCodeSnifferTool" );
47
+ public final static QualifiedName QUALIFIED_NAME = new QualifiedName (
48
+ PHPCodeSnifferPlugin . PLUGIN_ID , "phpCodeSnifferTool" );
50
49
private static PHPCodeSniffer instance ;
51
50
52
51
protected PHPCodeSniffer () {
@@ -60,12 +59,14 @@ public static PHPCodeSniffer getInstance() {
60
59
}
61
60
62
61
public IProblem [] parse (IFile file ) throws CoreException , IOException {
63
- PHPCodeSnifferPreferences prefs = PHPCodeSnifferPreferencesFactory .factory (file );
62
+ PHPCodeSnifferPreferences prefs = PHPCodeSnifferPreferencesFactory
63
+ .factory (file );
64
64
if (canParse (prefs .getIgnorePattern (), prefs .getFileExtensions (), file )) {
65
65
66
66
ArrayList <IProblem > list = new ArrayList <IProblem >(10 );
67
67
for (Standard standard : prefs .getStandards ()) {
68
- IProblem [] problems = parseOutput (new PHPSourceFile (file ), launchFile (file , prefs , standard ), prefs );
68
+ IProblem [] problems = parseOutput (new PHPSourceFile (file ),
69
+ launchFile (file , prefs , standard ), prefs );
69
70
for (IProblem problem : problems )
70
71
if (!list .contains (problem ))
71
72
list .add (problem );
@@ -77,7 +78,8 @@ public IProblem[] parse(IFile file) throws CoreException, IOException {
77
78
}
78
79
}
79
80
80
- protected boolean canParse (String ignorePattern , String [] fileExtensions , IFile file ) {
81
+ protected boolean canParse (String ignorePattern , String [] fileExtensions ,
82
+ IFile file ) {
81
83
boolean can = true ;
82
84
83
85
if (fileExtensions != null && fileExtensions .length > 0 ) {
@@ -100,7 +102,8 @@ protected boolean canParse(String ignorePattern, String[] fileExtensions, IFile
100
102
for (String pattern : patterns ) {
101
103
pattern = pattern .trim ();
102
104
if (pattern .length () > 0 ) {
103
- pattern = pattern .replace ("\\ " , "/" ).replace ("." , "\\ ." ).replace ("*" , ".*" ).replace ("?" , ".?" );
105
+ pattern = pattern .replace ("\\ " , "/" ).replace ("." , "\\ ." )
106
+ .replace ("*" , ".*" ).replace ("?" , ".?" );
104
107
Pattern p = Pattern .compile (pattern );
105
108
if (p .matcher (filePath ).matches ()) {
106
109
can = false ;
@@ -113,11 +116,13 @@ protected boolean canParse(String ignorePattern, String[] fileExtensions, IFile
113
116
return can ;
114
117
}
115
118
116
- protected String launchFile (IFile file , PHPCodeSnifferPreferences prefs , Standard standard ) {
119
+ protected String launchFile (IFile file , PHPCodeSnifferPreferences prefs ,
120
+ Standard standard ) {
117
121
118
122
String output = null ;
119
123
try {
120
- PHPToolLauncher launcher = getPHPToolLauncher (file .getProject (), prefs , standard );
124
+ PHPToolLauncher launcher = getPHPToolLauncher (file .getProject (),
125
+ prefs , standard );
121
126
output = launcher .launch (file );
122
127
} catch (Exception e ) {
123
128
Logger .logException (e );
@@ -129,7 +134,8 @@ protected String launchFile(IFile file, PHPCodeSnifferPreferences prefs, Standar
129
134
return output ;
130
135
}
131
136
132
- protected IProblem [] parseOutput (ISourceFile file , String output , PHPCodeSnifferPreferences prefs ) {
137
+ protected IProblem [] parseOutput (ISourceFile file , String output ,
138
+ PHPCodeSnifferPreferences prefs ) {
133
139
ArrayList <IProblem > problems = new ArrayList <IProblem >();
134
140
135
141
try {
@@ -147,10 +153,14 @@ protected IProblem[] parseOutput(ISourceFile file, String output, PHPCodeSniffer
147
153
parser .parse (new InputSource (new StringReader (output )));
148
154
149
155
Document doc = parser .getDocument ();
150
- problems .addAll (createProblemMarker (file , doc .getElementsByTagName ("error" ),
151
- ProblemSeverity .ERROR , tabWidth , prefs .getIgnoreSniffs ()));
152
- problems .addAll (createProblemMarker (file , doc .getElementsByTagName ("warning" ),
153
- ProblemSeverity .WARNING , tabWidth , prefs .getIgnoreSniffs ()));
156
+ problems .addAll (createProblemMarker (file ,
157
+ doc .getElementsByTagName ("error" ),
158
+ ProblemSeverity .ERROR , tabWidth ,
159
+ prefs .getIgnoreSniffs ()));
160
+ problems .addAll (createProblemMarker (file ,
161
+ doc .getElementsByTagName ("warning" ),
162
+ ProblemSeverity .WARNING , tabWidth ,
163
+ prefs .getIgnoreSniffs ()));
154
164
}
155
165
}
156
166
} catch (Exception e ) {
@@ -160,7 +170,8 @@ protected IProblem[] parseOutput(ISourceFile file, String output, PHPCodeSniffer
160
170
return problems .toArray (new IProblem [0 ]);
161
171
}
162
172
163
- protected ArrayList <IProblem > createProblemMarker (ISourceFile file , NodeList list , ProblemSeverity type , int tabWidth ,
173
+ protected ArrayList <IProblem > createProblemMarker (ISourceFile file ,
174
+ NodeList list , ProblemSeverity type , int tabWidth ,
164
175
String [] ignoreSniffs ) {
165
176
if (tabWidth <= 0 )
166
177
tabWidth = 2 ;
@@ -186,37 +197,45 @@ protected ArrayList<IProblem> createProblemMarker(ISourceFile file, NodeList lis
186
197
continue ;
187
198
}
188
199
189
- int lineNr = Integer .parseInt (attr .getNamedItem ("line" ).getTextContent ());
190
- int column = Integer .parseInt (attr .getNamedItem ("column" ).getTextContent ());
200
+ int lineNr = Integer .parseInt (attr .getNamedItem ("line" )
201
+ .getTextContent ());
202
+ int column = Integer .parseInt (attr .getNamedItem ("column" )
203
+ .getTextContent ());
191
204
int lineStart = file .lineStart (lineNr );
192
205
193
206
// calculate real column with tabs
194
207
if (column > 1 )
195
208
lineStart += (column - 1 - (file .lineStartTabCount (lineNr ) * (tabWidth - 1 )));
196
209
197
- System .out .println (IProblem .Syntax );
198
- System .out .println (DefaultProblemIdentifier .decode (IProblem .Syntax ));
199
- problems .add (new CodeSnifferProblem (file .getFile ().getFullPath ().toOSString (), item .getTextContent (),
200
- DefaultProblemIdentifier .decode (IProblem .Syntax ), new String [0 ], type , lineStart , file .lineEnd (lineNr ), lineNr , column , source ));
210
+ problems .add (new CodeSnifferProblem (file .getFile ().getFullPath ()
211
+ .toOSString (), item .getTextContent (),
212
+ DefaultProblemIdentifier .decode (IProblem .Syntax ),
213
+ new String [0 ], type , lineStart , file .lineEnd (lineNr ),
214
+ lineNr , column , source ));
201
215
}
202
216
203
217
return problems ;
204
218
}
205
219
206
- protected PHPToolLauncher getPHPToolLauncher (IProject project , PHPCodeSnifferPreferences prefs , Standard standard ) {
220
+ protected PHPToolLauncher getPHPToolLauncher (IProject project ,
221
+ PHPCodeSnifferPreferences prefs , Standard standard ) {
207
222
PHPToolLauncher launcher ;
208
223
try {
209
- launcher = (PHPToolLauncher ) project .getSessionProperty (QUALIFIED_NAME );
224
+ launcher = (PHPToolLauncher ) project
225
+ .getSessionProperty (QUALIFIED_NAME );
210
226
if (launcher != null ) {
211
- launcher .setCommandLineArgs (getCommandLineArgs (standard , prefs .getTabWidth ()));
227
+ launcher .setCommandLineArgs (getCommandLineArgs (standard ,
228
+ prefs .getTabWidth ()));
212
229
return launcher ;
213
230
}
214
231
} catch (CoreException e ) {
215
232
Logger .logException (e );
216
233
}
217
234
218
- launcher = new PHPToolLauncher (QUALIFIED_NAME , getPHPExecutable (prefs .getPhpExecutable ()), getScriptFile (),
219
- getCommandLineArgs (standard , prefs .getTabWidth ()), getPHPINIEntries (prefs , project , standard ));
235
+ launcher = new PHPToolLauncher (QUALIFIED_NAME ,
236
+ getPHPExecutable (prefs .getPhpExecutable ()), getScriptFile (),
237
+ getCommandLineArgs (standard , prefs .getTabWidth ()),
238
+ getPHPINIEntries (prefs , project , standard ));
220
239
221
240
launcher .setPrintOuput (prefs .isPrintOutput ());
222
241
@@ -229,21 +248,25 @@ protected PHPToolLauncher getPHPToolLauncher(IProject project, PHPCodeSnifferPre
229
248
return launcher ;
230
249
}
231
250
232
- private INIFileEntry [] getPHPINIEntries (PHPCodeSnifferPreferences prefs , IProject project , Standard standard ) {
251
+ private INIFileEntry [] getPHPINIEntries (PHPCodeSnifferPreferences prefs ,
252
+ IProject project , Standard standard ) {
233
253
234
- IPath [] includePaths = PHPCodeSnifferPlugin .getDefault ().getPluginIncludePaths (project );
254
+ IPath [] includePaths = PHPCodeSnifferPlugin .getDefault ()
255
+ .getPluginIncludePaths (project );
235
256
236
257
if (standard .custom ) {
237
258
IPath [] tmpIncludePaths = new IPath [includePaths .length + 2 ];
238
- System .arraycopy (includePaths , 0 , tmpIncludePaths , 2 , includePaths .length );
259
+ System .arraycopy (includePaths , 0 , tmpIncludePaths , 2 ,
260
+ includePaths .length );
239
261
tmpIncludePaths [0 ] = new Path (standard .path );
240
262
tmpIncludePaths [1 ] = new Path (standard .path ).removeLastSegments (1 );
241
263
includePaths = tmpIncludePaths ;
242
264
}
243
265
244
266
INIFileEntry [] entries ;
245
267
if (includePaths .length > 0 ) {
246
- entries = new INIFileEntry [] { INIFileUtil .createIncludePathEntry (includePaths ) };
268
+ entries = new INIFileEntry [] { INIFileUtil
269
+ .createIncludePathEntry (includePaths ) };
247
270
} else {
248
271
entries = new INIFileEntry [0 ];
249
272
}
@@ -252,13 +275,15 @@ private INIFileEntry[] getPHPINIEntries(PHPCodeSnifferPreferences prefs, IProjec
252
275
}
253
276
254
277
public static IPath getScriptFile () {
255
- return PHPCodeSnifferPlugin .getDefault ().resolvePluginResource ("/php/tools/phpcs.php" );
278
+ return PHPCodeSnifferPlugin .getDefault ().resolvePluginResource (
279
+ "/php/tools/phpcs.php" );
256
280
}
257
281
258
282
private String getCommandLineArgs (Standard standard , int tabWidth ) {
259
283
260
284
String args = "--report=xml --standard="
261
- + (standard .custom ? OperatingSystem .escapeShellFileArg (standard .path ) : OperatingSystem
285
+ + (standard .custom ? OperatingSystem
286
+ .escapeShellFileArg (standard .path ) : OperatingSystem
262
287
.escapeShellArg (standard .name ));
263
288
264
289
if (tabWidth > 0 )
0 commit comments