Class: Pronto::PhpStan

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

Instance Method Summary collapse

Constructor Details

#initialize(patches, commit = nil) ⇒ PhpStan

Returns a new instance of PhpStan.



7
8
9
10
11
12
13
14
# 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

Instance Method Details

#inspect(patch) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



61
62
63
64
65
66
# 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) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pronto/phpstan.rb', line 68

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

#runObject



16
17
18
19
20
21
22
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/pronto/phpstan.rb', line 24

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