class Phpcs::Phpcs

Constants

STANDARDS

Public Class Methods

new(standard = nil) click to toggle source
# File lib/phpcs.rb, line 20
def initialize(standard = nil)
  @standard = (STANDARDS.include? standard) ? standard : 'PSR2'
end

Public Instance Methods

lint(content) click to toggle source
# File lib/phpcs.rb, line 24
def lint(content)
  code = Base64.encode64(content).strip[0..16]
  file = Tempfile.new(code)
  file.write(content)
  file.close

  stdin, stdout, stderr, wait_thr = Open3.popen3('phpcs', file.path, "--standard=#{@standard}", '--report=json')

  out = JSON.load(stdout.read)

  if out['totals']['errors'] > 0
    parse_matches(out)
  else
    []
  end
end
parse_matches(body) click to toggle source
# File lib/phpcs.rb, line 41
def parse_matches(body)
  path = body['files'].keys.first
  errors = body['files'][path]
  matches = []

  errors['messages'].each do |error|
    matches << Match.new(error['line'], error['message'])
  end
  matches
end