ElseOnNewLineSniff   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A process() 0 19 3
1
<?php
2
/**
3
 * CodeIgniter_Sniffs_WhiteSpace_ElseOnNewLineSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Thomas Ernest <[email protected]>
10
 * @copyright 2006 Thomas Ernest
11
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
/**
16
 * CodeIgniter_Sniffs_WhiteSpace_ElseOnNewLineSniff.
17
 *
18
 * Ensures that control structures else and elseif stand on new lines.
19
 *
20
 * @category  PHP
21
 * @package   PHP_CodeSniffer
22
 * @author    Thomas Ernest <[email protected]>
23
 * @copyright 2006 Thomas Ernest
24
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
25
 * @link      http://pear.php.net/package/PHP_CodeSniffer
26
 */
27
28
namespace CodeIgniter\Sniffs\WhiteSpace;
29
30
use PHP_CodeSniffer\Sniffs\Sniff;
31
use PHP_CodeSniffer\Files\File;
32
33
class ElseOnNewLineSniff implements Sniff
34
{
35
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(
44
            T_ELSE,
45
            T_ELSEIF,
46
        );
47
    }//end register()
48
49
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param File $phpcsFile The current file being scanned.
54
     * @param int                  $stackPtr  The position of the current token
55
     *                                        in the stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(File $phpcsFile, $stackPtr)
60
    {
61
        $tokens = $phpcsFile->getTokens();
62
63
        $else_token = $tokens[$stackPtr];
64
        $previous_non_blank_token_ptr = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true);
65
66
        if (false === $previous_non_blank_token_ptr) {
67
        	// else is no preceded with any symbol, but it is not the responsibility of this sniff.
68
        	return;
69
        }
70
71
        $previous_non_blank_token = $tokens[$previous_non_blank_token_ptr];
72
        if ($previous_non_blank_token['line'] === $else_token['line']) {
73
            $error = '"' . $else_token['content'] . '" should be on a new line.';
74
            $phpcsFile->addError($error, $stackPtr);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
75
        }
76
77
    }//end process()
78
79
80
}//end class
81
82
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...