Class: Pronto::Phpcs

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

Instance Method Summary collapse

Constructor Details

#initialize(patches, commit = nil) ⇒ Phpcs

Returns a new instance of Phpcs.



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

def initialize(patches, commit = nil)
  super

  @executable = ENV['PRONTO_PHPCS_EXECUTABLE'] || 'phpcs'
  @standard = ENV['PRONTO_PHPCS_STANDARD'] || 'PSR2'
end

Instance Method Details

#inspect(patch) ⇒ Object



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

def inspect(patch)
  path = patch.new_file_full_path.to_s
  run_phpcs(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



45
46
47
48
49
50
# File 'lib/pronto/phpcs.rb', line 45

def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  level = if offence['type'] == 'ERROR' then :error else :warning end

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

#php_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/pronto/phpcs.rb', line 52

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

#runObject



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

def run
  return [] unless @patches

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

#run_phpcs(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/pronto/phpcs.rb', line 34

def run_phpcs(path)
  escaped_executable = Shellwords.escape(@executable)
  escaped_standard = Shellwords.escape(@standard)
  escaped_path = Shellwords.escape(path)

  JSON.parse(`#{escaped_executable} --report=json --standard=#{escaped_standard} #{escaped_path}`)
    .fetch('files', {})
    .fetch(path, {})
    .fetch('messages', [])
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


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

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