ValidClassNameSniff   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 1
A process() 0 16 2
1
<?php
2
/**
3
 * CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Thomas Ernest <[email protected]>
10
 * @copyright 2010 Thomas Ernest
11
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
/**
16
 * CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff.
17
 *
18
 * Ensures that class and interface names have their first letter uppercase
19
 * and that words are separated with an underscore, and not CamelCased.
20
 *
21
 * @todo Try to avoid overly long and verbose names in using property rule and
22
 * configuration variable to set limits. Have a look at
23
 * CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff.
24
 *
25
 * @category  PHP
26
 * @package   PHP_CodeSniffer
27
 * @author    Thomas Ernest <[email protected]>
28
 * @copyright 2010 Thomas Ernest
29
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
33
namespace CodeIgniter\Sniffs\NamingConventions;
34
35
use PHP_CodeSniffer\Sniffs\Sniff;
36
use PHP_CodeSniffer\Files\File;
37
38
class ValidClassNameSniff implements Sniff
39
{
40
41
42
    /**
43
     * Returns an array of tokens this test wants to listen for.
44
     *
45
     * @return array
46
     */
47
    public function register()
48
    {
49
        return array(
50
            T_CLASS,
51
            T_INTERFACE,
52
        );
53
54
    }//end register()
55
56
    /**
57
     * Processes this test, when one of its tokens is encountered.
58
     *
59
     * @param File $phpcsFile The current file being processed.
60
     * @param int                  $stackPtr  The position of the current token
61
     *                                        in the stack passed in $tokens.
62
     *
63
     * @return void
64
     */
65
    public function process(File $phpcsFile, $stackPtr)
66
    {
67
        // get the class name
68
        $className = trim($phpcsFile->getDeclarationName($stackPtr));
69
        // compute the expected class name
70
        // [^_] means "something different from _", but not "nothing or something different from _"
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
        $lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className);
72
        $expectedClassName
73
            = strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1));
74
        // ensures that the current class name
75
        // and the expected class name are identical
76
        if (0 !== strcmp($className, $expectedClassName)) {
77
            $error =  'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 234 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
            $phpcsFile->addError($error, $stackPtr);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
79
        }
80
    }//end process()
81
82
}//end class
83
84
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
85