random_api_migration.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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<string, string>``
  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']]``
  64. - `@PHP82Migration:risky <./../../ruleSets/PHP82MigrationRisky.rst>`_ with config:
  65. ``['replacements' => ['mt_rand' => 'random_int', 'rand' => 'random_int']]``
  66. References
  67. ----------
  68. - Fixer class: `PhpCsFixer\\Fixer\\Alias\\RandomApiMigrationFixer <./../../../src/Fixer/Alias/RandomApiMigrationFixer.php>`_
  69. - Test class: `PhpCsFixer\\Tests\\Fixer\\Alias\\RandomApiMigrationFixerTest <./../../../tests/Fixer/Alias/RandomApiMigrationFixerTest.php>`_
  70. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.