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

NamespaceDeclarationSniff::process()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
nc 16
nop 2
dl 0
loc 53
ccs 34
cts 34
cp 1
crap 9
rs 7.4698
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CodingStandard_Sniffs_Formatting_NamespaceDeclarationSniff.
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\Formatting;
15
16
use PHP_CodeSniffer\Files\File;
17
use PHP_CodeSniffer\Sniffs\Sniff;
18
19
/**
20
 * Namespace formatting test.
21
 *
22
 * Checks the that of empty lines present after namespace.
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 NamespaceDeclarationSniff implements Sniff
31
{
32
33
    /**
34
     * Empty line count after namespace declaration.
35
     *
36
     * @var int
37
     */
38
    public $emptyLineCount = 2;
39
40
41
    /**
42
     * Returns an array of tokens this test wants to listen for.
43
     *
44
     * @return integer[]
45
     */
46 1
    public function register()
47
    {
48 1
        return array(T_NAMESPACE);
49
    }//end register()
50
51
52
    /**
53
     * Processes this test, when one of its tokens is encountered.
54
     *
55
     * @param File $phpcsFile The file being scanned.
56
     * @param int  $stackPtr  The position of the current token in the
57
     *                        stack passed in $tokens.
58
     *
59
     * @return void
60
     */
61 1
    public function process(File $phpcsFile, $stackPtr)
62
    {
63 1
        $tokens            = $phpcsFile->getTokens();
64 1
        $declarationEndPtr = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
65
66 1
        $nextCodePtr = $phpcsFile->findNext(
67 1
            T_WHITESPACE,
68 1
            ($declarationEndPtr + 1),
69 1
            null,
70
            true
71 1
        );
72
73 1
        if ($nextCodePtr === false) {
74 1
            return;
75
        }
76
77 1
        $diff = ($tokens[$nextCodePtr]['line'] - $tokens[$declarationEndPtr]['line'] - 1);
78
79 1
        if ($diff === $this->emptyLineCount) {
80 1
            return;
81
        }
82
83 1
        if ($diff < 0) {
84 1
            $diff = 0;
85 1
        }
86
87 1
        $data  = array($diff);
88 1
        $error = 'Expected '.$this->emptyLineCount.' blank line(-s) after namespace declaration; %s found';
89 1
        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'BlankLineAfter', $data);
90
91 1
        if ($fix === false) {
92 1
            return;
93
        }
94
95 1
        $phpcsFile->fixer->beginChangeset();
96
97 1
        if ($diff > 0) {
98 1
            for ($j = ($declarationEndPtr + 1); $j < $nextCodePtr; $j++) {
99 1
                if ($tokens[$j]['line'] === $tokens[$nextCodePtr]['line']) {
100
                    // Keep existing indentation.
101 1
                    break;
102
                }
103
104 1
                $phpcsFile->fixer->replaceToken($j, '');
105 1
            }
106 1
        }
107
108 1
        for ($i = 0; $i <= $this->emptyLineCount; $i++) {
109 1
            $phpcsFile->fixer->addNewline($declarationEndPtr);
110 1
        }
111
112 1
        $phpcsFile->fixer->endChangeset();
113 1
    }//end process()
114
}//end class
115