~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.3/CodeSniffer/Standards/PSR1/Sniffs/Classes/ClassDeclarationSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140721144241-g4orlcuk4jzn9mhs
Tags: 1.5.3-1
* Team upload
* Focus on stable release
* Update copyright
* Bump standards version to 3.9.5
* Update Homepage
* Use ${phppear:…} instead of hardcoding them
* Run tests in dh_auto_test
* Update patch with gbp pq
* Simplify configuration installation
* Edit package.xml to move script
* Add DEP-8 tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Class Declaration Test.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer
 
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
10
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
11
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
13
 */
 
14
 
 
15
/**
 
16
 * Class Declaration Test.
 
17
 *
 
18
 * Checks the declaration of the class is correct.
 
19
 *
 
20
 * @category  PHP
 
21
 * @package   PHP_CodeSniffer
 
22
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
23
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
24
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
25
 * @version   Release: 1.5.3
 
26
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
27
 */
 
28
class PSR1_Sniffs_Classes_ClassDeclarationSniff implements PHP_CodeSniffer_Sniff
 
29
{
 
30
 
 
31
 
 
32
    /**
 
33
     * Returns an array of tokens this test wants to listen for.
 
34
     *
 
35
     * @return array
 
36
     */
 
37
    public function register()
 
38
    {
 
39
        return array(
 
40
                T_CLASS,
 
41
                T_INTERFACE,
 
42
                T_TRAIT,
 
43
               );
 
44
 
 
45
    }//end register()
 
46
 
 
47
 
 
48
    /**
 
49
     * Processes this test, when one of its tokens is encountered.
 
50
     *
 
51
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
52
     * @param integer              $stackPtr  The position of the current token in
 
53
     *                                        the token stack.
 
54
     *
 
55
     * @return void
 
56
     */
 
57
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
58
    {
 
59
        $tokens    = $phpcsFile->getTokens();
 
60
        $errorData = array(strtolower($tokens[$stackPtr]['content']));
 
61
 
 
62
        $nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_TRAIT), ($stackPtr + 1));
 
63
        if ($nextClass !== false) {
 
64
            $error = 'Each %s must be in a file by itself';
 
65
            $phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
 
66
        }
 
67
 
 
68
        if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
 
69
            $namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
 
70
            if ($namespace === false) {
 
71
                $error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
 
72
                $phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $errorData);
 
73
            }
 
74
        }
 
75
 
 
76
    }//end process()
 
77
 
 
78
 
 
79
}//end class
 
80
 
 
81
?>