RemovedPredefinedGlobalVariablesSniff.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2019 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Sniffs\Variables;
  11. use PHPCompatibility\AbstractRemovedFeatureSniff;
  12. use PHPCompatibility\PHPCSHelper;
  13. use PHP_CodeSniffer_File as File;
  14. use PHP_CodeSniffer_Tokens as Tokens;
  15. /**
  16. * Detect the use of removed global variables. Suggests alternatives if available.
  17. *
  18. * PHP version 5.3+
  19. *
  20. * @link https://wiki.php.net/rfc/deprecations_php_7_2#php_errormsg
  21. *
  22. * @since 5.5 Introduced `LongArrays` sniff.
  23. * @since 7.0 Introduced `RemovedGlobalVariables` sniff.
  24. * @since 7.0.7 The `LongArrays` sniff now throws a warning for deprecated and an error for removed.
  25. * Previously the `LongArrays` sniff would always throw a warning.
  26. * @since 7.1.0 The `RemovedGlobalVariables` sniff now extends the `AbstractNewFeatureSniff`
  27. * instead of the base `Sniff` class.
  28. * @since 7.1.3 Merged the `LongArrays` sniff into the `RemovedGlobalVariables` sniff.
  29. * @since 9.0.0 Renamed from `RemovedGlobalVariablesSniff` to `RemovedPredefinedGlobalVariablesSniff`.
  30. */
  31. class RemovedPredefinedGlobalVariablesSniff extends AbstractRemovedFeatureSniff
  32. {
  33. /**
  34. * A list of removed global variables with their alternative, if any.
  35. *
  36. * The array lists : version number with false (deprecated) and true (removed).
  37. * If's sufficient to list the first version where the variable was deprecated/removed.
  38. *
  39. * @since 5.5
  40. * @since 7.0
  41. *
  42. * @var array(string => array(string => bool|string))
  43. */
  44. protected $removedGlobalVariables = array(
  45. 'HTTP_POST_VARS' => array(
  46. '5.3' => false,
  47. '5.4' => true,
  48. 'alternative' => '$_POST',
  49. ),
  50. 'HTTP_GET_VARS' => array(
  51. '5.3' => false,
  52. '5.4' => true,
  53. 'alternative' => '$_GET',
  54. ),
  55. 'HTTP_ENV_VARS' => array(
  56. '5.3' => false,
  57. '5.4' => true,
  58. 'alternative' => '$_ENV',
  59. ),
  60. 'HTTP_SERVER_VARS' => array(
  61. '5.3' => false,
  62. '5.4' => true,
  63. 'alternative' => '$_SERVER',
  64. ),
  65. 'HTTP_COOKIE_VARS' => array(
  66. '5.3' => false,
  67. '5.4' => true,
  68. 'alternative' => '$_COOKIE',
  69. ),
  70. 'HTTP_SESSION_VARS' => array(
  71. '5.3' => false,
  72. '5.4' => true,
  73. 'alternative' => '$_SESSION',
  74. ),
  75. 'HTTP_POST_FILES' => array(
  76. '5.3' => false,
  77. '5.4' => true,
  78. 'alternative' => '$_FILES',
  79. ),
  80. 'HTTP_RAW_POST_DATA' => array(
  81. '5.6' => false,
  82. '7.0' => true,
  83. 'alternative' => 'php://input',
  84. ),
  85. 'php_errormsg' => array(
  86. '7.2' => false,
  87. 'alternative' => 'error_get_last()',
  88. ),
  89. );
  90. /**
  91. * Returns an array of tokens this test wants to listen for.
  92. *
  93. * @since 5.5
  94. * @since 7.0
  95. *
  96. * @return array
  97. */
  98. public function register()
  99. {
  100. return array(\T_VARIABLE);
  101. }
  102. /**
  103. * Processes this test, when one of its tokens is encountered.
  104. *
  105. * @since 5.5
  106. * @since 7.0
  107. *
  108. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  109. * @param int $stackPtr The position of the current token in the
  110. * stack passed in $tokens.
  111. *
  112. * @return void
  113. */
  114. public function process(File $phpcsFile, $stackPtr)
  115. {
  116. if ($this->supportsAbove('5.3') === false) {
  117. return;
  118. }
  119. $tokens = $phpcsFile->getTokens();
  120. $varName = substr($tokens[$stackPtr]['content'], 1);
  121. if (isset($this->removedGlobalVariables[$varName]) === false) {
  122. return;
  123. }
  124. if ($this->isClassProperty($phpcsFile, $stackPtr) === true) {
  125. // Ok, so this was a class property declaration, not our concern.
  126. return;
  127. }
  128. // Check for static usage of class properties shadowing the removed global variables.
  129. if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) {
  130. $prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
  131. if ($prevToken !== false && $tokens[$prevToken]['code'] === \T_DOUBLE_COLON) {
  132. return;
  133. }
  134. }
  135. // Do some additional checks for the $php_errormsg variable.
  136. if ($varName === 'php_errormsg'
  137. && $this->isTargetPHPErrormsgVar($phpcsFile, $stackPtr, $tokens) === false
  138. ) {
  139. return;
  140. }
  141. // Still here, so throw an error/warning.
  142. $itemInfo = array(
  143. 'name' => $varName,
  144. );
  145. $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
  146. }
  147. /**
  148. * Get the relevant sub-array for a specific item from a multi-dimensional array.
  149. *
  150. * @since 7.1.0
  151. *
  152. * @param array $itemInfo Base information about the item.
  153. *
  154. * @return array Version and other information about the item.
  155. */
  156. public function getItemArray(array $itemInfo)
  157. {
  158. return $this->removedGlobalVariables[$itemInfo['name']];
  159. }
  160. /**
  161. * Get the error message template for this sniff.
  162. *
  163. * @since 7.1.0
  164. *
  165. * @return string
  166. */
  167. protected function getErrorMsgTemplate()
  168. {
  169. return "Global variable '\$%s' is ";
  170. }
  171. /**
  172. * Filter the error message before it's passed to PHPCS.
  173. *
  174. * @since 8.1.0
  175. *
  176. * @param string $error The error message which was created.
  177. * @param array $itemInfo Base information about the item this error message applies to.
  178. * @param array $errorInfo Detail information about an item this error message applies to.
  179. *
  180. * @return string
  181. */
  182. protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
  183. {
  184. if ($itemInfo['name'] === 'php_errormsg') {
  185. $error = str_replace('Global', 'The', $error);
  186. }
  187. return $error;
  188. }
  189. /**
  190. * Run some additional checks for the `$php_errormsg` variable.
  191. *
  192. * @since 8.1.0
  193. *
  194. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  195. * @param int $stackPtr The position of the current token in the
  196. * stack passed in $tokens.
  197. * @param array $tokens Token array of the current file.
  198. *
  199. * @return bool
  200. */
  201. private function isTargetPHPErrormsgVar(File $phpcsFile, $stackPtr, array $tokens)
  202. {
  203. $scopeStart = 0;
  204. /*
  205. * If the variable is detected within the scope of a function/closure, limit the checking.
  206. */
  207. $function = $phpcsFile->getCondition($stackPtr, \T_CLOSURE);
  208. if ($function === false) {
  209. $function = $phpcsFile->getCondition($stackPtr, \T_FUNCTION);
  210. }
  211. // It could also be a function param, which is not in the function scope.
  212. if ($function === false && isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
  213. $nestedParentheses = $tokens[$stackPtr]['nested_parenthesis'];
  214. $parenthesisCloser = end($nestedParentheses);
  215. if (isset($tokens[$parenthesisCloser]['parenthesis_owner'])
  216. && ($tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_FUNCTION
  217. || $tokens[$tokens[$parenthesisCloser]['parenthesis_owner']]['code'] === \T_CLOSURE)
  218. ) {
  219. $function = $tokens[$parenthesisCloser]['parenthesis_owner'];
  220. }
  221. }
  222. if ($function !== false) {
  223. $scopeStart = $tokens[$function]['scope_opener'];
  224. }
  225. /*
  226. * Now, let's do some additional checks.
  227. */
  228. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  229. // Is the variable being used as an array ?
  230. if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === \T_OPEN_SQUARE_BRACKET) {
  231. // The PHP native variable is a string, so this is probably not it
  232. // (except for array access to string, but why would you in this case ?).
  233. return false;
  234. }
  235. // Is this a variable assignment ?
  236. if ($nextNonEmpty !== false
  237. && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true
  238. ) {
  239. return false;
  240. }
  241. // Is this a function param shadowing the PHP native one ?
  242. if ($function !== false) {
  243. $parameters = PHPCSHelper::getMethodParameters($phpcsFile, $function);
  244. if (\is_array($parameters) === true && empty($parameters) === false) {
  245. foreach ($parameters as $param) {
  246. if ($param['name'] === '$php_errormsg') {
  247. return false;
  248. }
  249. }
  250. }
  251. }
  252. $skipPast = array(
  253. 'T_CLASS' => true,
  254. 'T_ANON_CLASS' => true,
  255. 'T_INTERFACE' => true,
  256. 'T_TRAIT' => true,
  257. 'T_FUNCTION' => true,
  258. 'T_CLOSURE' => true,
  259. );
  260. // Walk back and see if there is an assignment to the variable within the same scope.
  261. for ($i = ($stackPtr - 1); $i >= $scopeStart; $i--) {
  262. if ($tokens[$i]['code'] === \T_CLOSE_CURLY_BRACKET
  263. && isset($tokens[$i]['scope_condition'])
  264. && isset($skipPast[$tokens[$tokens[$i]['scope_condition']]['type']])
  265. ) {
  266. // Skip past functions, classes etc.
  267. $i = $tokens[$i]['scope_condition'];
  268. continue;
  269. }
  270. if ($tokens[$i]['code'] !== \T_VARIABLE || $tokens[$i]['content'] !== '$php_errormsg') {
  271. continue;
  272. }
  273. $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
  274. if ($nextNonEmpty !== false
  275. && isset(Tokens::$assignmentTokens[$tokens[$nextNonEmpty]['code']]) === true
  276. ) {
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. }