Completed
Push — in-portal ( 542489...3b6de0 )
by Alexander
04:02
created

CodingStandard_Sniffs_Files_OneTraitPerFileSniff   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
ccs 9
cts 9
cp 1
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A OneTraitPerFileSniff::register() 0 4 1
A OneTraitPerFileSniff::process() 0 8 2
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Files_OneTraitPerFileSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Alexander Obuhovich <[email protected]>
10
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
11
 * @link     https://github.com/aik099/CodingStandard
12
 */
13
14
namespace CodingStandard\Sniffs\Files;
15
16
use PHP_CodeSniffer\Files\File;
17
use PHP_CodeSniffer\Sniffs\Sniff;
18
19
/**
20
 * Checks that only one trait is declared per file.
21
 *
22
 * @category PHP
23
 * @package  PHP_CodeSniffer
24
 * @author   Alexander Obuhovich <[email protected]>
25
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
26
 * @link     https://github.com/aik099/CodingStandard
27
 */
28
class OneTraitPerFileSniff implements Sniff
29
{
30
31
32
    /**
33
     * Returns an array of tokens this test wants to listen for.
34
     *
35
     * @return integer[]
36
     */
37 1
    public function register()
38
    {
39 1
        return array(T_TRAIT);
40
    }//end register()
41
42
43
    /**
44
     * Processes this test, when one of its tokens is encountered.
45
     *
46
     * @param File $phpcsFile The file being scanned.
47
     * @param int  $stackPtr  The position of the current token in the
48
     *                        stack passed in $tokens.
49
     *
50
     * @return void
51
     */
52 1
    public function process(File $phpcsFile, $stackPtr)
53
    {
54 1
        $nextClass = $phpcsFile->findNext($this->register(), ($stackPtr + 1));
55 1
        if ($nextClass !== false) {
56 1
            $error = 'Only one trait is allowed in a file';
57 1
            $phpcsFile->addError($error, $nextClass, 'MultipleFound');
58 1
        }
59 1
    }//end process()
60
}//end class
61