AbstractSniff   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 156
c 0
b 0
f 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addPointerToTokens() 0 6 2
A areRequirementsMet() 0 4 1
A fixDefaultProblem() 0 5 1
A getExceptionHandler() 0 4 1
A getFile() 0 4 1
A getStackPos() 0 4 1
A process() 0 23 4
processToken() 0 1 ?
A setUp() 0 3 1
A tearDown() 0 3 1
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\Helper\ExceptionHelper;
10
use PHP_CodeSniffer\Files\File;
11
use PHP_CodeSniffer\Sniffs\Sniff;
12
13
/**
14
 * Class AbstractSniff
15
 *
16
 * @author Nick Lubisch <[email protected]>
17
 * @package BestIt\Sniffs
18
 */
19
abstract class AbstractSniff implements Sniff
20
{
21
    use SuppressingTrait;
22
23
    /**
24
     * The used file.
25
     *
26
     * @var File|void
27
     */
28
    protected $file;
29
30
    /**
31
     * Position of the listened token.
32
     *
33
     * @var int|void
34
     */
35
    protected $stackPos;
36
37
    /**
38
     * The used token.
39
     *
40
     * @var array|void
41
     */
42
    protected $token;
43
44
    /**
45
     * All tokens of the class.
46
     *
47
     * @var array|void The tokens of the class.
48
     */
49
    protected $tokens;
50
51
    /**
52
     * Adds the pointer to all token data arrays.
53
     *
54
     * @return void
55
     */
56
    protected function addPointerToTokens(): void
57
    {
58
        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...
59
            $token['pointer'] = $tokenPtr;
60
        }
61
    }
62
63
    /**
64
     * Returns true if the requirements for this sniff are met.
65
     *
66
     * @return bool Are the requirements met and the sniff should proceed?
67
     */
68
    protected function areRequirementsMet(): bool
69
    {
70
        return true;
71
    }
72
73
    /**
74
     * Default method for fixing exceptions.
75
     *
76
     * @param CodeWarning $exception
77
     *
78
     * @return void
79
     */
80
    protected function fixDefaultProblem(CodeWarning $exception): void
81
    {
82
        // Satisfy PHP MD
83
        unset($exception);
84
    }
85
86
    /**
87
     * Returns an exception handler for the sniffed file.
88
     *
89
     * @return ExceptionHelper Returns the exception helper.
90
     */
91
    protected function getExceptionHandler(): ExceptionHelper
92
    {
93
        return new ExceptionHelper($this->getFile());
94
    }
95
96
    /**
97
     * Type-safe getter for the file.
98
     *
99
     * @return File
100
     */
101
    protected function getFile(): File
102
    {
103
        return $this->file;
104
    }
105
106
    /**
107
     * Type-safe getter for the stack position.
108
     *
109
     * @return int
110
     */
111
    protected function getStackPos(): int
112
    {
113
        return $this->stackPos;
114
    }
115
116
    /**
117
     * Called when one of the token types that this sniff is listening for is found.
118
     *
119
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
120
     *
121
     * @param File $file The PHP_CodeSniffer file where the token was found.
122
     * @param int $stackPos The position in the PHP_CodeSniffer file's token stack where the token was found.
123
     *
124
     * @return void
125
     */
126
    public function process(File $file, $stackPos): void
127
    {
128
        $this->file = $file;
129
        $this->stackPos = $stackPos;
130
        $this->tokens = $this->getFile()->getTokens();
131
        $this->token = $this->tokens[$stackPos];
132
133
        $this->setUp();
134
135
        if ($this->areRequirementsMet()) {
136
            try {
137
                $this->processToken();
138
            } catch (CodeWarning | CodeError $exception) {
139
                $withFix = $this->getExceptionHandler()->handleException($exception);
140
141
                if ($withFix) {
142
                    $this->fixDefaultProblem($exception);
143
                }
144
            }
145
        }
146
147
        $this->tearDown();
148
    }
149
150
    /**
151
     * Processes the token.
152
     *
153
     * @return void
154
     */
155
    abstract protected function processToken(): void;
156
157
    /**
158
     * Do you want to setup things before processing the token?
159
     *
160
     * @return void
161
     */
162
    protected function setUp(): void
163
    {
164
    }
165
166
    /**
167
     * Is there something to destroy after processing the token?
168
     *
169
     * @return void
170
     */
171
    protected function tearDown(): void
172
    {
173
    }
174
}
175