random_api_migration.rst 2.4 KB

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