Completed
Push — feature/new-new-typecasts-snif... ( 6ffdb5 )
by Juliette
03:44 queued 01:51
created

NewTypeCastsSniff::getNonVersionArrayKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\NewTypeCastsSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
namespace PHPCompatibility\Sniffs\PHP;
11
12
use PHPCompatibility\AbstractNewFeatureSniff;
13
14
/**
15
 * \PHPCompatibility\Sniffs\PHP\NewTypeCastsSniff.
16
 *
17
 * @category PHP
18
 * @package  PHPCompatibility
19
 * @author   Juliette Reinders Folmer <[email protected]>
20
 */
21
class NewTypeCastsSniff extends AbstractNewFeatureSniff
22
{
23
24
    /**
25
     * A list of new type casts, not present in older versions.
26
     *
27
     * The array lists : version number with false (not present) or true (present).
28
     * If's sufficient to list the first version where the keyword appears.
29
     *
30
     * @var array(string => array(string => int|string|null))
31
     */
32
    protected $newTypeCasts = array(
33
        'T_UNSET_CAST' => array(
34
            '4.4'         => false,
35
            '5.0'         => true,
36
            'description' => 'The unset cast',
37
        ),
38
        'T_BINARY_CAST' => array(
39
            '5.2.0'       => false,
40
            '5.2.1'       => true,
41
            'description' => 'The binary cast',
42
        ),
43
    );
44
45
46
    /**
47
     * Returns an array of tokens this test wants to listen for.
48
     *
49
     * @return array
50
     */
51
    public function register()
52
    {
53
        $tokens = array();
54
        foreach ($this->newTypeCasts as $token => $versions) {
55
            $tokens[] = constant($token);
56
        }
57
58
        /*
59
         * Work around tokenizer issues.
60
         *
61
         * - (binary) cast is incorrectly tokenized as T_STRING_CAST by PHP and PHPCS.
62
         * - b"something" binary cast is incorrectly tokenized as T_CONSTANT_ENCAPSED_STRING by PHP and PHPCS.
63
         *
64
         * @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1574
65
         */
66
        $tokens[] = T_STRING_CAST;
67
        $tokens[] = T_CONSTANT_ENCAPSED_STRING;
68
69
        return $tokens;
70
    }//end register()
71
72
73
    /**
74
     * Processes this test, when one of its tokens is encountered.
75
     *
76
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
77
     * @param int                   $stackPtr  The position of the current token in
78
     *                                         the stack passed in $tokens.
79
     *
80
     * @return void
81
     */
82
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
83
    {
84
        $tokens    = $phpcsFile->getTokens();
85
        $tokenType = $tokens[$stackPtr]['type'];
86
87
        // Detect incorrectly tokenized binary casts.
88
        if (isset($this->newTypeCasts[$tokenType]) === false) {
89
            $tokenContent = $tokens[$stackPtr]['content'];
90
            switch($tokenType) {
91
                case 'T_STRING_CAST':
92
                    if (preg_match('`^\(\s*binary\s*\)$`i', $tokenContent) !== 1) {
93
                        return;
94
                    }
95
96
                    $tokenType = 'T_BINARY_CAST';
97
                    break;
98
                case 'T_CONSTANT_ENCAPSED_STRING':
99
                    if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {
100
                        $tokenType = 'T_BINARY_CAST';
101
                    } else {
102
                        return;
103
                    }
104
                    break;
105
106
            }
107
        }
108
109
        // If the translation did not yield one of the tokens we are looking for, bow out.
110
        if (isset($this->newTypeCasts[$tokenType]) === false) {
111
            return;
112
        }
113
114
        $itemInfo = array(
115
            'name'    => $tokenType,
116
            'content' => $tokens[$stackPtr]['content'],
117
        );
118
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
119
120
    }//end process()
121
122
123
    /**
124
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
125
     *
126
     * @param array $itemInfo Base information about the item.
127
     *
128
     * @return array Version and other information about the item.
129
     */
130
    public function getItemArray(array $itemInfo)
131
    {
132
        return $this->newTypeCasts[$itemInfo['name']];
133
    }
134
135
136
    /**
137
     * Get an array of the non-PHP-version array keys used in a sub-array.
138
     *
139
     * @return array
140
     */
141
    protected function getNonVersionArrayKeys()
142
    {
143
        return array('description');
144
    }
145
146
147
    /**
148
     * Retrieve the relevant detail (version) information for use in an error message.
149
     *
150
     * @param array $itemArray Version and other information about the item.
151
     * @param array $itemInfo  Base information about the item.
152
     *
153
     * @return array
154
     */
155
    public function getErrorInfo(array $itemArray, array $itemInfo)
156
    {
157
        $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
158
        $errorInfo['description'] = $itemArray['description'];
159
160
        return $errorInfo;
161
    }
162
163
164
    /**
165
     * Filter the error message before it's passed to PHPCS.
166
     *
167
     * @param string $error     The error message which was created.
168
     * @param array  $itemInfo  Base information about the item this error message applied to.
169
     * @param array  $errorInfo Detail information about an item this error message applied to.
170
     *
171
     * @return string
172
     */
173
    protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
174
    {
175
        return $error . '. Found: %s';
176
    }
177
178
179
    /**
180
     * Filter the error data before it's passed to PHPCS.
181
     *
182
     * @param array $data      The error data array which was created.
183
     * @param array $itemInfo  Base information about the item this error message applied to.
184
     * @param array $errorInfo Detail information about an item this error message applied to.
185
     *
186
     * @return array
187
     */
188
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
189
    {
190
        $data[0] = $errorInfo['description'];
191
        $data[]  = $itemInfo['content'];
192
        return $data;
193
    }
194
195
196
}//end class
197