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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidClassNameSniff.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
 
 * PEAR_Sniffs_NamingConventions_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-2012 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
 
 * PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
18
 
 *
19
 
 * Ensures class and interface names start with a capital letter
20
 
 * and use _ separators.
21
 
 *
22
 
 * @category  PHP
23
 
 * @package   PHP_CodeSniffer
24
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 
 * @author    Marc McIntyre <mmcintyre@squiz.net>
26
 
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
27
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
28
 
 * @version   Release: 1.5.0RC2
29
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 
 */
31
 
class PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
32
 
{
33
 
 
34
 
 
35
 
    /**
36
 
     * Returns an array of tokens this test wants to listen for.
37
 
     *
38
 
     * @return array
39
 
     */
40
 
    public function register()
41
 
    {
42
 
        return array(
43
 
                T_CLASS,
44
 
                T_INTERFACE,
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
55
 
     *                                        in the 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
 
        $className = $phpcsFile->findNext(T_STRING, $stackPtr);
64
 
        $name      = trim($tokens[$className]['content']);
65
 
        $errorData = array(ucfirst($tokens[$stackPtr]['content']));
66
 
 
67
 
        // Make sure the first letter is a capital.
68
 
        if (preg_match('|^[A-Z]|', $name) === 0) {
69
 
            $error = '%s name must begin with a capital letter';
70
 
            $phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
71
 
        }
72
 
 
73
 
        // Check that each new word starts with a capital as well, but don't
74
 
        // check the first word, as it is checked above.
75
 
        $validName = true;
76
 
        $nameBits  = explode('_', $name);
77
 
        $firstBit  = array_shift($nameBits);
78
 
        foreach ($nameBits as $bit) {
79
 
            if ($bit === '' || $bit{0} !== strtoupper($bit{0})) {
80
 
                $validName = false;
81
 
                break;
82
 
            }
83
 
        }
84
 
 
85
 
        if ($validName === false) {
86
 
            // Strip underscores because they cause the suggested name
87
 
            // to be incorrect.
88
 
            $nameBits = explode('_', trim($name, '_'));
89
 
            $firstBit = array_shift($nameBits);
90
 
            if ($firstBit === '') {
91
 
                $error = '%s name is not valid';
92
 
                $phpcsFile->addError($error, $stackPtr, 'Invalid', $errorData);
93
 
            } else {
94
 
                $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
95
 
                foreach ($nameBits as $bit) {
96
 
                    if ($bit !== '') {
97
 
                        $newName .= strtoupper($bit{0}).substr($bit, 1).'_';
98
 
                    }
99
 
                }
100
 
 
101
 
                $newName = rtrim($newName, '_');
102
 
                $error   = '%s name is not valid; consider %s instead';
103
 
                $data    = $errorData;
104
 
                $data[]  = $newName;
105
 
                $phpcsFile->addError($error, $stackPtr, 'Invalid', $data);
106
 
            }
107
 
        }//end if
108
 
 
109
 
    }//end process()
110
 
 
111
 
 
112
 
}//end class
113
 
 
114
 
 
115
 
?>