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>.+)\"/
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?, #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


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/overcommit/hook/pre_commit/php_cs.rb', line 34

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


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

def run
  messages = []

  applicable_files.each do |file|
    result = execute(command, args: [file])
    if result.status
      rows = result.stdout.split("\n")

      # Discard the csv header
      rows.shift

      # Push each of the errors in the particular file into the array
      rows.map do |row|
        messages << row
      end
    end
  end

  return :pass if messages.empty?

  parse_messages(messages)
end