Passed
Pull Request — master (#19)
by
unknown
02:19
created

File::addFixableError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 5
crap 1
1
<?php
2
3
namespace BestIt\CodeSniffer;
4
5
use PHP_CodeSniffer_File;
6
7
/**
8
 * Class File
9
 *
10
 * Wrapper Class for PHP_CodeSniffer_File to provide a consistent way to replace int|bool returns
11
 * with int returns (false => -1)
12
 * Additionally there could be some architecture changes in the future, like Token-Objects and so on.
13
 *
14
 * @package BestIt\CodeSniffer
15
 * @author Nick Lubisch <[email protected]>
16
 */
17
class File
18
{
19
    /**
20
     * The CodeSniffer file
21
     *
22
     * @var PHP_CodeSniffer_File
23
     */
24
    private $baseFile;
25
26
    /**
27
     * Wrapped PHP_CodeSniffer_Fixer
28
     *
29
     * @var Fixer
30
     */
31
    private $fixer;
32
33
    /**
34
     * Contains the advanced token stack
35
     *
36
     * @var array
37
     */
38
    private $tokens;
39
40
    /**
41
     * File constructor.
42
     *
43
     * @param PHP_CodeSniffer_File $baseFile CodeSniffer file
44
     */
45 109
    public function __construct(PHP_CodeSniffer_File $baseFile)
46
    {
47 109
        $this->baseFile = $baseFile;
48 109
        $this->fixer = new Fixer($this, $this->baseFile->fixer);
49 109
        $this->tokens = $this->getAdvancedTokens();
50 109
    }
51
52
    /**
53
     * Adds the pointer to all token data arrays.
54
     *
55
     * @return array Advanced token stack.
56
     */
57 109
    private function getAdvancedTokens()
58
    {
59 109
        $tokens = [];
60
61 109
        foreach ($this->baseFile->getTokens() as $tokenPtr => $token) {
62 109
            $token['pointer'] = $tokenPtr;
63 109
            $tokens[$tokenPtr] = $token;
64
        }
65
66 109
        return $tokens;
67
    }
68
69
    /**
70
     * Records a fixable error against a specific token in the file.
71
     *
72
     * @param string $error The error message.
73
     * @param int $stackPtr The stack position where the error occurred.
74
     * @param string $code A violation code unique to the sniff message.
75
     * @param array $data Replacements for the error message.
76
     * @param int $severity The severity level for this error.
77
     *                      A value of 0 will be converted into the default severity level.
78
     *
79
     * @return boolean Returns true if the error was recorded and should be fixed.
80
     */
81 68
    public function addFixableError($error, $stackPtr, $code = '', array $data = [], $severity = 0)
82
    {
83 68
         return $this->baseFile->addFixableError($error, $stackPtr, $code, $data, $severity);
84
    }
85
86
    /**
87
     * Returns the token stack for this file.
88
     *
89
     * @return array Return array of token data
90
     */
91 109
    public function getTokens()
92
    {
93 109
        return $this->tokens;
94
    }
95
96
    /**
97
     * Returns the position of the previous specified token(s).
98
     *
99
     * If a value is specified, the previous token of the specified type(s)
100
     * containing the specified value will be returned.
101
     *
102
     * Returns false if no token can be found.
103
     *
104
     * @param array $types The type(s) of tokens to search for.
105
     * @param int $start The position to start searching from in the token stack.
106
     * @param int $end The end position to fail if no token is found.
107
     *        if not specified or null, end will default to the start of the token stack.
108
     * @param bool $exclude If true, find the previous token that are NOT of the types specified in $types.
109
     * @param string $value The value that the token(s) must be equal to.
110
     *        If value is omitted, tokens with any value will be returned.
111
     * @param bool $local If true, tokens outside the current statement will not be checked.
112
     *        IE. checking will stop at the previous semi-colon found.
113
     *
114
     * @return int Pointer to the found token
115
     */
116 109
    public function findPrevious(array $types, $start, $end = null, $exclude = false, $value = null, $local = false)
117
    {
118 109
        return $this->baseFile->findPrevious($types, $start, $end, $exclude, $value, $local);
119
    }
120
121
    /**
122
     * Records an error against a specific token in the file.
123
     *
124
     * @param string $error The error message.
125
     * @param int $stackPtr The stack position where the error occurred.
126
     * @param string $code A violation code unique to the sniff message.
127
     * @param array $data Replacements for the error message.
128
     * @param int $severity The severity level for this error. A value of 0
129
     *                      will be converted into the default severity level.
130
     * @param bool $fixable Can the error be fixed by the sniff?
131
     *
132
     * @return bool Returns true if setting the error was done or false
133
     */
134 54
    public function addError($error, $stackPtr, $code = '', array $data = [], $severity = 0, $fixable = false)
135
    {
136 54
        return $this->baseFile->addError($error, $stackPtr, $code, $data, $severity, $fixable);
137
    }
138
139
    /**
140
     * Records an error against a specific line in the file.
141
     *
142
     * @param string $error The error message.
143
     * @param int $line The line on which the error occurred.
144
     * @param string $code A violation code unique to the sniff message.
145
     * @param array $data Replacements for the error message.
146
     * @param int $severity The severity level for this error. A value of 0
147
     *                      will be converted into the default severity level.
148
     *
149
     * @return bool Returns true of the error got recorded
150
     */
151 12
    public function addErrorOnLine($error, $line, $code = '', array $data = [], $severity = 0)
152
    {
153 12
        return $this->baseFile->addErrorOnLine($error, $line, $code, $data, $severity);
154
    }
155
156
    /**
157
     * Returns the position of the next specified token(s).
158
     *
159
     * If a value is specified, the next token of the specified type(s)
160
     * containing the specified value will be returned.
161
     *
162
     * Returns false if no token can be found.
163
     *
164
     * @param array $types The type(s) of tokens to search for.
165
     * @param int $start The position to start searching from in the
166
     *                   token stack.
167
     * @param int $end The end position to fail if no token is found. if not specified or null, end will default to
168
     *                 the end of the token stack.
169
     * @param bool $exclude If true, find the next token that is NOT of a type specified in $types.
170
     * @param string $value The value that the token(s) must be equal to.
171
     *                      If value is omitted, tokens with any value will be returned.
172
     * @param bool $local If true, tokens outside the current statement will not be checked. i.e., checking will stop
173
     *                    at the next semi-colon found.
174
     *
175
     * @return int Returns the pointer of the token or -1
176
     */
177 101
    public function findNext($types, $start, $end = null, $exclude = false, $value = null, $local = false)
178
    {
179 101
        $result = $this->baseFile->findNext($types, $start, $end, $exclude, $value, $local);
180
181 101
        return $this->preparePointer($result);
0 ignored issues
show
Security Bug introduced by
It seems like $result defined by $this->baseFile->findNex...xclude, $value, $local) on line 179 can also be of type false; however, BestIt\CodeSniffer\File::preparePointer() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
182
    }
183
184
    /**
185
     * Prepares given pointer result.
186
     *
187
     * @param int $pointer Pointer of a token
188
     *
189
     * @return int Pointer or -1 when not found
190
     */
191 101
    public function preparePointer($pointer)
192
    {
193 101
        return $pointer !== false ? $pointer : -1;
194
    }
195
196
    /**
197
     * Returns the Wrapped PHP_CodeSniffer_Fixer
198
     *
199
     * @return Fixer Returns the wrapped PHP_CodeSniffer_Fixer
200
     */
201 33
    public function getFixer()
202
    {
203 33
        return $this->fixer;
204
    }
205
206
    /**
207
     * Returns the eol char of the file
208
     *
209
     * @return string Returns the EndOfLine-Character of the processd file
210
     */
211 87
    public function getEolChar()
212
    {
213 87
        return $this->baseFile->eolChar;
214
    }
215
}
216