Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * The base class for all PHP_CodeSniffer documentation generators.
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 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: Generator.php,v 1.4 2008/12/02 02:38:34 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * The base class for all PHP_CodeSniffer documentation generators.
19
 *
20
 * Documentation generators are used to print documentation about code sniffs
21
 * in a standard.
22
 *
23
 * @category  PHP
24
 * @package   PHP_CodeSniffer
25
 * @author    Greg Sherwood <gsherwood@squiz.net>
26
 * @author    Marc McIntyre <mmcintyre@squiz.net>
27
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
28
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
29
 * @version   Release: 1.2.0RC1
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
class PHP_CodeSniffer_DocGenerators_Generator
33
{
34
 
35
    /**
36
     * The name of the coding standard we are generating docs for.
37
     *
38
     * @var string
39
     */
40
    private $_standard = '';
41
 
42
    /**
43
     * An array of sniffs that we are limiting the generated docs to.
44
     *
45
     * If this array is empty, docs are generated for all sniffs in the
46
     * supplied coding standard.
47
     *
48
     * @var string
49
     */
50
    private $_sniffs = array();
51
 
52
 
53
    /**
54
     * Constructs a PHP_CodeSniffer_DocGenerators_Generator object.
55
     *
56
     * @param string $standard The name of the coding standard to generate
57
     *                         docs for.
58
     * @param array  $sniffs   An array of sniffs that we are limiting the
59
     *                         generated docs to.
60
     *
61
     * @see generate()
62
     */
63
    public function __construct($standard, array $sniffs=array())
64
    {
65
        $this->_standard = $standard;
66
        $this->_sniffs   = $sniffs;
67
 
68
    }//end __construct()
69
 
70
 
71
    /**
72
     * Retrieves the title of the sniff from the DOMNode supplied.
73
     *
74
     * @param DOMNode $doc The DOMNode object for the sniff.
75
     *                     It represents the "documentation" tag in the XML
76
     *                     standard file.
77
     *
78
     * @return string
79
     */
80
    protected function getTitle(DOMNode $doc)
81
    {
82
        return $doc->getAttribute('title');
83
 
84
    }//end getTitle()
85
 
86
 
87
    /**
88
     * Retrieves the name of the standard we are generating docs for.
89
     *
90
     * @return string
91
     */
92
    protected function getStandard()
93
    {
94
        return $this->_standard;
95
 
96
    }//end getStandard()
97
 
98
 
99
    /**
100
     * Generates the documentation for a standard.
101
     *
102
     * It's probably wise for doc generators to override this method so they
103
     * have control over how the docs are produced. Otherwise, the processSniff
104
     * method should be overridden to output content for each sniff.
105
     *
106
     * @return void
107
     * @see processSniff()
108
     */
109
    public function generate()
110
    {
111
        $standardFiles = $this->getStandardFiles();
112
 
113
        foreach ($standardFiles as $standard) {
114
            $doc = new DOMDocument();
115
            $doc->load($standard);
116
            $documentation = $doc->getElementsByTagName('documentation')->item(0);
117
            $this->processSniff($documentation);
118
        }
119
 
120
    }//end generate()
121
 
122
 
123
    /**
124
     * Returns a list of paths to XML standard files for all sniffs in a standard.
125
     *
126
     * Any sniffs that do not have an XML standard file are obviously not included
127
     * in the returned array. If documentation is only being generated for some
128
     * sniffs (ie. $this->_sniffs is not empty) then all others sniffs will
129
     * be filtered from the results as well.
130
     *
131
     * @return array(string)
132
     */
133
    protected function getStandardFiles()
134
    {
135
        if (is_dir($this->_standard) === true) {
136
            // This is a custom standard.
137
            $standardDir = $this->_standard;
138
            $standard    = basename($this->_standard);
139
        } else {
140
            $standardDir
141
                = realpath(dirname(__FILE__).'/../Standards/'.$this->_standard);
142
            $standard = $this->_standard;
143
        }
144
 
145
        $sniffs = PHP_CodeSniffer::getSniffFiles($standardDir, $standard);
146
 
147
        $standardFiles = array();
148
        foreach ($sniffs as $sniff) {
149
            if (empty($this->_sniffs) === false) {
150
                // We are limiting the docs to certain sniffs only, so filter
151
                // out any unwanted sniffs.
152
                $sniffName = substr($sniff, (strrpos($sniff, '/') + 1));
153
                $sniffName = substr($sniffName, 0, -9);
154
                if (in_array($sniffName, $this->_sniffs) === false) {
155
                    continue;
156
                }
157
            }
158
 
159
            $standardFile= str_replace(
160
                DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR,
161
                DIRECTORY_SEPARATOR.'Docs'.DIRECTORY_SEPARATOR,
162
                $sniff
163
            );
164
            $standardFile = str_replace('Sniff.php', 'Standard.xml', $standardFile);
165
 
166
            if (is_file($standardFile) === true) {
167
                $standardFiles[] = $standardFile;
168
            }
169
        }
170
 
171
        return $standardFiles;
172
 
173
    }//end getStandardFiles()
174
 
175
 
176
    /**
177
     * Process the documentation for a single sniff.
178
     *
179
     * Doc generators should override this function to produce output.
180
     *
181
     * @param DOMNode $doc The DOMNode object for the sniff.
182
     *                     It represents the "documentation" tag in the XML
183
     *                     standard file.
184
     *
185
     * @return void
186
     * @see generate()
187
     */
188
    protected function processSniff(DOMNode $doc)
189
    {
190
 
191
    }//end processSniff()
192
 
193
 
194
}//end class
195
 
196
?>