class Pronto::PhpStan

Public Class Methods

new(patches, commit = nil) click to toggle source
Calls superclass method
# File lib/pronto/phpstan.rb, line 7
def initialize(patches, commit = nil)
  super

  @executable = ENV['PRONTO_PHPSTAN_EXECUTABLE'] || 'phpstan'
  @config = ENV['PRONTO_PHPSTAN_CONFIG'] || nil
  @autoloader = ENV['PRONTO_PHPSTAN_AUTOLOADER'] || nil
  @level = ENV['PRONTO_PHPSTAN_LEVEL'] || 7
end

Public Instance Methods

inspect(patch) click to toggle source
# File lib/pronto/phpstan.rb, line 28
def inspect(patch)
  path = patch.new_file_full_path.to_s
  run_phpstan(path).map do |error_element|
    line_no = Integer(error_element.attribute('line').value)
    message = error_element.attribute('message').value
    severity = error_element.attribute('severity').value
    patch.added_lines.select { |line| line.new_lineno == line_no }
         .map { |line| new_message(message, severity, line) }
  end
end
new_message(message, severity, line) click to toggle source
# File lib/pronto/phpstan.rb, line 61
def new_message(message, severity, line)
  path = line.patch.delta.new_file[:path]
  level = severity == 'error' ? :error : :warning

  Message.new(path, line, level, message, nil, self.class)
end
php_file?(path) click to toggle source
# File lib/pronto/phpstan.rb, line 68
def php_file?(path)
  File.extname(path) == '.php'
end
run() click to toggle source
# File lib/pronto/phpstan.rb, line 16
def run
  return [] unless @patches

  @patches.select { |patch| valid_patch?(patch) }
          .map { |patch| inspect(patch) }
          .flatten.compact
end
run_phpstan(path) click to toggle source
# File lib/pronto/phpstan.rb, line 39
def run_phpstan(path)
  escaped_executable = Shellwords.escape(@executable)
  escaped_level = Shellwords.escape(@level)
  escaped_path = Shellwords.escape(path)

  command = "#{escaped_executable} analyse --errorFormat=checkstyle -l #{escaped_level}"

  unless @autoloader.nil?
    escaped_autoloader = Shellwords.escape(@autoloader)
    command = "#{command} -a #{escaped_autoloader}"
  end

  unless @config.nil?
    escaped_config = Shellwords.escape(@config)
    command = "#{command} -c #{escaped_config}"
  end

  command = "#{command} #{escaped_path}"
  doc = Nokogiri::XML(`#{command}`)
  doc.xpath('//file/error')
end
valid_patch?(patch) click to toggle source
# File lib/pronto/phpstan.rb, line 24
def valid_patch?(patch)
  patch.additions > 0 && php_file?(patch.new_file_full_path)
end