Skip to content
9 changes: 9 additions & 0 deletions Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Category
"CleanCode/BooleanArgumentFlag" => [self::CLARITY, 300000],
"CleanCode/ElseExpression" => [self::CLARITY, 200000],
"CleanCode/StaticAccess" => [self::CLARITY, 200000],
"CleanCode/IfStatementAssignment" => [self::CLARITY, 200000],
"CleanCode/DuplicatedArrayKey" => [self::CLARITY, 300000],
"CleanCode/MissingImport" => [self::CLARITY, 300000],
"CleanCode/UndefinedVariable" => [self::CLARITY, 700000],
"CleanCode/ErrorControlOperator" => [self::CLARITY, 300000],
"Controversial/CamelCaseClassName" => [self::STYLE, 500000],
"Controversial/CamelCaseMethodName" => [self::STYLE, 1000],
"Controversial/CamelCaseParameterName" => [self::STYLE, 500000],
Expand All @@ -22,8 +27,10 @@ class Category
"Controversial/Superglobals" => [self::SECURITY, 100000],
"CyclomaticComplexity" => [self::COMPLEXITY, 100000],
"Design/CouplingBetweenObjects" => [self::CLARITY, 400000],
"Design/CountInLoopExpression" => [self::BUG_RISK, 100000],
"Design/DepthOfInheritance" => [self::CLARITY, 500000],
"Design/DevelopmentCodeFragment" => [self::SECURITY, 100000],
"Design/EmptyCatchBlock" => [self::BUG_RISK, 200000],
"Design/EvalExpression" => [self::SECURITY, 300000],
"Design/ExitExpression" => [self::BUG_RISK, 200000],
"Design/GotoStatement" => [self::CLARITY, 200000],
Expand All @@ -40,7 +47,9 @@ class Category
"Naming/BooleanGetMethodName" => [self::STYLE, 200000],
"Naming/ConstantNamingConventions" => [self::STYLE, 100000],
"Naming/ConstructorWithNameAsEnclosingClass" => [self::COMPATIBILITY, 400000],
"Naming/LongClassName" => [self::STYLE, 1000000],
"Naming/LongVariable" => [self::STYLE, 1000000],
"Naming/ShortClassName" => [self::STYLE, 500000],
"Naming/ShortMethodName" => [self::STYLE, 800000],
"Naming/ShortVariable" => [self::STYLE, 500000],
"UnusedFormalParameter" => [self::BUG_RISK, 200000],
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ RUN composer install --no-dev && \

# Build Content
COPY bin/build-content ./bin/build-content
RUN apk add --no-cache ruby ruby-json ruby-bigdecimal && \
gem install rdoc httparty --no-document && \
RUN apk add --no-cache ruby ruby-json ruby-bigdecimal ruby-dev build-base libxml2-dev libxslt-dev libffi-dev && \
gem install rdoc nokogiri httparty --no-document && \
./bin/build-content && \
chown -R app:app content && \
gem uninstall rdoc httparty && \
gem uninstall --all rdoc httparty nokogiri && \
rm -rf $( gem environment gemdir ) && \
apk del --purge ruby ruby-json ruby-bigdecimal && \
apk del --purge ruby ruby-json ruby-bigdecimal ruby-dev build-base libxml2-dev libxslt-dev libffi-dev && \
rm -r /var/cache/* ~/.gem

COPY . ./
Expand Down
4 changes: 3 additions & 1 deletion Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodeClimate\PHPMD;

use PHPMD\PHPMD;
use PHPMD\Report;
use PHPMD\RuleSetFactory;
use PHPMD\Writer\StreamWriter;
use PHPMD\Renderer\JSONRenderer;
Expand Down Expand Up @@ -115,7 +116,8 @@ public function run($files)
$file,
$rulesets,
array($renderer),
$ruleSetFactory
$ruleSetFactory,
new Report()
);
}

Expand Down
93 changes: 52 additions & 41 deletions bin/build-content
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
#!/usr/bin/env ruby

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

CONTENT_DIR = "./content"
FILE_NAME_OVERRIDES = {
"excessiveclasscomplexity" => "weightedmethodcount",
}

categories = {
cleancode: "http://phpmd.org/rules/cleancode.txt",
codesize: "http://phpmd.org/rules/codesize.txt",
controversial: "http://phpmd.org/rules/controversial.txt",
design: "http://phpmd.org/rules/design.txt",
naming: "http://phpmd.org/rules/naming.txt",
unused: "http://phpmd.org/rules/unusedcode.txt"
}
CATEGORIES = %w(cleancode codesize controversial design naming unusedcode)

FileUtils.rm_rf(CONTENT_DIR)
FileUtils.mkdir_p(CONTENT_DIR)
class Rule
attr_accessor :name, :since, :source, :description, :example
end

def file_name(header)
file_name = header.gsub(" ", "_").downcase
FILE_NAME_OVERRIDES.fetch(file_name, file_name)
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

categories.each do |category, url|
text = HTTParty.get(url).body
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

matches = text.split(/=+\n.*?\n=+/, 2).pop
matches = matches.split(/^=+\n$/)
FileUtils.rm_rf(CONTENT_DIR)
FileUtils.mkdir_p(CONTENT_DIR)

sections = matches.each_with_object([]) do |match, array|
split = match.split(/\n/)
title = split.pop
body = split.join("\n")
rules = []

body.gsub!(/(?<=Example:) ::/, "\n\n```php")
body = body.split(/This rule.*/).shift
body += "\n```"
CATEGORIES.each do |category|
raw_xml_rules = fetch_category_rules(category)
category_rules = process_category_rules(raw_xml_rules)
rules.concat(category_rules)
end

array << body
array << title
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}

sections.shift
sections.pop
#{rule.description}

sections.each_slice(2) do |(header, body)|
next if header == "Remark"
## Example
```php
#{rule.example}
```

File.open("#{CONTENT_DIR}/#{file_name(header)}.txt", "w") do |file|
file.write(body)
file.write("\n\nSource: #{url}")
end
## Source #{rule.source}
MARKDOWN
file.write(md)
end
end
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"phpmd/phpmd": "~2.3",
"phpmd/phpmd": "~2.10",
"barracudanetworks/forkdaemon-php": "~1.0.7"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.