forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_cs_fixer.rb
57 lines (45 loc) · 1.34 KB
/
php_cs_fixer.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
51
52
53
54
55
56
57
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Runs `php-cs-fixer` against any modified PHP files.
class PhpCsFixer < Base
MESSAGE_REGEX = /\s+\d+\)\s+(?<file>.*\.php)(?<violated_rules>\s+\(\w+(?:,\s+)?\))?/
def run
messages = []
feedback = ''
# Exit status for all of the runs. Should be zero!
exit_status_sum = 0
applicable_files.each do |file|
result = execute(command, args: [file])
output = result.stdout.chomp
exit_status_sum += result.status
if result.status
messages = output.lstrip.split("\n")
end
end
unless messages.empty?
feedback = parse_messages(messages)
end
:pass if exit_status_sum == 0
:pass if feedback.empty?
feedback
end
def parse_messages(messages)
output = []
messages.map do |message|
message.scan(MESSAGE_REGEX).map do |file, violated_rules|
type = :error
unless violated_rules.nil?
type = :warning
end
text = if type == :error
"Cannot process #{file}: Syntax error"
else
"#{file} has been fixed"
end
output << Overcommit::Hook::Message.new(type, file, 0, text)
end
end
output
end
end
end