Failed Conditions
Push — master ( c35fd9...2f93af )
by Alexander
02:57
created

ValidClassNameSniff::process()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 2
dl 0
loc 27
ccs 18
cts 19
cp 0.9474
crap 6.0052
rs 8.8657
c 0
b 0
f 0
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
namespace CodingStandard\Sniffs\NamingConventions;
16
17
use PHP_CodeSniffer\Files\File;
18
use PHP_CodeSniffer\Sniffs\Sniff;
19
20
/**
21
 * CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff.
22
 *
23
 * Throws errors if abstract class names are not prefixed with "Abstract".
24
 *
25
 * @category PHP
26
 * @package  PHP_CodeSniffer
27
 * @author   Alexander Obuhovich <[email protected]>
28
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
29
 * @link     https://github.com/aik099/CodingStandard
30
 */
31
class ValidClassNameSniff implements Sniff
32
{
33
34
    /**
35
     * A list of tokenizers this sniff supports.
36
     *
37
     * @var array
38
     */
39
    public $supportedTokenizers = array('PHP');
40
41
42
    /**
43
     * Returns an array of tokens this test wants to listen for.
44
     *
45
     * @return integer[]
46
     */
47 1
    public function register()
48
    {
49 1
        return array(T_CLASS);
50
    }//end register()
51
52
53
    /**
54
     * Processes this test, when one of its tokens is encountered.
55
     *
56
     * @param File $phpcsFile The file being scanned.
57
     * @param int  $stackPtr  The position of the current token in the
58
     *                        stack passed in $tokens.
59
     *
60
     * @return void
61
     */
62 1
    public function process(File $phpcsFile, $stackPtr)
63
    {
64 1
        $className = $phpcsFile->getDeclarationName($stackPtr);
65
66 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...
67
            return;
68
        }
69
70 1
        $classProperties = $phpcsFile->getClassProperties($stackPtr);
71 1
        $hasAbstractName = substr($className, 0, 8) === 'Abstract';
72
73 1
        if ($classProperties['is_abstract'] === true && $hasAbstractName === false) {
74 1
            $phpcsFile->addError(
75 1
                'Abstract class name "%s" is not prefixed with "Abstract"',
76 1
                $stackPtr,
77 1
                'AbstractWrongName',
78 1
                array($className)
79
            );
80 1
        } elseif ($classProperties['is_abstract'] === false && $hasAbstractName === true) {
81 1
            $phpcsFile->addError(
82 1
                'Non-abstract class name "%s" is prefixed with "Abstract"',
83 1
                $stackPtr,
84 1
                'AbstractMissingModifier',
85 1
                array($className)
86
            );
87
        }
88 1
    }//end process()
89
}//end class
90