1<?php
2/**
3 * A doc generator that outputs text-based documentation.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @author    Marc McIntyre <mmcintyre@squiz.net>
11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16if (class_exists('PHP_CodeSniffer_DocGenerators_Generator', true) === false) {
17    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_Generator not found');
18}
19
20/**
21 * A doc generator that outputs text-based documentation.
22 *
23 * Output is designed to be displayed in a terminal and is wrapped to 100 characters.
24 *
25 * @category  PHP
26 * @package   PHP_CodeSniffer
27 * @author    Greg Sherwood <gsherwood@squiz.net>
28 * @author    Marc McIntyre <mmcintyre@squiz.net>
29 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
30 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31 * @version   Release: @package_version@
32 * @link      http://pear.php.net/package/PHP_CodeSniffer
33 */
34class PHP_CodeSniffer_DocGenerators_Text extends PHP_CodeSniffer_DocGenerators_Generator
35{
36
37
38    /**
39     * Process the documentation for a single sniff.
40     *
41     * @param DOMNode $doc The DOMNode object for the sniff.
42     *                     It represents the "documentation" tag in the XML
43     *                     standard file.
44     *
45     * @return void
46     */
47    public function processSniff(DOMNode $doc)
48    {
49        $this->printTitle($doc);
50
51        foreach ($doc->childNodes as $node) {
52            if ($node->nodeName === 'standard') {
53                $this->printTextBlock($node);
54            } else if ($node->nodeName === 'code_comparison') {
55                $this->printCodeComparisonBlock($node);
56            }
57        }
58
59    }//end processSniff()
60
61
62    /**
63     * Prints the title area for a single sniff.
64     *
65     * @param DOMNode $doc The DOMNode object for the sniff.
66     *                     It represents the "documentation" tag in the XML
67     *                     standard file.
68     *
69     * @return void
70     */
71    protected function printTitle(DOMNode $doc)
72    {
73        $title    = $this->getTitle($doc);
74        $standard = $this->getStandard();
75
76        echo PHP_EOL;
77        echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
78        echo strtoupper(PHP_EOL."| $standard CODING STANDARD: $title |".PHP_EOL);
79        echo str_repeat('-', (strlen("$standard CODING STANDARD: $title") + 4));
80        echo PHP_EOL.PHP_EOL;
81
82    }//end printTitle()
83
84
85    /**
86     * Print a text block found in a standard.
87     *
88     * @param DOMNode $node The DOMNode object for the text block.
89     *
90     * @return void
91     */
92    protected function printTextBlock($node)
93    {
94        $text = trim($node->nodeValue);
95        $text = str_replace('<em>', '*', $text);
96        $text = str_replace('</em>', '*', $text);
97
98        $lines    = array();
99        $tempLine = '';
100        $words    = explode(' ', $text);
101
102        foreach ($words as $word) {
103            if (strlen($tempLine.$word) >= 99) {
104                if (strlen($tempLine.$word) === 99) {
105                    // Adding the extra space will push us to the edge
106                    // so we are done.
107                    $lines[]  = $tempLine.$word;
108                    $tempLine = '';
109                } else if (strlen($tempLine.$word) === 100) {
110                    // We are already at the edge, so we are done.
111                    $lines[]  = $tempLine.$word;
112                    $tempLine = '';
113                } else {
114                    $lines[]  = rtrim($tempLine);
115                    $tempLine = $word.' ';
116                }
117            } else {
118                $tempLine .= $word.' ';
119            }
120        }//end foreach
121
122        if ($tempLine !== '') {
123            $lines[] = rtrim($tempLine);
124        }
125
126        echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
127
128    }//end printTextBlock()
129
130
131    /**
132     * Print a code comparison block found in a standard.
133     *
134     * @param DOMNode $node The DOMNode object for the code comparison block.
135     *
136     * @return void
137     */
138    protected function printCodeComparisonBlock($node)
139    {
140        $codeBlocks = $node->getElementsByTagName('code');
141        $first      = trim($codeBlocks->item(0)->nodeValue);
142        $firstTitle = $codeBlocks->item(0)->getAttribute('title');
143
144        $firstTitleLines = array();
145        $tempTitle       = '';
146        $words           = explode(' ', $firstTitle);
147
148        foreach ($words as $word) {
149            if (strlen($tempTitle.$word) >= 45) {
150                if (strlen($tempTitle.$word) === 45) {
151                    // Adding the extra space will push us to the edge
152                    // so we are done.
153                    $firstTitleLines[] = $tempTitle.$word;
154                    $tempTitle         = '';
155                } else if (strlen($tempTitle.$word) === 46) {
156                    // We are already at the edge, so we are done.
157                    $firstTitleLines[] = $tempTitle.$word;
158                    $tempTitle         = '';
159                } else {
160                    $firstTitleLines[] = $tempTitle;
161                    $tempTitle         = $word;
162                }
163            } else {
164                $tempTitle .= $word.' ';
165            }
166        }//end foreach
167
168        if ($tempTitle !== '') {
169            $firstTitleLines[] = $tempTitle;
170        }
171
172        $first      = str_replace('<em>', '', $first);
173        $first      = str_replace('</em>', '', $first);
174        $firstLines = explode("\n", $first);
175
176        $second      = trim($codeBlocks->item(1)->nodeValue);
177        $secondTitle = $codeBlocks->item(1)->getAttribute('title');
178
179        $secondTitleLines = array();
180        $tempTitle        = '';
181        $words            = explode(' ', $secondTitle);
182
183        foreach ($words as $word) {
184            if (strlen($tempTitle.$word) >= 45) {
185                if (strlen($tempTitle.$word) === 45) {
186                    // Adding the extra space will push us to the edge
187                    // so we are done.
188                    $secondTitleLines[] = $tempTitle.$word;
189                    $tempTitle          = '';
190                } else if (strlen($tempTitle.$word) === 46) {
191                    // We are already at the edge, so we are done.
192                    $secondTitleLines[] = $tempTitle.$word;
193                    $tempTitle          = '';
194                } else {
195                    $secondTitleLines[] = $tempTitle;
196                    $tempTitle          = $word;
197                }
198            } else {
199                $tempTitle .= $word.' ';
200            }
201        }//end foreach
202
203        if ($tempTitle !== '') {
204            $secondTitleLines[] = $tempTitle;
205        }
206
207        $second      = str_replace('<em>', '', $second);
208        $second      = str_replace('</em>', '', $second);
209        $secondLines = explode("\n", $second);
210
211        $maxCodeLines  = max(count($firstLines), count($secondLines));
212        $maxTitleLines = max(count($firstTitleLines), count($secondTitleLines));
213
214        echo str_repeat('-', 41);
215        echo ' CODE COMPARISON ';
216        echo str_repeat('-', 42).PHP_EOL;
217
218        for ($i = 0; $i < $maxTitleLines; $i++) {
219            if (isset($firstTitleLines[$i]) === true) {
220                $firstLineText = $firstTitleLines[$i];
221            } else {
222                $firstLineText = '';
223            }
224
225            if (isset($secondTitleLines[$i]) === true) {
226                $secondLineText = $secondTitleLines[$i];
227            } else {
228                $secondLineText = '';
229            }
230
231            echo '| ';
232            echo $firstLineText.str_repeat(' ', (46 - strlen($firstLineText)));
233            echo ' | ';
234            echo $secondLineText.str_repeat(' ', (47 - strlen($secondLineText)));
235            echo ' |'.PHP_EOL;
236        }//end for
237
238        echo str_repeat('-', 100).PHP_EOL;
239
240        for ($i = 0; $i < $maxCodeLines; $i++) {
241            if (isset($firstLines[$i]) === true) {
242                $firstLineText = $firstLines[$i];
243            } else {
244                $firstLineText = '';
245            }
246
247            if (isset($secondLines[$i]) === true) {
248                $secondLineText = $secondLines[$i];
249            } else {
250                $secondLineText = '';
251            }
252
253            echo '| ';
254            echo $firstLineText.str_repeat(' ', (47 - strlen($firstLineText)));
255            echo '| ';
256            echo $secondLineText.str_repeat(' ', (48 - strlen($secondLineText)));
257            echo '|'.PHP_EOL;
258        }//end for
259
260        echo str_repeat('-', 100).PHP_EOL.PHP_EOL;
261
262    }//end printCodeComparisonBlock()
263
264
265}//end class
266