Sha256: 643d412fd7ce5444da00e39086d5a4542f673fcfe6ae64c4f1538d20f37f3a37

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

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

Version data entries

5 entries across 3 versions & 2 rubygems

Version Path
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_commit/php_cs_fixer.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_commit/php_cs_fixer.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_commit/php_cs_fixer.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/overcommit-0.46.0/lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.46.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb