PageRenderTime 119ms CodeModel.GetById 75ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 1ms

/.dev/code-sniffs/XLite/Sniffs/PHP/Commenting/FileCommentSniff.php

https://github.com/istran/core
PHP | 310 lines | 206 code | 37 blank | 67 comment | 24 complexity | 244675eaaa5d41ddfa5c8077c162ea3e MD5 | raw file
  1<?php
  2/**
  3 * Parses and verifies the doc comments for files.
  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: fc8e74ee8c1e42ce060817653579cd5219024aed $
 14 * @link      http://pear.php.net/package/PHP_CodeSniffer
 15 */
 16
 17if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
 18    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found');
 19}
 20
 21/**
 22 * Parses and verifies the doc comments for files.
 23 *
 24 * Verifies that :
 25 * <ul>
 26 *  <li>A doc comment exists.</li>
 27 *  <li>There is a blank newline after the short description.</li>
 28 *  <li>There is a blank newline between the long and short description.</li>
 29 *  <li>There is a blank newline between the long description and tags.</li>
 30 *  <li>A PHP version is specified.</li>
 31 *  <li>Check the order of the tags.</li>
 32 *  <li>Check the indentation of each tag.</li>
 33 *  <li>Check required and optional tags and the format of their content.</li>
 34 * </ul>
 35 *
 36 * @category  PHP
 37 * @package   PHP_CodeSniffer
 38 * @author    Greg Sherwood <gsherwood@squiz.net>
 39 * @author    Marc McIntyre <mmcintyre@squiz.net>
 40 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 41 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 42 * @version   Release: 1.2.0RC1
 43 * @link      http://pear.php.net/package/PHP_CodeSniffer
 44 */
 45
 46class XLite_Sniffs_PHP_Commenting_FileCommentSniff extends XLite_TagsSniff
 47{
 48
 49    /**
 50     * Tags in correct order and related info.
 51     *
 52     * @var array
 53     */
 54    protected $tags = array(
 55                       'category'   => array(
 56                                        'required'       => true,
 57                                        'allow_multiple' => false,
 58                                        'order_text'     => 'precedes @package',
 59                                       ),
 60                       'package'    => array(
 61                                        'required'       => true,
 62                                        'allow_multiple' => false,
 63                                        'order_text'     => 'follows @category',
 64                                       ),
 65                       'subpackage' => array(
 66                                        'required'       => false,
 67                                        'allow_multiple' => false,
 68                                        'order_text'     => 'follows @package',
 69                                       ),
 70                       'author'     => array(
 71                                        'required'       => true,
 72                                        'allow_multiple' => false,
 73                                        'order_text'     => 'follows @subpackage (if used) or @package',
 74                                       ),
 75                       'copyright'  => array(
 76                                        'required'       => true,
 77                                        'allow_multiple' => false,
 78                                        'order_text'     => 'follows @author',
 79                                       ),
 80                       'license'    => array(
 81                                        'required'       => true,
 82                                        'allow_multiple' => false,
 83                                        'order_text'     => 'follows @copyright (if used) or @author',
 84                                       ),
 85                       'version'    => array(
 86                                        'required'       => true,
 87                                        'allow_multiple' => false,
 88                                        'order_text'     => 'follows @license',
 89                                       ),
 90                       'link'       => array(
 91                                        'required'       => true,
 92                                        'allow_multiple' => false,
 93                                        'order_text'     => 'follows @version',
 94                                       ),
 95                       'see'        => array(
 96                                        'required'       => false,
 97                                        'allow_multiple' => false,
 98                                        'order_text'     => 'follows @link',
 99                                       ),
100                       'since'      => array(
101                                        'required'       => false,
102                                        'allow_multiple' => false,
103                                        'order_text'     => 'follows @see (if used) or @link',
104                                       ),
105                       'deprecated' => array(
106                                        'required'       => false,
107                                        'allow_multiple' => false,
108                                        'order_text'     => 'follows @since (if used) or @see (if used) or @link',
109                                       ),
110                );
111
112
113	protected $reqCodeRequire = 'REQ.PHP.4.3.4';
114	protected $reqCodePHPVersion = 'REQ.PHP.4.3.3';
115	protected $reqCodeForbidden = 'REQ.PHP.4.3.9';
116	protected $reqCodeOnlyOne = 'REQ.PHP.4.3.8';
117
118	protected $docBlock = 'file';
119
120    /**
121     * Processes this test, when one of its tokens is encountered.
122     *
123     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
124     * @param int                  $stackPtr  The position of the current token
125     *                                        in the stack passed in $tokens.
126     *
127     * @return void
128     */
129    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
130    {
131        $this->currentFile = $phpcsFile;
132
133        // We are only interested if this is the first open tag.
134        if ($stackPtr !== 0) {
135            if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
136                return;
137            }
138        }
139
140        $tokens = $phpcsFile->getTokens();
141
142        // Find the next non whitespace token.
143        $commentStart
144            = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
145
146		if ($tokens[$commentStart]['code'] !== T_COMMENT) {
147			$phpcsFile->addError(
148				$this->getReqPrefix('REQ.PHP.4.3.5') . 'Файл должен начинаться с однострочного комментария',
149				$commentStart
150			);
151
152		} elseif (!preg_match('/vim: set ts=\d sw=\d sts=\d et:/', $tokens[$commentStart]['content'])) {
153            $phpcsFile->addError(
154                $this->getReqPrefix('REQ.PHP.4.3.5') . 'Файл должен начинаться с однострочного комментария с директивой для vim',
155                $commentStart
156            );
157		}
158
159        $commentStart = $phpcsFile->findNext(
160            T_WHITESPACE,
161            ($commentStart + 1),
162            null,
163            true
164        );
165
166        $errorToken = ($stackPtr + 1);
167        if (isset($tokens[$errorToken]) === false) {
168            $errorToken--;
169        }
170
171        if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
172            // We are only interested if this is the first open tag.
173            return;
174
175        } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
176            $error = 'You must use "/**" style comments for a file comment';
177            $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.7') . $error, $errorToken);
178            return;
179        } else if ($commentStart === false
180            || $tokens[$commentStart]['code'] !== T_DOC_COMMENT
181        ) {
182            $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.1') . 'Missing file doc comment', $errorToken);
183            return;
184        } else {
185
186            // Extract the header comment docblock.
187            $commentEnd = $phpcsFile->findNext(
188                T_DOC_COMMENT,
189                ($commentStart + 1),
190                null,
191                true
192            );
193
194            $commentEnd--;
195
196            // Check if there is only 1 doc comment between the
197            // open tag and class token.
198            $nextToken   = array(
199                            T_ABSTRACT,
200                            T_CLASS,
201                            T_FUNCTION,
202                            T_DOC_COMMENT,
203                           );
204
205            $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1));
206            if ($commentNext !== false
207                && $tokens[$commentNext]['code'] !== T_DOC_COMMENT
208            ) {
209                // Found a class token right after comment doc block.
210                $newlineToken = $phpcsFile->findNext(
211                    T_WHITESPACE,
212                    ($commentEnd + 1),
213                    $commentNext,
214                    false,
215                    $phpcsFile->eolChar
216                );
217
218                if ($newlineToken !== false) {
219                    $newlineToken = $phpcsFile->findNext(
220                        T_WHITESPACE,
221                        ($newlineToken + 1),
222                        $commentNext,
223                        false,
224                        $phpcsFile->eolChar
225                    );
226
227                    if ($newlineToken === false) {
228                        // No blank line between the class token and the doc block.
229                        // The doc block is most likely a class comment.
230                        $error = 'Missing file doc comment';
231                        $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.6') . $error, $errorToken);
232                        return;
233                    }
234                }
235            }//end if
236
237            $comment = $phpcsFile->getTokensAsString(
238                $commentStart,
239                ($commentEnd - $commentStart + 1)
240            );
241
242            // Parse the header comment docblock.
243            try {
244                $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
245                $this->commentParser->parse();
246            } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
247                $line = ($e->getLineWithinComment() + $commentStart);
248                $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.1') . $e->getMessage(), $line);
249                return;
250            }
251
252            $comment = $this->commentParser->getComment();
253            if (is_null($comment) === true) {
254                $error = 'File doc comment is empty';
255                $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.3.2') . $error, $commentStart);
256                return;
257            }
258
259            // No extra newline before short description.
260            $short        = $comment->getShortComment();
261            $newlineCount = 0;
262            $newlineSpan  = strspn($short, $phpcsFile->eolChar);
263            if ($short !== '' && $newlineSpan > 0) {
264                $line  = ($newlineSpan > 1) ? 'newlines' : 'newline';
265                $error = "Extra $line found before file comment short description";
266                $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.7') . $error, ($commentStart + 1));
267            }
268
269            $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
270
271            // Exactly one blank line between short and long description.
272            $long = $comment->getLongComment();
273            if (empty($long) === false) {
274                $between        = $comment->getWhiteSpaceBetween();
275                $newlineBetween = substr_count($between, $phpcsFile->eolChar);
276                if ($newlineBetween !== 2) {
277                    $error = 'There must be exactly one blank line between descriptions in file comment';
278                    $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.19') . $error, ($commentStart + $newlineCount + 1));
279                }
280
281                $newlineCount += $newlineBetween;
282            }
283
284            // Exactly one blank line before tags.
285            $tags = $this->commentParser->getTagOrders();
286            if (count($tags) > 1) {
287                $newlineSpan = $comment->getNewlineAfter();
288                if ($newlineSpan !== 2) {
289                    $error = 'There must be exactly one blank line before the tags in file comment';
290                    if ($long !== '') {
291                        $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
292                    }
293
294                    $phpcsFile->addError($this->getReqPrefix('REQ.PHP.4.1.18') . $error, ($commentStart + $newlineCount));
295                    $short = rtrim($short, $phpcsFile->eolChar.' ');
296                }
297            }
298
299            // Check the PHP Version.
300            $this->processPHPVersion($commentStart, $commentEnd, $long);
301
302            // Check each tag.
303            $this->processTags($commentStart, $commentEnd);
304        }//end if
305
306    }//end process()
307
308}//end class
309
310?>