Passed
Pull Request — master (#18)
by Andru
04:16 queued 12s
created

update-rules.php ➔ generate_index()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 62

Duplication

Lines 16
Ratio 25.81 %

Importance

Changes 0
Metric Value
cc 6
nc 20
nop 1
dl 16
loc 62
rs 8.2068
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env php
2
<?php
3
$input = __DIR__ . '/../../..';
4
5
// The output directory
6
$output = __DIR__ . '/../rst/rules';
7
8
if (file_exists($input) === false) {
9
    fwrite(STDOUT, 'Cannot locate rules, skipping here...' . PHP_EOL);
10
    exit(1);
11
}
12
13
$sets = array();
14
15
$files = glob($input . '/src/main/resources/rulesets/*.xml');
16
sort($files);
17
18
$index = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL .
19
         '<index>' . PHP_EOL .
20
         '  <site index="true" display="false">' . PHP_EOL .
21
         '    <name>Index</name>' . PHP_EOL .
22
         '    <path>index.rst</path>' . PHP_EOL .
23
         '  </site>' . PHP_EOL;
24
25
foreach ($files as $file) {
26
    echo 'Processing: ', $file, PHP_EOL;
27
28
    $name = pathinfo($file, PATHINFO_FILENAME);
29
    $path = $output . '/' . $name . '.rst';
30
31
    $cmd = sprintf(
32
        'xsltproc %s/pmd.xsl %s > %s',
33
        escapeshellarg(dirname(__FILE__)),
34
        escapeshellarg($file),
35
        escapeshellarg($path)
36
    );
37
    shell_exec($cmd);
38
39
    $sxml   = simplexml_load_file($file);
40
    $index .= '    <site>' . PHP_EOL .
41
              '        <name>' . $sxml['name'] . '</name>' . PHP_EOL .
42
              '        <path>' . $name . '.rst</path>' . PHP_EOL .
43
              '    </site>' . PHP_EOL;
44
45
    $rules = array();
46
    foreach ($sxml->rule as $rule) {
47
        $rules[] = array(
48
            'name'  =>  normalize($rule['name']),
49
            'desc'  =>  normalize($rule->description),
50
            'href'  =>  $name . '.html#' . strtolower($rule['name']),
51
        );
52
    }
53
54
    $sets[] = array(
55
        'name'   =>  normalize($sxml['name']),
56
        'desc'   =>  normalize($sxml->description),
57
        'rules'  =>  $rules,
58
    );
59
}
60
61
$index .= '</index>';
62
63
file_put_contents($output . '/index.rst', generate_index($sets));
64
file_put_contents($output . '/.index.xml', $index);
65
66
exit(0);
67
68
function normalize($elem)
69
{
70
    return preg_replace('(\s+)s', ' ', trim((string) $elem));
71
}
72
73
function generate_index(array $sets)
74
{
75
    $content = '================' . PHP_EOL
76
             . 'Current Rulesets' . PHP_EOL
77
             . '================'
78
             . PHP_EOL . PHP_EOL
79
             . 'List of rulesets and rules contained in each ruleset.'
80
             . PHP_EOL . PHP_EOL;
81
82
    foreach ($sets as $set) {
83
        $content .= sprintf(
84
            '- `%s`__: %s%s',
85
            $set['name'],
86
            $set['desc'],
87
            PHP_EOL
88
        );
89
    }
90
91
    $content .= PHP_EOL;
92
    foreach ($sets as $set) {
93
        $anchor = preg_replace('([^a-z0-9]+)i', '-', $set['name']);
94
        $anchor = strtolower($anchor);
95
96
        $content .= '__ index.html#' . $anchor . PHP_EOL;
97
    }
98
    $content .= PHP_EOL;
99
100
    foreach ($sets as $set) {
101
        $content .= $set['name'] . PHP_EOL;
102
        $content .= str_repeat('=', strlen($set['name']));
103
        $content .= PHP_EOL . PHP_EOL;
104
105
        foreach ($set['rules'] as $rule) {
106
            $content .= sprintf(
107
                '- `%s`__: %s%s',
108
                $rule['name'],
109
                $rule['desc'],
110
                PHP_EOL
111
            );
112
        }
113
114
        $content .= PHP_EOL;
115
        foreach ($set['rules'] as $rule) {
116
            $content .= '__ ' . $rule['href'] . PHP_EOL;
117
        }
118
        $content .= PHP_EOL;
119
    }
120
    $content .= PHP_EOL;
121
    $content .= 'Remark' . PHP_EOL .
122
                '======' . PHP_EOL . PHP_EOL .
123
                '  This document is based on a ruleset xml-file, that ' .
124
                'was taken from the original source of the `PMD`__ ' .
125
                'project. This means that most parts of the content ' .
126
                'on this page are the intellectual work of the PMD ' .
127
                'community and its contributors and not of the PHPMD ' .
128
                'project.' .
129
                PHP_EOL . PHP_EOL .
130
                '__ https://pmd.sourceforge.net/' .
131
                PHP_EOL;
132
133
    return $content;
134
}
135