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 = (standard.to_s.empty?) ? 'PSR2' : standard
  unless STANDARDS.include?(@standard)
    file = Tempfile.new(['standard', '.xml'])
    file.write(standard)
    file.close
    @standard = file.path
  end
end

Public Instance Methods

lint(content) click to toggle source
# File lib/phpcs.rb, line 34
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_path, 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 51
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
phpcs_path() click to toggle source
# File lib/phpcs.rb, line 30
def phpcs_path
  @path ||= File.expand_path('../../PHP_CodeSniffer/scripts/phpcs', __FILE__)
end