Skip to content

Commit 8781cf6

Browse files
committed
Add content for PHPMD readups
This adds a hacky Ruby script to fetch the .txt versions of PHPMD rule documentation and format it in the correct folder structure for the engine to read.
1 parent 7f3bb72 commit 8781cf6

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor/
2+
content

Category.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,17 @@ public static function categoryFor($checkName)
6262

6363
return $category;
6464
}
65+
66+
public static function documentationFor($checkName)
67+
{
68+
if (preg_match("/^Unused/i", $checkName)) {
69+
$checkName = "Unused/" . $checkName;
70+
}
71+
72+
$filePath = dirname(__FILE__) . "/content/" . strtolower($checkName) . ".txt";
73+
74+
if (file_exists($filePath)) {
75+
return file_get_contents($filePath);
76+
}
77+
}
6578
}

Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ WORKDIR /usr/src/app
44
COPY composer.json /usr/src/app/
55
COPY composer.lock /usr/src/app/
66

7-
RUN apk --update add git php-common php-xml php-dom php-ctype php-iconv php-json php-pcntl php-phar php-openssl php-opcache php-sockets curl && \
7+
RUN apk --update add git php-common php-xml php-dom php-ctype php-iconv \
8+
php-json php-pcntl php-phar php-openssl php-opcache php-sockets curl \
9+
build-base ruby-dev ruby ruby-bundler && \
10+
gem install httparty --no-rdoc --no-ri && \
811
curl -sS https://getcomposer.org/installer | php && \
912
/usr/src/app/composer.phar install && \
10-
apk del build-base && rm -fr /usr/share/ri
13+
apk del build-base
14+
15+
COPY . /usr/src/app
1116

1217
RUN adduser -u 9000 -D app
18+
RUN chown -R app .
19+
1320
USER app
1421

15-
COPY . /usr/src/app
22+
RUN ./bin/build-content
1623

1724
CMD ["/usr/src/app/bin/codeclimate-phpmd"]

JSONRenderer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function renderReport(Report $report)
2121

2222
$metric = $violation->getMetric();
2323
$points = Category::pointsFor($checkName, $metric);
24+
$content = Category::documentationFor($checkName);
2425

2526
$issue = array(
2627
"type" => "issue",
@@ -34,9 +35,15 @@ public function renderReport(Report $report)
3435
"begin" => $violation->getBeginLine(),
3536
"end" => $violation->getEndLine()
3637
)
37-
)
38+
),
3839
);
3940

41+
if ($content) {
42+
$issue["content"] = array(
43+
"body" => $content
44+
);
45+
}
46+
4047
$json = json_encode($issue, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE);
4148
$writer->write($json);
4249
$writer->write(chr(0));

bin/build-content

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'httparty'
4+
require 'fileutils'
5+
6+
CONTENT_DIR = "./content"
7+
8+
categories = {
9+
cleancode: "http://phpmd.org/rules/cleancode.txt",
10+
codesize: "http://phpmd.org/rules/codesize.txt",
11+
controversial: "http://phpmd.org/rules/controversial.txt",
12+
design: "http://phpmd.org/rules/design.txt",
13+
naming: "http://phpmd.org/rules/naming.txt",
14+
unused: "http://phpmd.org/rules/unusedcode.txt"
15+
}
16+
17+
FileUtils.rm_rf(CONTENT_DIR)
18+
19+
categories.each do |category, url|
20+
category_path = "#{CONTENT_DIR}/#{category}/"
21+
FileUtils.mkdir_p(category_path)
22+
23+
text = HTTParty.get(url).body
24+
25+
matches = text.split(/=+\n.*?\n=+/, 2).pop
26+
matches = matches.split(/^=+\n$/)
27+
28+
sections = matches.each_with_object([]) do |match, array|
29+
split = match.split(/\n/)
30+
title = split.pop
31+
body = split.join("\n")
32+
33+
body.gsub!(/(?<=Example:) ::/, "\n\n```php")
34+
body = body.split(/This rule.*/).shift
35+
body += "\n```"
36+
37+
38+
array << body
39+
array << title
40+
end
41+
42+
sections.shift
43+
sections.pop
44+
45+
sections.each_slice(2) do |(header, body)|
46+
next if header == "Remark"
47+
48+
file_name = header.gsub(" ", "_").downcase
49+
File.open("#{category_path}/#{file_name}.txt", "w") do |file|
50+
file.write(body)
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)