-
Notifications
You must be signed in to change notification settings - Fork 279
/
php_cs.rb
43 lines (35 loc) · 1.15 KB
/
php_cs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Runs `phpcs` against any modified PHP files.
class PhpCs < Base
# Parse `phpcs` csv mode output
MESSAGE_REGEX = /^\"(?<file>.+)\",(?<line>\d+),\d+,(?<type>.+),\"(?<msg>.+)\"/.freeze
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
'error'.include?(type) ? :error : :warning
end
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
# Transform the CSV output into a tidy human readable message
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
end
end