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

ClassNamespaceSniff   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 14
cts 14
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 14 2
A process() 0 11 2
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