com.github.rwhogg.git_vcr.review.php.PhpmdReviewTool.java Source code

Java tutorial

Introduction

Here is the source code for com.github.rwhogg.git_vcr.review.php.PhpmdReviewTool.java

Source

/*
PhpmdReviewTool.java: Reviews a PHP file using Phpmd
       
Copyright  2015 Bob W. Hogg. All Rights Reserved.
       
This file is part of Git-VCR.
       
Git-VCR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
       
Git-VCR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
       
You should have received a copy of the GNU General Public License
along with Git-VCR.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.rwhogg.git_vcr.review.php;

import com.github.rwhogg.git_vcr.review.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import org.apache.commons.io.*;

/**
 * PhpmdReviewTool reviews a PHP file using phpmd
 */
public class PhpmdReviewTool extends ProcessBasedReviewTool {
    @Override
    public List<String> getResults(File file) throws ReviewFailedException {
        // delete the cache first
        Path pathToCache = FileSystems.getDefault().getPath(System.getProperty("user.home"), ".pdepend");
        try {
            FileUtils.deleteQuietly(pathToCache.toFile());
        } catch (Exception e) {
        }

        List<Object> rules = configuration.getList("rules");
        String commandTemplate = "\"%s\" text \"%s\"";
        List<String> results;
        try {
            StringBuffer ruleStringBuffer = new StringBuffer();
            for (Object rule : rules) {
                ruleStringBuffer.append((String) rule + ",");
            }
            String ruleString = ruleStringBuffer.toString();
            results = runProcess(String.format(commandTemplate, file.getAbsolutePath(),
                    ruleString.substring(0, ruleString.length() - 1)));
        } catch (IOException e) {
            throw new ReviewFailedException(e.getLocalizedMessage(), this, file.getAbsolutePath());
        }

        // first result is always an empty line
        if ((results != null) && (results.size() > 0)) {
            List<String> copy = new LinkedList<>();
            copy.addAll(results);
            copy.remove(0);
            results = copy;
        }
        return results;
    }

    @Override
    public String getName() {
        return "phpmd";
    }

    @Override
    public String getExecutableName() {
        return getName();
    }
}