random_api_migration.rst 2.0 KB

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