Completed
Push — master ( f02e69...2d7d11 )
by Juliette
15s
created

DeprecatedTypeCastsSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 10 2
A process() 0 16 2
A getNonVersionArrayKeys() 0 4 1
A getItemArray() 0 4 1
A getErrorMsgTemplate() 0 4 1
A filterErrorData() 0 5 1
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\DeprecatedTypeCastsSniff.
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\AbstractRemovedFeatureSniff;
13
14
/**
15
 * \PHPCompatibility\Sniffs\PHP\DeprecatedTypeCastsSniff.
16
 *
17
 * @category PHP
18
 * @package  PHPCompatibility
19
 * @author   Juliette Reinders Folmer <[email protected]>
20
 */
21
class DeprecatedTypeCastsSniff extends AbstractRemovedFeatureSniff
22
{
23
    /**
24
     * A list of deprecated and removed type casts with their alternatives.
25
     *
26
     * The array lists : version number with false (deprecated) or true (removed) and an alternative function.
27
     * If no alternative exists, it is NULL, i.e, the function should just not be used.
28
     *
29
     * @var array(string => array(string => bool|string|null))
30
     */
31
    protected $deprecatedTypeCasts = array(
32
        'T_UNSET_CAST' => array(
33
            '7.2'         => false,
34
            'alternative' => 'unset()',
35
            'description' => 'unset',
36
        ),
37
    );
38
39
40
    /**
41
     * Returns an array of tokens this test wants to listen for.
42
     *
43
     * @return array
44
     */
45
    public function register()
46
    {
47
        $tokens = array();
48
        foreach ($this->deprecatedTypeCasts as $token => $versions) {
49
            $tokens[] = constant($token);
50
        }
51
52
        return $tokens;
53
54
    }//end register()
55
56
57
    /**
58
     * Processes this test, when one of its tokens is encountered.
59
     *
60
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                   $stackPtr  The position of the current token in
62
     *                                         the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens    = $phpcsFile->getTokens();
69
        $tokenType = $tokens[$stackPtr]['type'];
70
71
        if (isset($this->deprecatedTypeCasts[$tokenType]) === false) {
72
            return;
73
        }
74
75
        $itemInfo = array(
76
            'name'        => $tokenType,
77
            'description' => $this->deprecatedTypeCasts[$tokenType]['description'],
78
        );
79
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
80
81
    }//end process()
82
83
84
    /**
85
     * Get an array of the non-PHP-version array keys used in a sub-array.
86
     *
87
     * @return array
88
     */
89
    protected function getNonVersionArrayKeys()
90
    {
91
        return array('description', 'alternative');
92
    }
93
94
    /**
95
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
96
     *
97
     * @param array $itemInfo Base information about the item.
98
     *
99
     * @return array Version and other information about the item.
100
     */
101
    public function getItemArray(array $itemInfo)
102
    {
103
        return $this->deprecatedTypeCasts[$itemInfo['name']];
104
    }
105
106
107
    /**
108
     * Get the error message template for this sniff.
109
     *
110
     * @return string
111
     */
112
    protected function getErrorMsgTemplate()
113
    {
114
        return 'The %s cast is ';
115
    }
116
117
118
    /**
119
     * Filter the error data before it's passed to PHPCS.
120
     *
121
     * @param array $data      The error data array which was created.
122
     * @param array $itemInfo  Base information about the item this error message applied to.
123
     * @param array $errorInfo Detail information about an item this error message applied to.
124
     *
125
     * @return array
126
     */
127
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
128
    {
129
        $data[0] = $itemInfo['description'];
130
        return $data;
131
    }
132
133
}//end class
134