1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_AbstractRemovedFeatureSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_AbstractRemovedFeatureSniff. |
12
|
|
|
* |
13
|
|
|
* @category PHP |
14
|
|
|
* @package PHPCompatibility |
15
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class PHPCompatibility_AbstractRemovedFeatureSniff extends PHPCompatibility_AbstractComplexVersionSniff |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Determine whether an error/warning should be thrown for an item based on collected information. |
23
|
|
|
* |
24
|
|
|
* @param array $errorInfo Detail information about an item. |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
protected function shouldThrowError(array $errorInfo) |
29
|
|
|
{ |
30
|
|
|
return ($errorInfo['deprecated'] !== '' || $errorInfo['removed'] !== ''); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get an array of the non-PHP-version array keys used in a sub-array. |
36
|
|
|
* |
37
|
|
|
* By default, removed feature version arrays, contain an additional 'alternative' array key. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
protected function getNonVersionArrayKeys() |
42
|
|
|
{ |
43
|
|
|
return array('alternative'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve the relevant detail (version) information for use in an error message. |
49
|
|
|
* |
50
|
|
|
* @param array $itemArray Version and other information about the item. |
51
|
|
|
* @param array $itemInfo Base information about the item. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getErrorInfo(array $itemArray, array $itemInfo) |
56
|
|
|
{ |
57
|
|
|
$errorInfo = array( |
58
|
|
|
'deprecated' => '', |
59
|
|
|
'removed' => '', |
60
|
|
|
'alternative' => '', |
61
|
|
|
'error' => false, |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$versionArray = $this->getVersionArray($itemArray); |
65
|
|
|
|
66
|
|
|
if (empty($versionArray) === false) { |
67
|
|
|
foreach ($versionArray as $version => $removed) { |
68
|
|
|
if ($this->supportsAbove($version) === true) { |
69
|
|
|
if ($removed === true && $errorInfo['removed'] === '') { |
70
|
|
|
$errorInfo['removed'] = $version; |
71
|
|
|
$errorInfo['error'] = true; |
72
|
|
|
} elseif ($errorInfo['deprecated'] === '') { |
73
|
|
|
$errorInfo['deprecated'] = $version; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (isset($itemArray['alternative']) === true) { |
80
|
|
|
$errorInfo['alternative'] = $itemArray['alternative']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $errorInfo; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the error message template for suggesting an alternative for a specific sniff. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getAlternativeOptionTemplate() |
93
|
|
|
{ |
94
|
|
|
return '; Use %s instead'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generates the error or warning for this item. |
100
|
|
|
* |
101
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
102
|
|
|
* @param int $stackPtr The position of the relevant token in |
103
|
|
|
* the stack. |
104
|
|
|
* @param array $itemInfo Base information about the item. |
105
|
|
|
* @param array $errorInfo Array with detail (version) information |
106
|
|
|
* relevant to the item. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
111
|
|
|
{ |
112
|
|
|
$itemName = $this->getItemName($itemInfo, $errorInfo); |
113
|
|
|
$error = $this->getErrorMsgTemplate(); |
114
|
|
|
|
115
|
|
|
$errorCode = $this->stringToErrorCode($itemName); |
116
|
|
|
$data = array($itemName); |
117
|
|
|
|
118
|
|
|
if ($errorInfo['deprecated'] !== '') { |
119
|
|
|
$error .= 'deprecated since PHP %s and '; |
120
|
|
|
$errorCode .= 'Deprecated'; |
121
|
|
|
$data[] = $errorInfo['deprecated']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($errorInfo['removed'] !== '') { |
125
|
|
|
$error .= 'removed since PHP %s and '; |
126
|
|
|
$errorCode .= 'Removed'; |
127
|
|
|
$data[] = $errorInfo['removed']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Remove the last 'and' from the message. |
131
|
|
|
$error = substr($error, 0, (strlen($error) - 5)); |
132
|
|
|
|
133
|
|
|
if ($errorInfo['alternative'] !== '') { |
134
|
|
|
$error .= $this->getAlternativeOptionTemplate(); |
135
|
|
|
$data[] = $errorInfo['alternative']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$error = $this->filterErrorMsg($error, $itemInfo, $errorInfo); |
139
|
|
|
$data = $this->filterErrorData($data, $itemInfo, $errorInfo); |
140
|
|
|
|
141
|
|
|
$this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data); |
142
|
|
|
|
143
|
|
|
}//end addError() |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
}//end class |
147
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.