random_api_migration.rst 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. =============================
  2. Rule ``random_api_migration``
  3. =============================
  4. Replaces ``rand``, ``srand``, ``getrandmax`` functions calls with their ``mt_*``
  5. analogs.
  6. .. warning:: Using this rule is risky.
  7. Risky when the configured functions are overridden.
  8. Configuration
  9. -------------
  10. ``replacements``
  11. ~~~~~~~~~~~~~~~~
  12. Mapping between replaced functions with the new ones.
  13. Allowed types: ``array``
  14. Default value: ``['getrandmax' => 'mt_getrandmax', 'rand' => 'mt_rand', 'srand' => 'mt_srand']``
  15. Examples
  16. --------
  17. Example #1
  18. ~~~~~~~~~~
  19. *Default* configuration.
  20. .. code-block:: diff
  21. --- Original
  22. +++ New
  23. @@ -1,4 +1,4 @@
  24. <?php
  25. -$a = getrandmax();
  26. -$a = rand($b, $c);
  27. -$a = srand();
  28. +$a = mt_getrandmax();
  29. +$a = mt_rand($b, $c);
  30. +$a = mt_srand();
  31. Example #2
  32. ~~~~~~~~~~
  33. With configuration: ``['replacements' => ['getrandmax' => 'mt_getrandmax']]``.
  34. .. code-block:: diff
  35. --- Original
  36. +++ New
  37. @@ -1,4 +1,4 @@
  38. <?php
  39. -$a = getrandmax();
  40. +$a = mt_getrandmax();
  41. $a = rand($b, $c);
  42. $a = srand();
  43. Rule sets
  44. ---------
  45. The rule is part of the following rule sets:
  46. @PHP70Migration:risky
  47. Using the ``@PHP70Migration:risky`` rule set will enable the ``random_api_migration`` rule with the config below:
  48. ``['replacements' => ['mt_rand' => 'random_int', 'rand' => 'random_int']]``
  49. @PHP71Migration:risky
  50. Using the ``@PHP71Migration:risky`` rule set will enable the ``random_api_migration`` rule with the config below:
  51. ``['replacements' => ['mt_rand' => 'random_int', 'rand' => 'random_int']]``
  52. @PHP80Migration:risky
  53. Using the ``@PHP80Migration:risky`` rule set will enable the ``random_api_migration`` rule with the config below:
  54. ``['replacements' => ['mt_rand' => 'random_int', 'rand' => 'random_int']]``