Passed
Push — master ( c34b74...73c062 )
by Björn
13:02
created

AbstractSniff::addPointerToTokens()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs;
6
7
use BestIt\CodeSniffer\CodeError;
8
use BestIt\CodeSniffer\CodeWarning;
9
use BestIt\CodeSniffer\File;
10
use BestIt\CodeSniffer\Helper\ExceptionHelper;
11
use PHP_CodeSniffer\Files\File as BaseFile;
12
use PHP_CodeSniffer\Sniffs\Sniff;
13
14
/**
15
 * Class AbstractSniff
16
 *
17
 * @author Nick Lubisch <[email protected]>
18
 * @package BestIt\Sniffs
19
 */
20
abstract class AbstractSniff implements Sniff
21
{
22
    use SuppressingTrait;
23
24
    /**
25
     * The used file.
26
     *
27
     * @var File|void
28
     */
29
    protected $file;
30
31
    /**
32
     * Position of the listened token.
33
     *
34
     * @var int|void
35
     */
36
    protected $stackPos;
37
38
    /**
39
     * The used token.
40
     *
41
     * @var array|void
42
     */
43
    protected $token;
44
45
    /**
46
     * All tokens of the class.
47
     *
48
     * @var array|void The tokens of the class.
49
     */
50
    protected $tokens;
51
52
    /**
53
     * Adds the pointer to all token data arrays.
54
     *
55
     * @return void
56
     */
57
    protected function addPointerToTokens(): void
58
    {
59
        foreach ($this->tokens as $tokenPtr => &$token) {
0 ignored issues
show
Bug introduced by
The expression $this->tokens of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
60
            $token['pointer'] = $tokenPtr;
61
        }
62
    }
63
64
    /**
65
     * Returns true if the requirements for this sniff are met.
66
     *
67
     * @return bool Are the requirements met and the sniff should proceed?
68
     */
69
    protected function areRequirementsMet(): bool
70
    {
71
        return true;
72
    }
73
74
    /**
75
     * Default method for fixing exceptions.
76
     *
77
     * @param CodeWarning $exception
78
     *
79
     * @return void
80
     */
81
    protected function fixDefaultProblem(CodeWarning $exception): void
82
    {
83
        // Satisfy PHP MD
84
        unset($exception);
85
    }
86
87
    /**
88
     * Returns an exception handler for the sniffed file.
89
     *
90
     * @return ExceptionHelper Returns the exception helper.
91
     */
92
    protected function getExceptionHandler(): ExceptionHelper
93
    {
94
        return new ExceptionHelper($this->getFile());
95
    }
96
97
    /**
98
     * Type-safe getter for the file.
99
     *
100
     * @return File
101
     */
102
    protected function getFile(): File
103
    {
104
        return $this->file;
105
    }
106
107
    /**
108
     * Type-safe getter for the stack position.
109
     *
110
     * @return int
111
     */
112
    protected function getStackPos(): int
113
    {
114
        return $this->stackPos;
115
    }
116
117
    /**
118
     * Called when one of the token types that this sniff is listening for is found.
119
     *
120
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
121
     *
122
     * @param BaseFile $phpcsFile The PHP_CodeSniffer file where the token was found.
123
     * @param int $stackPos The position in the PHP_CodeSniffer file's token stack where the token was found.
124
     *
125
     * @return void
126
     */
127
    public function process(BaseFile $phpcsFile, $stackPos): void
128
    {
129
        $this->file = new File($phpcsFile);
130
        $this->stackPos = $stackPos;
131
        $this->tokens = $this->getFile()->getTokens();
132
        $this->token = $this->tokens[$stackPos];
133
134
        $this->setUp();
135
136
        if ($this->areRequirementsMet()) {
137
            try {
138
                $this->processToken();
139
            } catch (CodeWarning | CodeError $exception) {
140
                $withFix = $this->getExceptionHandler()->handleException($exception);
141
142
                if ($withFix) {
143
                    $this->fixDefaultProblem($exception);
144
                }
145
            }
146
        }
147
148
        $this->tearDown();
149
    }
150
151
    /**
152
     * Processes the token.
153
     *
154
     * @return void
155
     */
156
    abstract protected function processToken(): void;
157
158
    /**
159
     * Do you want to setup things before processing the token?
160
     *
161
     * @return void
162
     */
163
    protected function setUp(): void
164
    {
165
    }
166
167
    /**
168
     * Is there something to destroy after processing the token?
169
     *
170
     * @return void
171
     */
172
    protected function tearDown(): void
173
    {
174
    }
175
}
176