Skip to content

Commit cb90ec2

Browse files
author
skiera
committed
Close 60: Add validator
1 parent 2343120 commit cb90ec2

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

plugin.xml

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
<attribute name="severity"/>
1515
<super type="org.eclipse.core.resources.problemmarker"/>
1616
</extension>
17+
<extension
18+
point="org.eclipse.wst.validation.validatorV2"
19+
id="PHPUnitAutomaticTestExecutionValidatorForPHP"
20+
name="PHPDepend Metrics Validator (for PHP Files)">
21+
<validator
22+
class="org.phpsrc.eclipse.pti.tools.phpdepend.validator.PHPDependValidator"
23+
build="false"
24+
manual="false"
25+
markerId="org.phpsrc.eclipse.pti.tools.phpdepend.validator.phpToolPHPDependMarker"
26+
sourceid="org.eclipse.php.core.phpsource"
27+
version="1">
28+
<include>
29+
<rules>
30+
<contentType exactMatch="true" id="org.eclipse.php.core.phpsource"></contentType>
31+
</rules>
32+
</include>
33+
<group id="org.eclipse.wst.sse.core.structuredModelGroup" />
34+
</validator>
35+
</extension>
1736
<extension
1837
point="org.eclipse.ui.preferencePages">
1938
<page
@@ -127,5 +146,4 @@
127146
<viewShortcut id="org.phpsrc.eclipse.pti.tools.phpdepend.ui.views.metricrunnercharts" />
128147
</perspectiveExtension>
129148
</extension>
130-
131149
</plugin>
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2010 Sven Kiera
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*******************************************************************************/
8+
19
package org.phpsrc.eclipse.pti.tools.phpdepend.validator;
210

11+
import org.eclipse.core.resources.IFile;
12+
import org.eclipse.core.resources.IMarker;
313
import org.eclipse.core.resources.IResource;
14+
import org.eclipse.core.runtime.CoreException;
415
import org.eclipse.core.runtime.IProgressMonitor;
16+
import org.eclipse.dltk.compiler.problem.IProblem;
517
import org.eclipse.wst.validation.AbstractValidator;
618
import org.eclipse.wst.validation.ValidationResult;
719
import org.eclipse.wst.validation.ValidationState;
20+
import org.phpsrc.eclipse.pti.tools.phpdepend.IPHPDependConstants;
21+
import org.phpsrc.eclipse.pti.tools.phpdepend.core.PHPDepend;
22+
import org.phpsrc.eclipse.pti.ui.Logger;
823

924
public class PHPDependValidator extends AbstractValidator {
10-
public ValidationResult validate(IResource resource, int kind, ValidationState state,
11-
IProgressMonitor monitor) {
25+
public ValidationResult validate(IResource resource, int kind, ValidationState state, IProgressMonitor monitor) {
26+
// process only PHP files
27+
if (resource.getType() != IResource.FILE) {
28+
return null;
29+
}
1230

1331
ValidationResult result = new ValidationResult();
32+
validateFile(result, (IFile) resource, kind);
1433
return result;
1534
}
35+
36+
protected void validateFile(ValidationResult result, IFile file, int kind) {
37+
// remove the markers currently existing for this resource
38+
// in case of project/folder, the markers are deleted recursively
39+
try {
40+
file.deleteMarkers(IPHPDependConstants.VALIDATOR_PHP_DEPEND_MARKER, false, IResource.DEPTH_INFINITE);
41+
} catch (CoreException e) {
42+
e.printStackTrace();
43+
}
44+
45+
PHPDepend pDepend = PHPDepend.getInstance();
46+
IProblem[] problems;
47+
try {
48+
problems = pDepend.validateResource(file);
49+
for (IProblem problem : problems) {
50+
IMarker marker = file.createMarker(IPHPDependConstants.VALIDATOR_PHP_DEPEND_MARKER);
51+
marker.setAttribute(IMarker.PROBLEM, true);
52+
marker.setAttribute(IMarker.LINE_NUMBER, problem.getSourceLineNumber());
53+
54+
if (problem.isWarning()) {
55+
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
56+
result.incrementWarning(1);
57+
} else {
58+
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
59+
result.incrementError(1);
60+
}
61+
marker.setAttribute(IMarker.CHAR_START, problem.getSourceStart());
62+
marker.setAttribute(IMarker.CHAR_END, problem.getSourceEnd());
63+
marker.setAttribute(IMarker.MESSAGE, problem.getMessage());
64+
}
65+
} catch (CoreException e) {
66+
Logger.logException(e);
67+
}
68+
}
1669
}

0 commit comments

Comments
 (0)