Failed Conditions
Push — phpcs-3-upgrade ( d91341 )
by Alexander
02:06
created

ValidTraitNameSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
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
    public function register()
48
    {
49
        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
    public function process(File $phpcsFile, $stackPtr)
63
    {
64
        $traitName    = $phpcsFile->getDeclarationName($stackPtr);
65
        $firstLetter  = $traitName[0];
66
        $secondLetter = $traitName[1];
67
68
        if ($firstLetter !== 'T' || $secondLetter === strtolower($secondLetter)) {
69
            $phpcsFile->addError(
70
                'Trait name is not prefixed with "T"',
71
                $stackPtr,
72
                'WrongPrefix'
73
            );
74
        }
75
    }//end process()
76
}//end class
77