RemovedFunctionParametersSniff.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\FunctionUse;
  11. use PHPCompatibility\AbstractRemovedFeatureSniff;
  12. use PHP_CodeSniffer_File as File;
  13. use PHP_CodeSniffer_Tokens as Tokens;
  14. /**
  15. * Detect use of deprecated/removed function parameters in calls to native PHP functions.
  16. *
  17. * PHP version All
  18. *
  19. * @link https://www.php.net/manual/en/doc.changelog.php
  20. *
  21. * @since 7.0.0
  22. * @since 7.1.0 Now extends the `AbstractRemovedFeatureSniff` instead of the base `Sniff` class.
  23. */
  24. class RemovedFunctionParametersSniff extends AbstractRemovedFeatureSniff
  25. {
  26. /**
  27. * A list of removed function parameters, which were present in older versions.
  28. *
  29. * The array lists : version number with false (deprecated) and true (removed).
  30. * The index is the location of the parameter in the parameter list, starting at 0 !
  31. * If's sufficient to list the first version where the function parameter was deprecated/removed.
  32. *
  33. * The optional `callback` key can be used to pass a method name which should be called for an
  34. * additional check. The method will be passed the parameter info and should return true
  35. * if the notice should be thrown or false otherwise.
  36. *
  37. * @since 7.0.0
  38. * @since 7.0.2 Visibility changed from `public` to `protected`.
  39. * @since 9.3.0 Optional `callback` key.
  40. *
  41. * @var array
  42. */
  43. protected $removedFunctionParameters = array(
  44. 'curl_version' => array(
  45. 0 => array(
  46. 'name' => 'age',
  47. '7.4' => false,
  48. 'callback' => 'curlVersionInvalidValue',
  49. ),
  50. ),
  51. 'define' => array(
  52. 2 => array(
  53. 'name' => 'case_insensitive',
  54. '7.3' => false, // Slated for removal in PHP 8.0.0.
  55. ),
  56. ),
  57. 'gmmktime' => array(
  58. 6 => array(
  59. 'name' => 'is_dst',
  60. '5.1' => false,
  61. '7.0' => true,
  62. ),
  63. ),
  64. 'ldap_first_attribute' => array(
  65. 2 => array(
  66. 'name' => 'ber_identifier',
  67. '5.2.4' => true,
  68. ),
  69. ),
  70. 'ldap_next_attribute' => array(
  71. 2 => array(
  72. 'name' => 'ber_identifier',
  73. '5.2.4' => true,
  74. ),
  75. ),
  76. 'mktime' => array(
  77. 6 => array(
  78. 'name' => 'is_dst',
  79. '5.1' => false,
  80. '7.0' => true,
  81. ),
  82. ),
  83. );
  84. /**
  85. * Returns an array of tokens this test wants to listen for.
  86. *
  87. * @since 7.0.0
  88. *
  89. * @return array
  90. */
  91. public function register()
  92. {
  93. // Handle case-insensitivity of function names.
  94. $this->removedFunctionParameters = $this->arrayKeysToLowercase($this->removedFunctionParameters);
  95. return array(\T_STRING);
  96. }
  97. /**
  98. * Processes this test, when one of its tokens is encountered.
  99. *
  100. * @since 7.0.0
  101. *
  102. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  103. * @param int $stackPtr The position of the current token in
  104. * the stack passed in $tokens.
  105. *
  106. * @return void
  107. */
  108. public function process(File $phpcsFile, $stackPtr)
  109. {
  110. $tokens = $phpcsFile->getTokens();
  111. $ignore = array(
  112. \T_DOUBLE_COLON => true,
  113. \T_OBJECT_OPERATOR => true,
  114. \T_FUNCTION => true,
  115. \T_CONST => true,
  116. );
  117. $prevToken = $phpcsFile->findPrevious(\T_WHITESPACE, ($stackPtr - 1), null, true);
  118. if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
  119. // Not a call to a PHP function.
  120. return;
  121. }
  122. $function = $tokens[$stackPtr]['content'];
  123. $functionLc = strtolower($function);
  124. if (isset($this->removedFunctionParameters[$functionLc]) === false) {
  125. return;
  126. }
  127. $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr);
  128. $parameterCount = \count($parameters);
  129. if ($parameterCount === 0) {
  130. return;
  131. }
  132. // If the parameter count returned > 0, we know there will be valid open parenthesis.
  133. $openParenthesis = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
  134. $parameterOffsetFound = $parameterCount - 1;
  135. foreach ($this->removedFunctionParameters[$functionLc] as $offset => $parameterDetails) {
  136. if ($offset <= $parameterOffsetFound) {
  137. if (isset($parameterDetails['callback']) && method_exists($this, $parameterDetails['callback'])) {
  138. if ($this->{$parameterDetails['callback']}($phpcsFile, $parameters[($offset + 1)]) === false) {
  139. continue;
  140. }
  141. }
  142. $itemInfo = array(
  143. 'name' => $function,
  144. 'nameLc' => $functionLc,
  145. 'offset' => $offset,
  146. );
  147. $this->handleFeature($phpcsFile, $openParenthesis, $itemInfo);
  148. }
  149. }
  150. }
  151. /**
  152. * Get the relevant sub-array for a specific item from a multi-dimensional array.
  153. *
  154. * @since 7.1.0
  155. *
  156. * @param array $itemInfo Base information about the item.
  157. *
  158. * @return array Version and other information about the item.
  159. */
  160. public function getItemArray(array $itemInfo)
  161. {
  162. return $this->removedFunctionParameters[$itemInfo['nameLc']][$itemInfo['offset']];
  163. }
  164. /**
  165. * Get an array of the non-PHP-version array keys used in a sub-array.
  166. *
  167. * @since 7.1.0
  168. *
  169. * @return array
  170. */
  171. protected function getNonVersionArrayKeys()
  172. {
  173. return array('name', 'callback');
  174. }
  175. /**
  176. * Retrieve the relevant detail (version) information for use in an error message.
  177. *
  178. * @since 7.1.0
  179. *
  180. * @param array $itemArray Version and other information about the item.
  181. * @param array $itemInfo Base information about the item.
  182. *
  183. * @return array
  184. */
  185. public function getErrorInfo(array $itemArray, array $itemInfo)
  186. {
  187. $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
  188. $errorInfo['paramName'] = $itemArray['name'];
  189. return $errorInfo;
  190. }
  191. /**
  192. * Get the item name to be used for the creation of the error code.
  193. *
  194. * @since 7.1.0
  195. *
  196. * @param array $itemInfo Base information about the item.
  197. * @param array $errorInfo Detail information about an item.
  198. *
  199. * @return string
  200. */
  201. protected function getItemName(array $itemInfo, array $errorInfo)
  202. {
  203. return $itemInfo['name'] . '_' . $errorInfo['paramName'];
  204. }
  205. /**
  206. * Get the error message template for this sniff.
  207. *
  208. * @since 7.1.0
  209. *
  210. * @return string
  211. */
  212. protected function getErrorMsgTemplate()
  213. {
  214. return 'The "%s" parameter for function %s() is ';
  215. }
  216. /**
  217. * Filter the error data before it's passed to PHPCS.
  218. *
  219. * @since 7.1.0
  220. *
  221. * @param array $data The error data array which was created.
  222. * @param array $itemInfo Base information about the item this error message applies to.
  223. * @param array $errorInfo Detail information about an item this error message applies to.
  224. *
  225. * @return array
  226. */
  227. protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
  228. {
  229. array_shift($data);
  230. array_unshift($data, $errorInfo['paramName'], $itemInfo['name']);
  231. return $data;
  232. }
  233. /**
  234. * Check whether curl_version() was passed the default CURLVERSION_NOW.
  235. *
  236. * @since 9.3.0
  237. *
  238. * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
  239. * @param array $parameter Parameter info array.
  240. *
  241. * @return bool True if the value was not CURLVERSION_NOW, false otherwise.
  242. */
  243. protected function curlVersionInvalidValue(File $phpcsFile, array $parameter)
  244. {
  245. $tokens = $phpcsFile->getTokens();
  246. $raw = '';
  247. for ($i = $parameter['start']; $i <= $parameter['end']; $i++) {
  248. if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) {
  249. continue;
  250. }
  251. $raw .= $tokens[$i]['content'];
  252. }
  253. if ($raw !== 'CURLVERSION_NOW'
  254. && $raw !== (string) \CURLVERSION_NOW
  255. ) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. }