Class: Overcommit::Hook::PreCommit::PhpCs

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/php_cs.rb

Overview

Runs ‘phpcs` against any modified PHP files.

Constant Summary collapse

MESSAGE_REGEX =

Parse ‘phpcs` csv mode output

/^\"(?<file>.+)\",(?<line>\d+),\d+,(?<type>.+),\"(?<msg>.+)\"/.freeze
MESSAGE_TYPE_CATEGORIZER =
lambda do |type|
  'error'.include?(type) ? :error : :warning
end

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#parse_messages(messages) ⇒ Object

Transform the CSV output into a tidy human readable message



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/overcommit/hook/pre_commit/php_cs.rb', line 29

def parse_messages(messages)
  output = []

  messages.map do |message|
    message.scan(MESSAGE_REGEX).map do |file, line, type, msg|
      type = MESSAGE_TYPE_CATEGORIZER.call(type)
      text = " #{file}:#{line}\n  #{msg}"
      output << Overcommit::Hook::Message.new(type, file, line.to_i, text)
    end
  end

  output
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/overcommit/hook/pre_commit/php_cs.rb', line 12

def run
  messages = []

  result = execute(command, args: applicable_files)
  if result.status
    messages = result.stdout.split("\n")
    # Discard the csv header
    messages.shift
  end

  return :fail if messages.empty? && !result.success?
  return :pass if messages.empty?

  parse_messages(messages)
end