forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_cs.rb
50 lines (40 loc) · 1.27 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
44
45
46
47
48
49
50
# 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>.+)\"/
MESSAGE_TYPE_CATEGORIZER = lambda do |type|
'error'.include?(type) ? :error : :warning
end
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
# 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