class Danger::DangerPhpmd

[Danger](danger.systems/ruby/) plugin for [phpmd](phpmd.org/).

@example Run phpmd and send warn comment.

phpmd.binary_path = "vendor/bin/phpmd"
phpmd.run ruleset: "rulesets.xml"

@see ktakayama/danger-phpmd @tags phpmd

Attributes

binary_path[RW]

phpmd path @return [String]

Public Instance Methods

run(options) click to toggle source

Execute phpmd @return [void]

# File lib/phpmd/plugin.rb, line 24
def run(options)
  return if target_files.empty?

  tmpfile = Tempfile.open("phpmd_result")
  Open3.capture3(cmd_path, "--reportfile", tmpfile.path, target_files.join(","), "json", options[:ruleset])
  results = parse(tmpfile.read)
  results.each do |result|
    warn(result[:message], file: result[:file], line: result[:line])
  end
end

Private Instance Methods

cmd_path() click to toggle source
# File lib/phpmd/plugin.rb, line 37
def cmd_path
  return binary_path if binary_path

  File.exist?("./vendor/bin/phpmd") ? "./vendor/bin/phpmd" : "phpmd"
end
parse(json) click to toggle source
# File lib/phpmd/plugin.rb, line 43
def parse(json)
  array = JSON.parse json
  return if array.empty?

  path = "#{Dir.pwd}/"

  results = []
  array["files"].each do |line|
    file = line["file"].sub(path, "")
    line["violations"].each do |violation|
      results << {
        message: violation["description"],
        file: file,
        line: violation["beginLine"]
      }
    end
  end
  results
end
target_files() click to toggle source
# File lib/phpmd/plugin.rb, line 63
def target_files
  ((git.added_files + (git.modified_files - git.deleted_files)) - git.renamed_files.map { |r| r[:before] } + git.renamed_files.map { |r| r[:after] }).uniq
end