Failed Conditions
Push — in-portal ( bd8962...3cf23c )
by Alexander
02:37 queued 47s
created

ValidClassNameSniff::process()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0033
Metric Value
dl 0
loc 28
ccs 21
cts 22
cp 0.9545
rs 8.439
cc 6
eloc 18
nc 4
nop 2
crap 6.0033
1
<?php
2
/**
3
 * CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Symfony2-phpcs-authors <[email protected]>
10
 * @author   Alexander Obuhovich <[email protected]>
11
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
12
 * @link     https://github.com/aik099/CodingStandard
13
 */
14
15
/**
16
 * CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff.
17
 *
18
 * Throws errors if abstract class names are not prefixed with "Abstract".
19
 *
20
 * @category PHP
21
 * @package  PHP_CodeSniffer
22
 * @author   Alexander Obuhovich <[email protected]>
23
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
24
 * @link     https://github.com/aik099/CodingStandard
25
 */
26
class CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
27
{
28
29
    /**
30
     * A list of tokenizers this sniff supports.
31
     *
32
     * @var array
33
     */
34
    public $supportedTokenizers = array('PHP');
35
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return integer[]
41
     */
42 1
    public function register()
43
    {
44 1
        return array(T_CLASS);
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 All the tokens found in the document.
53
     * @param int                  $stackPtr  The position of the current token in
54
     *                                        the stack passed in $tokens.
55
     *
56
     * @return void
57
     */
58 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59
    {
60 1
        $className = $phpcsFile->getDeclarationName($stackPtr);
61
62 1
        if (!$className) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $className of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
            return;
64
        }
65
66 1
        $classProperties = $phpcsFile->getClassProperties($stackPtr);
67 1
        $hasAbstractName = substr($className, 0, 8) === 'Abstract';
68
69 1
        if ($classProperties['is_abstract'] === true && $hasAbstractName === false) {
70 1
            $phpcsFile->addError(
71 1
                'Abstract class name "%s" is not prefixed with "Abstract"',
72 1
                $stackPtr,
73 1
                'AbstractWrongName',
74 1
                array($className)
75 1
            );
76 1
        } elseif ($classProperties['is_abstract'] === false && $hasAbstractName === true) {
77 1
            $phpcsFile->addError(
78 1
                'Non-abstract class name "%s" is prefixed with "Abstract"',
79 1
                $stackPtr,
80 1
                'AbstractMissingModifier',
81 1
                array($className)
82 1
            );
83 1
        }
84
85 1
    }//end process()
86
87
88
}//end class
89