NestedIfSniff   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 24
c 1
b 0
f 0
dl 0
loc 93
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A errorAlreadyAdded() 0 7 2
A register() 0 3 1
A isIfStatement() 0 7 2
A process() 0 11 2
A checkForNestedIf() 0 12 3
1
<?php declare(strict_types = 1);
2
3
namespace Codor\Sniffs\ControlStructures;
4
5
use PHP_CodeSniffer\Files\File as PHP_CodeSniffer_File;
6
use PHP_CodeSniffer\Sniffs\Sniff;
7
8
class NestedIfSniff implements Sniff
9
{
10
11
    /**
12
     * Returns the token types that this sniff is interested in.
13
     * @return array
14
     */
15
    public function register(): array
16
    {
17
        return [T_IF];
18
    }
19
20
    /**
21
     * The PHP Code Sniffer file.
22
     * @var PHP_CodeSniffer_File
23
     */
24
    protected $phpcsFile;
25
26
    /**
27
     * The list of errors encountered;
28
     * @var array
29
     */
30
    protected $errorStack;
31
32
    /**
33
     * Processes the tokens that this sniff is interested in.
34
     *
35
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
36
     * @param integer              $stackPtr  The position in the stack where
37
     *                                    the token was found.
38
     * @return void
39
     */
40
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
41
    {
42
        $this->phpcsFile = $phpcsFile;
43
        $tokens          = $phpcsFile->getTokens();
44
        $token           = $tokens[$stackPtr];
45
        $start           = $token['scope_opener'];
46
        $end             = $token['scope_closer'];
47
48
        $this->errorStack = [];
49
        for ($index=$start; $index <= $end; $index++) {
50
            $this->checkForNestedIf($tokens[$index], $stackPtr);
51
        }
52
    }
53
54
    /**
55
     * Check to see if the $token is a nested if statement.
56
     * @param  array   $token    Current Token.
57
     * @param  integer $stackPtr The position in the stack where the token was found.
58
     * @return void
59
     */
60
    protected function checkForNestedIf(array $token, int $stackPtr)
61
    {
62
        if (! $this->isIfStatement($token)) {
63
            return;
64
        }
65
66
        if ($this->errorAlreadyAdded($stackPtr)) {
67
            return;
68
        }
69
70
        $this->phpcsFile->addError('Nested if statement found.', $stackPtr, __CLASS__);
71
        $this->errorStack[] = $stackPtr;
72
    }
73
74
    /**
75
     * Checks to see if this position in the stack
76
     * has already had an error reported.
77
     * @param  integer $stackPtr The position in the stack where the token was found.
78
     * @return boolean
79
     */
80
    protected function errorAlreadyAdded(int $stackPtr): bool
81
    {
82
        if (in_array($stackPtr, $this->errorStack)) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * Checks if the proved token is an if statement.
91
     * @param  array $token Token data.
92
     * @return boolean
93
     */
94
    protected function isIfStatement(array $token): bool
95
    {
96
        if ($token['type'] === 'T_IF') {
97
            return true;
98
        }
99
100
        return false;
101
    }
102
}
103