Failed Conditions
Push — master ( a46fbb...c35fd9 )
by Alexander
01:13
created

ValidTraitNameSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 14 3
1
<?php
2
/**
3
 * CodingStandard_Sniffs_NamingConventions_ValidTraitNameSniff.
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_ValidTraitNameSniff.
22
 *
23
 * Throws errors if trait names are not prefixed with "T".
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 ValidTraitNameSniff 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_TRAIT);
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
        $traitName    = $phpcsFile->getDeclarationName($stackPtr);
65 1
        $firstLetter  = $traitName[0];
66 1
        $secondLetter = $traitName[1];
67
68 1
        if ($firstLetter !== 'T' || $secondLetter === strtolower($secondLetter)) {
69 1
            $phpcsFile->addError(
70 1
                'Trait name is not prefixed with "T"',
71 1
                $stackPtr,
72
                'WrongPrefix'
73 1
            );
74 1
        }
75 1
    }//end process()
76
}//end class
77