|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility_Sniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Wim Godden <[email protected]> |
|
10
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* PHPCompatibility_Sniff. |
|
15
|
|
|
* |
|
16
|
|
|
* @category PHP |
|
17
|
|
|
* @package PHPCompatibility |
|
18
|
|
|
* @author Wim Godden <[email protected]> |
|
19
|
|
|
* @version 1.1.0 |
|
20
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/* The testVersion configuration variable may be in any of the following formats: |
|
26
|
|
|
* 1) Omitted/empty, in which case no version is specified. This effectively |
|
27
|
|
|
* disables all the checks provided by this standard. |
|
28
|
|
|
* 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
|
29
|
|
|
* the code will run on that version of PHP (no deprecated features or newer |
|
30
|
|
|
* features being used). |
|
31
|
|
|
* 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
|
32
|
|
|
* on all PHP versions in that range, and that it doesn't use any features that |
|
33
|
|
|
* were deprecated by the final version in the list, or which were not available |
|
34
|
|
|
* for the first version in the list. |
|
35
|
|
|
* PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
|
36
|
|
|
* would be treated as invalid, and ignored. |
|
37
|
|
|
* This standard doesn't support checking against PHP4, so the minimum version that |
|
38
|
|
|
* is recognised is "5.0". |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
private function getTestVersion() |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* var $arrTestVersions will hold an array containing min/max version of PHP |
|
45
|
|
|
* that we are checking against (see above). If only a single version |
|
46
|
|
|
* number is specified, then this is used as both the min and max. |
|
47
|
|
|
*/ |
|
48
|
|
|
static $arrTestVersions = array(); |
|
49
|
|
|
|
|
50
|
|
|
$testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
|
51
|
|
|
|
|
52
|
|
|
if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) { |
|
53
|
|
|
|
|
54
|
|
|
$arrTestVersions[$testVersion] = array(null, null); |
|
55
|
|
|
if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
|
56
|
|
|
$arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
|
57
|
|
|
} |
|
58
|
|
|
elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, |
|
59
|
|
|
$matches)) |
|
60
|
|
|
{ |
|
61
|
|
|
if (version_compare($matches[1], $matches[2], '>')) { |
|
62
|
|
|
trigger_error("Invalid range in testVersion setting: '" |
|
63
|
|
|
. $testVersion . "'", E_USER_WARNING); |
|
64
|
|
|
} |
|
65
|
|
|
else { |
|
66
|
|
|
$arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
elseif (!$testVersion == '') { |
|
70
|
|
|
trigger_error("Invalid testVersion setting: '" . $testVersion |
|
71
|
|
|
. "'", E_USER_WARNING); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (isset($arrTestVersions[$testVersion])) { |
|
76
|
|
|
return $arrTestVersions[$testVersion]; |
|
77
|
|
|
} |
|
78
|
|
|
else { |
|
79
|
|
|
return array(null, null); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
public function supportsAbove($phpVersion) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$testVersion = $this->getTestVersion(); |
|
86
|
|
|
$testVersion = $testVersion[1]; |
|
87
|
|
|
|
|
88
|
|
|
if (is_null($testVersion) |
|
89
|
|
|
|| version_compare($testVersion, $phpVersion) >= 0 |
|
90
|
|
|
) { |
|
91
|
|
|
return true; |
|
92
|
|
|
} else { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
}//end supportsAbove() |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
public function supportsBelow($phpVersion) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$testVersion = $this->getTestVersion(); |
|
100
|
|
|
$testVersion = $testVersion[0]; |
|
101
|
|
|
|
|
102
|
|
|
if (!is_null($testVersion) |
|
103
|
|
|
&& version_compare($testVersion, $phpVersion) <= 0 |
|
104
|
|
|
) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} else { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
}//end supportsBelow() |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the name(s) of the interface(s) that the specified class implements. |
|
113
|
|
|
* |
|
114
|
|
|
* Returns FALSE on error or if there are no implemented interface names. |
|
115
|
|
|
* |
|
116
|
|
|
* {@internal Duplicate of same method as introduced in PHPCS 2.7. |
|
117
|
|
|
* Once the minimum supported PHPCS version for this sniff library goes beyond |
|
118
|
|
|
* that, this method can be removed and call to it replaced with |
|
119
|
|
|
* `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
|
120
|
|
|
* |
|
121
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
122
|
|
|
* @param int $stackPtr The position of the class token. |
|
123
|
|
|
* |
|
124
|
|
|
* @return array|false |
|
125
|
|
|
*/ |
|
126
|
|
|
public function findImplementedInterfaceNames($phpcsFile, $stackPtr) |
|
127
|
|
|
{ |
|
128
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
129
|
|
|
|
|
130
|
|
|
// Check for the existence of the token. |
|
131
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if ($tokens[$stackPtr]['code'] !== T_CLASS) { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$classOpenerIndex = $tokens[$stackPtr]['scope_opener']; |
|
144
|
|
|
$implementsIndex = $phpcsFile->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
|
145
|
|
|
if ($implementsIndex === false) { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$find = array( |
|
150
|
|
|
T_NS_SEPARATOR, |
|
151
|
|
|
T_STRING, |
|
152
|
|
|
T_WHITESPACE, |
|
153
|
|
|
T_COMMA, |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$end = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
|
157
|
|
|
$name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
|
158
|
|
|
$name = trim($name); |
|
159
|
|
|
|
|
160
|
|
|
if ($name === '') { |
|
161
|
|
|
return false; |
|
162
|
|
|
} else { |
|
163
|
|
|
$names = explode(',', $name); |
|
164
|
|
|
$names = array_map('trim', $names); |
|
165
|
|
|
return $names; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
}//end findImplementedInterfaceNames() |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
}//end class |
|
172
|
|
|
|
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.