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

ClassNamespaceSniff::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Classes_ClassNamespaceSniff.
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\Classes;
15
16
use PHP_CodeSniffer\Files\File;
17
use PHP_CodeSniffer\Sniffs\Sniff;
18
19
/**
20
 * Class Namespace Test.
21
 *
22
 * Checks the that of the class is namespaced.
23
 *
24
 * @category PHP
25
 * @package  PHP_CodeSniffer
26
 * @author   Alexander Obuhovich <[email protected]>
27
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
28
 * @link     https://github.com/aik099/CodingStandard
29
 */
30
class ClassNamespaceSniff implements Sniff
31
{
32
33
34
    /**
35
     * Returns an array of tokens this test wants to listen for.
36
     *
37
     * @return array
38
     */
39 1
    public function register()
40
    {
41
        // @codeCoverageIgnoreStart
42
        if (version_compare(PHP_VERSION, '5.3.0') < 0) {
43
            return array();
44
        }
45
        // @codeCoverageIgnoreEnd
46
47
        return array(
48 1
                T_CLASS,
49 1
                T_INTERFACE,
50 1
                T_TRAIT,
51 1
               );
52
    }//end register()
53
54
55
    /**
56
     * Processes this test, when one of its tokens is encountered.
57
     *
58
     * @param File $phpcsFile The file being scanned.
59
     * @param int  $stackPtr  The position of the current token in the
60
     *                        stack passed in $tokens.
61
     *
62
     * @return void
63
     */
64 1
    public function process(File $phpcsFile, $stackPtr)
65
    {
66 1
        $tokens    = $phpcsFile->getTokens();
67 1
        $namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
68
69 1
        if ($namespace === false) {
70 1
            $error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
71 1
            $data  = array($tokens[$stackPtr]['content']);
72 1
            $phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $data);
73 1
        }
74 1
    }//end process()
75
}//end class
76