PropertyDeclarationSniff::handleVisibilityToken()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 20
rs 9.2222
1
<?php declare(strict_types = 1);
2
3
namespace Codor\Sniffs\Classes;
4
5
use PHP_CodeSniffer\Sniffs\Sniff as PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer\Files\File as PHP_CodeSniffer_File;
7
8
class PropertyDeclarationSniff implements PHP_CodeSniffer_Sniff
9
{
10
11
    /**
12
     * The decalred member variables in the class.
13
     * @var array
14
     */
15
    protected $memberVars = [];
16
17
    /**
18
     * The referenced member variables in the class.
19
     * @var array
20
     */
21
    protected $referencedMemberVars = [];
22
23
    /**
24
     * Visibility Tokens.
25
     * @var array
26
     */
27
    protected $visibilityTokens = ['T_PRIVATE', 'T_PUBLIC', 'T_PROTECTED'];
28
29
    /**
30
     * Holds the value if the class is extended or not.
31
     * @var boolean
32
     */
33
    protected $isExtendedClass = false;
34
35
    /**
36
     * Returns the token types that this sniff is interested in.
37
     * @return array
38
     */
39
    public function register(): array
40
    {
41
        return [T_CLASS];
42
    }
43
44
    /**
45
     * Processes the tokens that this sniff is interested in.
46
     *
47
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
48
     * @param int                  $stackPtr  The position in the stack where
49
     *                                        the token was found.
50
     * @return void
51
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
52
     */
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        $this->memberVars = [];
56
        $this->referencedMemberVars = [];
57
        $tokens = $phpcsFile->getTokens();
58
        foreach ($tokens as $index => $token) {
59
            $this->handleToken($tokens, $index);
60
        }
61
62
        $undeclaredMemberVars = array_diff($this->referencedMemberVars, $this->memberVars);
63
64
        foreach ($undeclaredMemberVars as $undeclaredVariable) {
65
            $this->handleError($undeclaredVariable, $phpcsFile, $stackPtr);
66
        }
67
    }
68
69
    /**
70
     * Handle the incoming token.
71
     * @param  array   $tokens List of tokens.
72
     * @param  integer $index  Current token index.
73
     * @return void
74
     */
75
    protected function handleToken($tokens, $index)
76
    {
77
        $token = $tokens[$index];
78
79
        if ($token['type'] === 'T_EXTENDS') {
80
            $this->isExtendedClass = true;
81
            return;
82
        }
83
84
        if ($this->isVisibilityToken($token)) {
85
            $this->handleVisibilityToken($tokens, $index);
86
            return;
87
        }
88
89
90
        if ($token['type'] === 'T_OBJECT_OPERATOR') {
91
            $this->handleObjectOperatorToken($tokens, $index);
92
            return;
93
        }
94
    }
95
96
    /**
97
     * Determines if the provided token is a visiblity token.
98
     * @param  array $token Token data.
99
     * @return boolean
100
     */
101
    protected function isVisibilityToken($token): bool
102
    {
103
        return in_array($token['type'], $this->visibilityTokens);
104
    }
105
106
    /**
107
     * Handles the logic for a visiblity type token.
108
     * @param  array   $tokens List of tokens.
109
     * @param  integer $index  Current token index.
110
     * @return void
111
     */
112
    protected function handleVisibilityToken($tokens, $index)
113
    {
114
        $count = count($tokens);
115
116
        for ($i = $index + 1; $i < $count; $i++) {
117
            $possibleVariable = $tokens[$i];
118
119
            if ($possibleVariable['type'] === 'T_CONST'
120
                || $possibleVariable['type'] === 'T_FUNCTION'
121
                || $possibleVariable['type'] === 'T_SEMICOLON'
122
            ) {
123
                return;
124
            }
125
126
            if ($possibleVariable['type'] !== 'T_VARIABLE') {
127
                continue;
128
            }
129
130
            $this->memberVars[] = str_replace('$', '', $possibleVariable['content']);
131
            return;
132
        }
133
    }
134
135
    /**
136
     * Handles the logic for an object operator token.
137
     * @param  array   $tokens List of tokens.
138
     * @param  integer $index  Current token index.
139
     * @return void
140
     */
141
    protected function handleObjectOperatorToken($tokens, $index)
142
    {
143
        $previousToken = $tokens[$index-1];
144
        $nextToken = $tokens[$index+1];
145
        $tokenAfterNext = $tokens[$index+2];
146
147
        if ($previousToken['content'] !== '$this') {
148
            return;
149
        }
150
151
        if (($tokenAfterNext['type'] !== 'T_WHITESPACE') && ($tokenAfterNext['type'] !== 'T_EQUAL')) {
152
            return;
153
        }
154
155
        $this->referencedMemberVars[] = $nextToken['content'];
156
    }
157
158
    /**
159
     * If the class is extended from another class then a warning is produced.
160
     * Otherwise an error is produced.
161
     * @param  string               $undeclaredVariable Name of the undeclared member variable.
162
     * @param  PHP_CodeSniffer_File $phpcsFile          PHP Code Sniffer File.
163
     * @param  integer              $index              Index of where the undeclared variable was found in the file.
164
     * @return void
165
     */
166
    protected function handleError($undeclaredVariable, $phpcsFile, $index)
167
    {
168
        if ($this->isExtendedClass) {
169
            $phpcsFile->addWarning("Class contains undeclared property {$undeclaredVariable}.", $index, __CLASS__);
170
            return;
171
        }
172
        $phpcsFile->addError("Class contains undeclared property {$undeclaredVariable}.", $index, __CLASS__);
173
    }
174
}
175