Sha256: 5f3f38c828e5a35fd3a3be8e2d8d482910068867933ab6140e655369a3d683c7

Contents?: true

Size: 1.34 KB

Versions: 19

Compression:

Stored size: 1.34 KB

Contents

# 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

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
overcommit-0.58.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.57.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.56.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.55.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.54.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.54.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-jeygeethanmedia-0.53.1.2 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-jeygeethanmedia-0.53.1.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-jeygeethanmedia-0.53.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.53.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.52.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.52.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.51.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.50.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.49.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.49.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.48.1 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.48.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb
overcommit-0.47.0 lib/overcommit/hook/pre_commit/php_cs_fixer.rb