Completed
Push — master ( 00546d...ca4b26 )
by Bill
9s
created

IndentationLevelSniff::adjustMaxIndentationFound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Codor\Sniffs\Files;
4
5
use PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer_File;
7
8
class IndentationLevelSniff implements PHP_CodeSniffer_Sniff
9
{
10
    /**
11
     * Indentations cannot be >= to this number.
12
     * @var integer
13
     */
14
    public $indentationLimit = 2;
15
16
    /**
17
     * The highest indentation level found.
18
     * @var integer
19
     */
20
    protected $maxIndentationFound;
21
22
    /**
23
     * Returns the token types that this sniff is interested in.
24
     * @return array
25
     */
26
    public function register()
27
    {
28
        return [T_FUNCTION, T_CLOSURE, T_SWITCH];
29
    }
30
31
    /**
32
     * Processes the tokens that this sniff is interested in.
33
     *
34
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
35
     * @param integer              $stackPtr  The position in the stack where
36
     *                                    the token was found.
37
     * @return void
38
     */
39
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
40
    {
41
        $tokens = $phpcsFile->getTokens();
42
        $token = $tokens[$stackPtr];
43
        $this->maxIndentationFound = 0;
44
45
        // Ignore functions with no body
46
        if (isset($token['scope_opener']) === false) {
47
            return;
48
        }
49
50
        $this->inspectScope($token, $tokens);
51
52
        if ($this->maxIndentationFound <= $this->indentationLimit) {
53
            return;
54
        }
55
56
        $phpcsFile->addError($this->getErrorMessage(), $stackPtr);
57
    }
58
59
    /**
60
     * Inspect the tokens in the scope of the provided $token.
61
     * @codingStandardsIgnoreStart
62
     * @param  array $token  Token Data.
63
     * @param  array $tokens Tokens.
64
     * @return void
65
     */
66
    protected function inspectScope(array $token, array $tokens)
67
    {
68
        $start = $token['scope_opener'];
69
        $end = $token['scope_closer'];
70
        $this->relativeScopeLevel = $tokens[$start]['level'];
0 ignored issues
show
Bug introduced by
The property relativeScopeLevel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
        for ($index=$start; $index <= $end; $index++) {
72
            $nestedToken = $tokens[$index];
73
            if ($nestedToken['type'] === "T_SWITCH") {
74
                return;
75
            }
76
            $this->adjustMaxIndentationFound($nestedToken);
77
        }
78
    }
79
    // @codingStandardsIgnoreEnd
80
81
    /**
82
     * Adjust the maximum indentation level found value.
83
     * @param  array $nestedToken Token data.
84
     * @return void
85
     */
86
    protected function adjustMaxIndentationFound(array $nestedToken)
87
    {
88
        $tokenNestedLevel = $nestedToken['level'];
89
        $nestedLevel = $tokenNestedLevel - $this->relativeScopeLevel;
90
        $nestedLevel > $this->maxIndentationFound ? $this->maxIndentationFound = $nestedLevel : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nestedLevel can also be of type double. However, the property $maxIndentationFound is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
91
    }
92
93
    /**
94
     * Produce the error message.
95
     * @return string
96
     */
97
    protected function getErrorMessage()
98
    {
99
        // Hack to fix the output numbers for the
100
        // indentation levels found and the
101
        // indentation limit.
102
        $indentationFound = $this->maxIndentationFound - 1;
103
        $indentationLimit = $this->indentationLimit - 1;
104
        return "{$indentationFound} indenation levels found. " .
105
        "Maximum of {$indentationLimit} indenation levels allowed.";
106
    }
107
}
108