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: @package_version@
28 * @link      http://pear.php.net/package/PHP_CodeSniffer
29 */
30class 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                T_TRAIT,
45               );
46
47    }//end register()
48
49
50    /**
51     * Processes this test, when one of its tokens is encountered.
52     *
53     * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
54     * @param int                  $stackPtr  The position of the current token in the
55     *                                        stack passed in $tokens.
56     *
57     * @return void
58     */
59    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60    {
61        $tokens = $phpcsFile->getTokens();
62
63        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
64            $error = 'Possible parse error: %s missing opening or closing brace';
65            $data  = array($tokens[$stackPtr]['content']);
66            $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
67            return;
68        }
69
70        // Determine the name of the class or interface. Note that we cannot
71        // simply look for the first T_STRING because a class name
72        // starting with the number will be multiple tokens.
73        $opener    = $tokens[$stackPtr]['scope_opener'];
74        $nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
75        $nameEnd   = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
76        if ($nameEnd === false) {
77            $name = $tokens[$nameStart]['content'];
78        } else {
79            $name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
80        }
81
82        // Check for camel caps format.
83        $valid = PHP_CodeSniffer::isCamelCaps($name, true, true, false);
84        if ($valid === false) {
85            $type  = ucfirst($tokens[$stackPtr]['content']);
86            $error = '%s name "%s" is not in camel caps format';
87            $data  = array(
88                      $type,
89                      $name,
90                     );
91            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
92            $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'no');
93        } else {
94            $phpcsFile->recordMetric($stackPtr, 'CamelCase class name', 'yes');
95        }
96
97    }//end process()
98
99
100}//end class
101