Class: Pronto::Phpmd

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/phpmd.rb

Instance Method Summary collapse

Constructor Details

#initialize(patches, commit = nil) ⇒ Phpmd

Returns a new instance of Phpmd.



7
8
9
10
11
12
# File 'lib/pronto/phpmd.rb', line 7

def initialize(patches, commit = nil)
  super

  @executable = ENV['PRONTO_PHPMD_EXECUTABLE'] || 'phpmd'
  @ruleset = ENV['PRONTO_PHPMD_RULESET'] || 'cleancode,codesize,controversial,design,naming,unusedcode'
end

Instance Method Details

#inspect(patch) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pronto/phpmd.rb', line 26

def inspect(patch)
  path = patch.new_file_full_path.to_s
  run_phpmd(path).map do |offence|
    patch.added_lines.select { |line| line.new_lineno == offence[:line] }
      .map { |line| new_message(offence, line) }
  end
end

#new_message(offence, line) ⇒ Object



50
51
52
53
54
55
# File 'lib/pronto/phpmd.rb', line 50

def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  level = :warning

  Message.new(path, line, level, offence[:msg], nil, self.class)
end

#php_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pronto/phpmd.rb', line 57

def php_file?(path)
  File.extname(path) == '.php'
end

#runObject



14
15
16
17
18
19
20
# File 'lib/pronto/phpmd.rb', line 14

def run
  return [] unless @patches

  @patches.select { |patch| valid_patch?(patch) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end

#run_phpmd(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pronto/phpmd.rb', line 34

def run_phpmd(path)
  escaped_executable = Shellwords.escape(@executable)
  escaped_path = Shellwords.escape(path)
  escaped_ruleset = Shellwords.escape(@ruleset)

  xml = `#{escaped_executable} #{escaped_path} xml #{escaped_ruleset}`
  doc = REXML::Document.new(xml)

  doc.elements.collect('pmd/file/violation') do |el|
    line = el.attributes['beginline'].to_i
    next unless line > 0

    { line: line, msg: el.first.to_s.strip }
  end
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/pronto/phpmd.rb', line 22

def valid_patch?(patch)
  patch.additions > 0 && php_file?(patch.new_file_full_path)
end