Sha256: e21aed8606806db8c7379e819934b0264ae0123e1911a7a6b7626f1b8bbcdf54

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

# 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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
overcommit-0.52.1 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.52.0 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.51.0 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.50.0 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.49.1 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.49.0 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.48.1 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.48.0 lib/overcommit/hook/pre_commit/php_cs.rb
overcommit-0.47.0 lib/overcommit/hook/pre_commit/php_cs.rb