codeclimate/codeclimate-phpmd

View on GitHub
bin/build-content

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require "httparty"
require "fileutils"
require "nokogiri"

CONTENT_DIR = "./content"
CATEGORIES = %w(cleancode codesize controversial design naming unusedcode)

class Rule
  attr_accessor :name, :since, :source, :description, :example
end

def fetch_category_rules(category)
  base_url = "https://raw.githubusercontent.com/phpmd/phpmd/master/src/main/resources/rulesets"
  HTTParty.get("#{base_url}/#{category}.xml").body
end

def process_category_rules(xml_rules)
  rules = []
  rules_node_set =  Nokogiri::XML.parse(xml_rules).children.children
  rules_node_set.each do |node|
    next unless node.name.eql?("rule")
    rule = Rule.new
    rule.name = node.get_attribute("name")
    rule.since = node.get_attribute("since")
    rule.source = node.get_attribute("externalInfoUrl")
    node.children.each do |child_node|
      if child_node.name.eql?("description")
        rule.description = child_node.text.strip
      end
      if child_node.name.eql?("example")
        rule.example = child_node.text
      end
    end
    rules << rule
  end
  rules
end

FileUtils.rm_rf(CONTENT_DIR)
FileUtils.mkdir_p(CONTENT_DIR)

rules = []

CATEGORIES.each do |category|
  raw_xml_rules = fetch_category_rules(category)
  category_rules = process_category_rules(raw_xml_rules)
  rules.concat(category_rules)
end

rules.each do |rule|
  File.open("#{CONTENT_DIR}/#{rule.name.downcase}.txt", "w") do |file|
    md = <<~MARKDOWN
      ## #{rule.name}
      #{"### Since: " if rule.since } #{rule.since if rule.since}

      #{rule.description}

      ## Example
      ```php
      #{rule.example}
      ```

      ### Source #{rule.source}
    MARKDOWN
    file.write(md)
  end
end