Class: Overcommit::Hook::PreCommit::PhpCsFixer

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

Overview

Runs ‘php-cs-fixer` against any modified PHP files.

Constant Summary collapse

MESSAGE_REGEX =
/\s+\d+\)\s+(?<file>.*\.php)(?<violated_rules>\s+\(\w+(?:,\s+)?\))?/.freeze

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/overcommit/hook/pre_commit/php_cs_fixer.rb', line 35

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

#runObject



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
# File 'lib/overcommit/hook/pre_commit/php_cs_fixer.rb', line 8

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