• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 9155695369

20 May 2024 08:11AM UTC coverage: 75.471%. Remained the same
9155695369

push

github

web-flow
Merge pull request #501 from PHPCSStandards/feature/changelog-3.10.0

Changelog for the 3.10.0 release

23239 of 30792 relevant lines covered (75.47%)

60.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Generators/HTML.php
1
<?php
2
/**
3
 * A doc generator that outputs documentation in one big HTML file.
4
 *
5
 * Output is in one large HTML file and is designed for you to style with
6
 * your own stylesheet. It contains a table of contents at the top with anchors
7
 * to each sniff.
8
 *
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
 */
13

14
namespace PHP_CodeSniffer\Generators;
15

16
use DOMDocument;
17
use DOMNode;
18
use PHP_CodeSniffer\Config;
19

20
class HTML extends Generator
21
{
22

23

24
    /**
25
     * Generates the documentation for a standard.
26
     *
27
     * @return void
28
     * @see    processSniff()
29
     */
30
    public function generate()
×
31
    {
32
        ob_start();
×
33
        $this->printHeader();
×
34
        $this->printToc();
×
35

36
        foreach ($this->docFiles as $file) {
×
37
            $doc = new DOMDocument();
×
38
            $doc->load($file);
×
39
            $documentation = $doc->getElementsByTagName('documentation')->item(0);
×
40
            $this->processSniff($documentation);
×
41
        }
42

43
        $this->printFooter();
×
44

45
        $content = ob_get_contents();
×
46
        ob_end_clean();
×
47

48
        echo $content;
×
49

50
    }//end generate()
51

52

53
    /**
54
     * Print the header of the HTML page.
55
     *
56
     * @return void
57
     */
58
    protected function printHeader()
×
59
    {
60
        $standard = $this->ruleset->name;
×
61
        echo '<html>'.PHP_EOL;
×
62
        echo ' <head>'.PHP_EOL;
×
63
        echo "  <title>$standard Coding Standards</title>".PHP_EOL;
×
64
        echo '  <style>
×
65
                    body {
66
                        background-color: #FFFFFF;
67
                        font-size: 14px;
68
                        font-family: Arial, Helvetica, sans-serif;
69
                        color: #000000;
70
                    }
71

72
                    h1 {
73
                        color: #666666;
74
                        font-size: 20px;
75
                        font-weight: bold;
76
                        margin-top: 0px;
77
                        background-color: #E6E7E8;
78
                        padding: 20px;
79
                        border: 1px solid #BBBBBB;
80
                    }
81

82
                    h2 {
83
                        color: #00A5E3;
84
                        font-size: 16px;
85
                        font-weight: normal;
86
                        margin-top: 50px;
87
                    }
88

89
                    .code-comparison {
90
                        width: 100%;
91
                    }
92

93
                    .code-comparison td {
94
                        border: 1px solid #CCCCCC;
95
                    }
96

97
                    .code-comparison-title, .code-comparison-code {
98
                        font-family: Arial, Helvetica, sans-serif;
99
                        font-size: 12px;
100
                        color: #000000;
101
                        vertical-align: top;
102
                        padding: 4px;
103
                        width: 50%;
104
                        background-color: #F1F1F1;
105
                        line-height: 15px;
106
                    }
107

108
                    .code-comparison-code {
109
                        font-family: Courier;
110
                        background-color: #F9F9F9;
111
                    }
112

113
                    .code-comparison-highlight {
114
                        background-color: #DDF1F7;
115
                        border: 1px solid #00A5E3;
116
                        line-height: 15px;
117
                    }
118

119
                    .tag-line {
120
                        text-align: center;
121
                        width: 100%;
122
                        margin-top: 30px;
123
                        font-size: 12px;
124
                    }
125

126
                    .tag-line a {
127
                        color: #000000;
128
                    }
129
                </style>'.PHP_EOL;
×
130
        echo ' </head>'.PHP_EOL;
×
131
        echo ' <body>'.PHP_EOL;
×
132
        echo "  <h1>$standard Coding Standards</h1>".PHP_EOL;
×
133

134
    }//end printHeader()
135

136

137
    /**
138
     * Print the table of contents for the standard.
139
     *
140
     * The TOC is just an unordered list of bookmarks to sniffs on the page.
141
     *
142
     * @return void
143
     */
144
    protected function printToc()
×
145
    {
146
        echo '  <h2>Table of Contents</h2>'.PHP_EOL;
×
147
        echo '  <ul class="toc">'.PHP_EOL;
×
148

149
        foreach ($this->docFiles as $file) {
×
150
            $doc = new DOMDocument();
×
151
            $doc->load($file);
×
152
            $documentation = $doc->getElementsByTagName('documentation')->item(0);
×
153
            $title         = $this->getTitle($documentation);
×
154
            echo '   <li><a href="#'.str_replace(' ', '-', $title)."\">$title</a></li>".PHP_EOL;
×
155
        }
156

157
        echo '  </ul>'.PHP_EOL;
×
158

159
    }//end printToc()
160

161

162
    /**
163
     * Print the footer of the HTML page.
164
     *
165
     * @return void
166
     */
167
    protected function printFooter()
×
168
    {
169
        // Turn off errors so we don't get timezone warnings if people
170
        // don't have their timezone set.
171
        $errorLevel = error_reporting(0);
×
172
        echo '  <div class="tag-line">';
×
173
        echo 'Documentation generated on '.date('r');
×
174
        echo ' by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer '.Config::VERSION.'</a>';
×
175
        echo '</div>'.PHP_EOL;
×
176
        error_reporting($errorLevel);
×
177

178
        echo ' </body>'.PHP_EOL;
×
179
        echo '</html>'.PHP_EOL;
×
180

181
    }//end printFooter()
182

183

184
    /**
185
     * Process the documentation for a single sniff.
186
     *
187
     * @param \DOMNode $doc The DOMNode object for the sniff.
188
     *                      It represents the "documentation" tag in the XML
189
     *                      standard file.
190
     *
191
     * @return void
192
     */
193
    public function processSniff(DOMNode $doc)
×
194
    {
195
        $title = $this->getTitle($doc);
×
196
        echo '  <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL;
×
197
        echo "  <h2>$title</h2>".PHP_EOL;
×
198

199
        foreach ($doc->childNodes as $node) {
×
200
            if ($node->nodeName === 'standard') {
×
201
                $this->printTextBlock($node);
×
202
            } else if ($node->nodeName === 'code_comparison') {
×
203
                $this->printCodeComparisonBlock($node);
×
204
            }
205
        }
206

207
    }//end processSniff()
208

209

210
    /**
211
     * Print a text block found in a standard.
212
     *
213
     * @param \DOMNode $node The DOMNode object for the text block.
214
     *
215
     * @return void
216
     */
217
    protected function printTextBlock(DOMNode $node)
×
218
    {
219
        $content = trim($node->nodeValue);
×
220
        $content = htmlspecialchars($content);
×
221

222
        // Allow em tags only.
223
        $content = str_replace('&lt;em&gt;', '<em>', $content);
×
224
        $content = str_replace('&lt;/em&gt;', '</em>', $content);
×
225

226
        echo "  <p class=\"text\">$content</p>".PHP_EOL;
×
227

228
    }//end printTextBlock()
229

230

231
    /**
232
     * Print a code comparison block found in a standard.
233
     *
234
     * @param \DOMNode $node The DOMNode object for the code comparison block.
235
     *
236
     * @return void
237
     */
238
    protected function printCodeComparisonBlock(DOMNode $node)
×
239
    {
240
        $codeBlocks = $node->getElementsByTagName('code');
×
241

242
        $firstTitle = $codeBlocks->item(0)->getAttribute('title');
×
243
        $first      = trim($codeBlocks->item(0)->nodeValue);
×
244
        $first      = str_replace('<?php', '&lt;?php', $first);
×
245
        $first      = str_replace("\n", '</br>', $first);
×
246
        $first      = str_replace(' ', '&nbsp;', $first);
×
247
        $first      = str_replace('<em>', '<span class="code-comparison-highlight">', $first);
×
248
        $first      = str_replace('</em>', '</span>', $first);
×
249

250
        $secondTitle = $codeBlocks->item(1)->getAttribute('title');
×
251
        $second      = trim($codeBlocks->item(1)->nodeValue);
×
252
        $second      = str_replace('<?php', '&lt;?php', $second);
×
253
        $second      = str_replace("\n", '</br>', $second);
×
254
        $second      = str_replace(' ', '&nbsp;', $second);
×
255
        $second      = str_replace('<em>', '<span class="code-comparison-highlight">', $second);
×
256
        $second      = str_replace('</em>', '</span>', $second);
×
257

258
        echo '  <table class="code-comparison">'.PHP_EOL;
×
259
        echo '   <tr>'.PHP_EOL;
×
260
        echo "    <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL;
×
261
        echo "    <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL;
×
262
        echo '   </tr>'.PHP_EOL;
×
263
        echo '   <tr>'.PHP_EOL;
×
264
        echo "    <td class=\"code-comparison-code\">$first</td>".PHP_EOL;
×
265
        echo "    <td class=\"code-comparison-code\">$second</td>".PHP_EOL;
×
266
        echo '   </tr>'.PHP_EOL;
×
267
        echo '  </table>'.PHP_EOL;
×
268

269
    }//end printCodeComparisonBlock()
270

271

272
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc