Skip to content

Commit

Permalink
random_int and random_bytes are not deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 26, 2021
1 parent 9720ad1 commit f36fa71
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/functionMetadata_original.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@
// continue functionMap.php, line 424
'count' => ['hasSideEffects' => false],
'sprintf' => ['hasSideEffects' => false],

// random functions, do not have side effects but are not deterministic
'mt_rand' => ['hasSideEffects' => true],
'rand' => ['hasSideEffects' => true],
'random_bytes' => ['hasSideEffects' => true],
'random_int' => ['hasSideEffects' => true],

// methods
'DateTime::createFromFormat' => ['hasSideEffects' => false],
Expand Down
2 changes: 2 additions & 0 deletions resources/functionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,8 @@
'quotemeta' => ['hasSideEffects' => false],
'rad2deg' => ['hasSideEffects' => false],
'rand' => ['hasSideEffects' => true],
'random_bytes' => ['hasSideEffects' => true],
'random_int' => ['hasSideEffects' => true],
'range' => ['hasSideEffects' => false],
'rawurldecode' => ['hasSideEffects' => false],
'rawurlencode' => ['hasSideEffects' => false],
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5765,6 +5765,11 @@ public function dataDoNotRememberImpureFunctions(): array
return $this->gatherAssertTypes(__DIR__ . '/data/do-not-remember-impure-functions.php');
}

public function dataBug4190(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4190.php');
}

/**
* @dataProvider dataArrayFunctions
* @param string $description
Expand Down Expand Up @@ -11401,6 +11406,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataTernarySpecifiedTypes
* @dataProvider dataBug560
* @dataProvider dataDoNotRememberImpureFunctions
* @dataProvider dataBug4190
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4190.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug4190;

use function PHPStan\Analyser\assertType;

function (): string
{
if (random_int(0, 10) <= 5) {
return 'first try';
}

assertType('bool', random_int(0, 10) <= 5);

return 'foo';
};

0 comments on commit f36fa71

Please sign in to comment.