NoAliasFunctionsFixer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Fixer\Alias;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\Fixer\ConfigurableFixerTrait;
  16. use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
  17. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
  18. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
  19. use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
  20. use PhpCsFixer\FixerDefinition\CodeSample;
  21. use PhpCsFixer\FixerDefinition\FixerDefinition;
  22. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  23. use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
  24. use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
  25. use PhpCsFixer\Tokenizer\Token;
  26. use PhpCsFixer\Tokenizer\Tokens;
  27. /**
  28. * @author Vladimir Reznichenko <kalessil@gmail.com>
  29. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  30. *
  31. * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
  32. *
  33. * @phpstan-type _AutogeneratedInputConfiguration array{
  34. * sets?: list<'@all'|'@exif'|'@ftp'|'@IMAP'|'@internal'|'@ldap'|'@mbreg'|'@mysqli'|'@oci'|'@odbc'|'@openssl'|'@pcntl'|'@pg'|'@posix'|'@snmp'|'@sodium'|'@time'>
  35. * }
  36. * @phpstan-type _AutogeneratedComputedConfiguration array{
  37. * sets: list<'@all'|'@exif'|'@ftp'|'@IMAP'|'@internal'|'@ldap'|'@mbreg'|'@mysqli'|'@oci'|'@odbc'|'@openssl'|'@pcntl'|'@pg'|'@posix'|'@snmp'|'@sodium'|'@time'>
  38. * }
  39. */
  40. final class NoAliasFunctionsFixer extends AbstractFixer implements ConfigurableFixerInterface
  41. {
  42. /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
  43. use ConfigurableFixerTrait;
  44. private const SETS = [
  45. '@internal' => [
  46. 'diskfreespace' => 'disk_free_space',
  47. 'dns_check_record' => 'checkdnsrr',
  48. 'dns_get_mx' => 'getmxrr',
  49. 'session_commit' => 'session_write_close',
  50. 'stream_register_wrapper' => 'stream_wrapper_register',
  51. 'set_file_buffer' => 'stream_set_write_buffer',
  52. 'socket_set_blocking' => 'stream_set_blocking',
  53. 'socket_get_status' => 'stream_get_meta_data',
  54. 'socket_set_timeout' => 'stream_set_timeout',
  55. 'socket_getopt' => 'socket_get_option',
  56. 'socket_setopt' => 'socket_set_option',
  57. 'chop' => 'rtrim',
  58. 'close' => 'closedir',
  59. 'doubleval' => 'floatval',
  60. 'fputs' => 'fwrite',
  61. 'get_required_files' => 'get_included_files',
  62. 'ini_alter' => 'ini_set',
  63. 'is_double' => 'is_float',
  64. 'is_integer' => 'is_int',
  65. 'is_long' => 'is_int',
  66. 'is_real' => 'is_float',
  67. 'is_writeable' => 'is_writable',
  68. 'join' => 'implode',
  69. 'key_exists' => 'array_key_exists',
  70. 'magic_quotes_runtime' => 'set_magic_quotes_runtime',
  71. 'pos' => 'current',
  72. 'show_source' => 'highlight_file',
  73. 'sizeof' => 'count',
  74. 'strchr' => 'strstr',
  75. 'user_error' => 'trigger_error',
  76. ],
  77. '@IMAP' => [
  78. 'imap_create' => 'imap_createmailbox',
  79. 'imap_fetchtext' => 'imap_body',
  80. 'imap_header' => 'imap_headerinfo',
  81. 'imap_listmailbox' => 'imap_list',
  82. 'imap_listsubscribed' => 'imap_lsub',
  83. 'imap_rename' => 'imap_renamemailbox',
  84. 'imap_scan' => 'imap_listscan',
  85. 'imap_scanmailbox' => 'imap_listscan',
  86. ],
  87. '@ldap' => [
  88. 'ldap_close' => 'ldap_unbind',
  89. 'ldap_modify' => 'ldap_mod_replace',
  90. ],
  91. '@mysqli' => [
  92. 'mysqli_execute' => 'mysqli_stmt_execute',
  93. 'mysqli_set_opt' => 'mysqli_options',
  94. 'mysqli_escape_string' => 'mysqli_real_escape_string',
  95. ],
  96. '@pg' => [
  97. 'pg_exec' => 'pg_query',
  98. ],
  99. '@oci' => [
  100. 'oci_free_cursor' => 'oci_free_statement',
  101. ],
  102. '@odbc' => [
  103. 'odbc_do' => 'odbc_exec',
  104. 'odbc_field_precision' => 'odbc_field_len',
  105. ],
  106. '@mbreg' => [
  107. 'mbereg' => 'mb_ereg',
  108. 'mbereg_match' => 'mb_ereg_match',
  109. 'mbereg_replace' => 'mb_ereg_replace',
  110. 'mbereg_search' => 'mb_ereg_search',
  111. 'mbereg_search_getpos' => 'mb_ereg_search_getpos',
  112. 'mbereg_search_getregs' => 'mb_ereg_search_getregs',
  113. 'mbereg_search_init' => 'mb_ereg_search_init',
  114. 'mbereg_search_pos' => 'mb_ereg_search_pos',
  115. 'mbereg_search_regs' => 'mb_ereg_search_regs',
  116. 'mbereg_search_setpos' => 'mb_ereg_search_setpos',
  117. 'mberegi' => 'mb_eregi',
  118. 'mberegi_replace' => 'mb_eregi_replace',
  119. 'mbregex_encoding' => 'mb_regex_encoding',
  120. 'mbsplit' => 'mb_split',
  121. ],
  122. '@openssl' => [
  123. 'openssl_get_publickey' => 'openssl_pkey_get_public',
  124. 'openssl_get_privatekey' => 'openssl_pkey_get_private',
  125. ],
  126. '@sodium' => [
  127. 'sodium_crypto_scalarmult_base' => 'sodium_crypto_box_publickey_from_secretkey',
  128. ],
  129. '@exif' => [
  130. 'read_exif_data' => 'exif_read_data',
  131. ],
  132. '@ftp' => [
  133. 'ftp_quit' => 'ftp_close',
  134. ],
  135. '@posix' => [
  136. 'posix_errno' => 'posix_get_last_error',
  137. ],
  138. '@pcntl' => [
  139. 'pcntl_errno' => 'pcntl_get_last_error',
  140. ],
  141. '@time' => [
  142. 'mktime' => ['time', 0],
  143. 'gmmktime' => ['time', 0],
  144. ],
  145. ];
  146. /**
  147. * @var array<string, array{string, int}|string> stores alias (key) - master (value) functions mapping
  148. */
  149. private array $aliases = [];
  150. public function getDefinition(): FixerDefinitionInterface
  151. {
  152. return new FixerDefinition(
  153. 'Master functions shall be used instead of aliases.',
  154. [
  155. new CodeSample(
  156. '<?php
  157. $a = chop($b);
  158. close($b);
  159. $a = doubleval($b);
  160. $a = fputs($b, $c);
  161. $a = get_required_files();
  162. ini_alter($b, $c);
  163. $a = is_double($b);
  164. $a = is_integer($b);
  165. $a = is_long($b);
  166. $a = is_real($b);
  167. $a = is_writeable($b);
  168. $a = join($glue, $pieces);
  169. $a = key_exists($key, $array);
  170. magic_quotes_runtime($new_setting);
  171. $a = pos($array);
  172. $a = show_source($filename, true);
  173. $a = sizeof($b);
  174. $a = strchr($haystack, $needle);
  175. $a = imap_header($imap_stream, 1);
  176. user_error($message);
  177. mbereg_search_getregs();
  178. '
  179. ),
  180. new CodeSample(
  181. '<?php
  182. $a = is_double($b);
  183. mbereg_search_getregs();
  184. ',
  185. ['sets' => ['@mbreg']]
  186. ),
  187. ],
  188. null,
  189. 'Risky when any of the alias functions are overridden.'
  190. );
  191. }
  192. /**
  193. * {@inheritdoc}
  194. *
  195. * Must run before ImplodeCallFixer, PhpUnitDedicateAssertFixer.
  196. */
  197. public function getPriority(): int
  198. {
  199. return 40;
  200. }
  201. public function isCandidate(Tokens $tokens): bool
  202. {
  203. return $tokens->isTokenKindFound(T_STRING);
  204. }
  205. public function isRisky(): bool
  206. {
  207. return true;
  208. }
  209. protected function configurePostNormalisation(): void
  210. {
  211. $this->aliases = [];
  212. foreach ($this->configuration['sets'] as $set) {
  213. if ('@all' === $set) {
  214. $this->aliases = array_merge(...array_values(self::SETS));
  215. break;
  216. }
  217. if (!isset(self::SETS[$set])) {
  218. throw new \LogicException(\sprintf('Set %s passed option validation, but not part of ::SETS.', $set));
  219. }
  220. $this->aliases = array_merge($this->aliases, self::SETS[$set]);
  221. }
  222. }
  223. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  224. {
  225. $functionsAnalyzer = new FunctionsAnalyzer();
  226. $argumentsAnalyzer = new ArgumentsAnalyzer();
  227. /** @var Token $token */
  228. foreach ($tokens->findGivenKind(T_STRING) as $index => $token) {
  229. // check mapping hit
  230. $tokenContent = strtolower($token->getContent());
  231. if (!isset($this->aliases[$tokenContent])) {
  232. continue;
  233. }
  234. // skip expressions without parameters list
  235. $openParenthesis = $tokens->getNextMeaningfulToken($index);
  236. if (!$tokens[$openParenthesis]->equals('(')) {
  237. continue;
  238. }
  239. if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
  240. continue;
  241. }
  242. if (\is_array($this->aliases[$tokenContent])) {
  243. [$alias, $numberOfArguments] = $this->aliases[$tokenContent];
  244. $count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis));
  245. if ($numberOfArguments !== $count) {
  246. continue;
  247. }
  248. } else {
  249. $alias = $this->aliases[$tokenContent];
  250. }
  251. $tokens[$index] = new Token([T_STRING, $alias]);
  252. }
  253. }
  254. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  255. {
  256. $sets = [
  257. '@all' => 'all listed sets',
  258. '@internal' => 'native functions',
  259. '@exif' => 'EXIF functions',
  260. '@ftp' => 'FTP functions',
  261. '@IMAP' => 'IMAP functions',
  262. '@ldap' => 'LDAP functions',
  263. '@mbreg' => 'from `ext-mbstring`',
  264. '@mysqli' => 'mysqli functions',
  265. '@oci' => 'oci functions',
  266. '@odbc' => 'odbc functions',
  267. '@openssl' => 'openssl functions',
  268. '@pcntl' => 'PCNTL functions',
  269. '@pg' => 'pg functions',
  270. '@posix' => 'POSIX functions',
  271. '@snmp' => 'SNMP functions', // @TODO Remove on next major 4.0 as this set is now empty
  272. '@sodium' => 'libsodium functions',
  273. '@time' => 'time functions',
  274. ];
  275. $list = "List of sets to fix. Defined sets are:\n\n";
  276. foreach ($sets as $set => $description) {
  277. $list .= \sprintf("* `%s` (%s);\n", $set, $description);
  278. }
  279. $list = rtrim($list, ";\n").'.';
  280. return new FixerConfigurationResolver([
  281. (new FixerOptionBuilder('sets', $list))
  282. ->setAllowedTypes(['string[]'])
  283. ->setAllowedValues([new AllowedValueSubset(array_keys($sets))])
  284. ->setDefault(['@internal', '@IMAP', '@pg'])
  285. ->getOption(),
  286. ]);
  287. }
  288. }