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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-2.3.3/CodeSniffer/Standards/Squiz/Sniffs/Classes/ValidClassNameSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood
  • Date: 2015-06-24 13:41:36 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20150624134136-dv60dnl6s20tdxwr
Tags: 2.3.3-1
[ Greg Sherwood ]
Prepare for 2.3.3 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Squiz_Sniffs_Classes_ValidClassNameSniff.
 
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
 
 
16
/**
 
17
 * Squiz_Sniffs_Classes_ValidClassNameSniff.
 
18
 *
 
19
 * Ensures classes are in camel caps, and the first letter is capitalised
 
20
 *
 
21
 * @category  PHP
 
22
 * @package   PHP_CodeSniffer
 
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
24
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
25
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
26
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
27
 * @version   Release: 2.3.3
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
class Squiz_Sniffs_Classes_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
 
31
{
 
32
 
 
33
 
 
34
    /**
 
35
     * Returns an array of tokens this test wants to listen for.
 
36
     *
 
37
     * @return array
 
38
     */
 
39
    public function register()
 
40
    {
 
41
        return array(
 
42
                T_CLASS,
 
43
                T_INTERFACE,
 
44
               );
 
45
 
 
46
    }//end register()
 
47
 
 
48
 
 
49
    /**
 
50
     * Processes this test, when one of its tokens is encountered.
 
51
     *
 
52
     * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
 
53
     * @param int                  $stackPtr  The position of the current token in the
 
54
     *                                        stack passed in $tokens.
 
55
     *
 
56
     * @return void
 
57
     */
 
58
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
59
    {
 
60
        $tokens = $phpcsFile->getTokens();
 
61
 
 
62
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
 
63
            $error = 'Possible parse error: %s missing opening or closing brace';
 
64
            $data  = array($tokens[$stackPtr]['content']);
 
65
            $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
 
66
            return;
 
67
        }
 
68
 
 
69
        // Determine the name of the class or interface. Note that we cannot
 
70
        // simply look for the first T_STRING because a class name
 
71
        // starting with the number will be multiple tokens.
 
72
        $opener    = $tokens[$stackPtr]['scope_opener'];
 
73
        $nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
 
74
        $nameEnd   = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
 
75
        $name      = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
 
76
 
 
77
        // Check for camel caps format.
 
78
        $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false);
 
79
        if ($valid === false) {
 
80
            $type  = ucfirst($tokens[$stackPtr]['content']);
 
81
            $error = '%s name "%s" is not in camel caps format';
 
82
            $data  = array(
 
83
                      $type,
 
84
                      $name,
 
85
                     );
 
86
            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
 
87
            $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no');
 
88
        } else {
 
89
            $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes');
 
90
        }
 
91
 
 
92
    }//end process()
 
93
 
 
94
 
 
95
}//end class