Skip to content

Commit

Permalink
Remove usage of mt_rand (#87)
Browse files Browse the repository at this point in the history
* avoid mt_rand usage

* avoid mt_rand usage in Guessers

* use int literal

* use PHP_INT_MAX for big numbers

* use self::numberBetween() instead
  • Loading branch information
IonBazan authored Dec 4, 2020
1 parent ad43ce8 commit d26ac52
Show file tree
Hide file tree
Showing 44 changed files with 196 additions and 191 deletions.
8 changes: 4 additions & 4 deletions src/Faker/ORM/CakePHP/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function guessFormat($column, $table)
return $generator->boolean;
};
case 'integer':
return function () {
return mt_rand(0, intval('2147483647'));
return function () use ($generator) {
return $generator->numberBetween(0, 2147483647);
};
case 'biginteger':
return function () {
return mt_rand(0, intval('9223372036854775807'));
return function () use ($generator) {
return $generator->numberBetween(0, PHP_INT_MAX);
};
case 'decimal':
case 'float':
Expand Down
22 changes: 10 additions & 12 deletions src/Faker/ORM/Doctrine/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
namespace Faker\ORM\Doctrine;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Faker\Generator;

class ColumnTypeGuesser
{
protected $generator;

/**
* @param \Faker\Generator $generator
*/
public function __construct(\Faker\Generator $generator)
public function __construct(Generator $generator)
{
$this->generator = $generator;
}
Expand All @@ -36,20 +34,20 @@ public function guessFormat($fieldName, ClassMetadata $class)
return $generator->randomNumber($size + 2) / 100;
};
case 'smallint':
return function () {
return mt_rand(0, 65535);
return function () use ($generator) {
return $generator->numberBetween(0, 65535);
};
case 'integer':
return function () {
return mt_rand(0, intval('2147483647'));
return function () use ($generator) {
return $generator->numberBetween(0, 2147483647);
};
case 'bigint':
return function () {
return mt_rand(0, intval('18446744073709551615'));
return function () use ($generator) {
return $generator->numberBetween(0, PHP_INT_MAX);
};
case 'float':
return function () {
return mt_rand(0, intval('4294967295')) / mt_rand(1, intval('4294967295'));
return function () use ($generator) {
return $generator->randomFloat();
};
case 'string':
$size = $class->fieldMappings[$fieldName]['length'] ?? 255;
Expand Down
4 changes: 2 additions & 2 deletions src/Faker/ORM/Doctrine/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function guessColumnFormatters(\Faker\Generator $generator)
}

$index = 0;
$formatters[$assocName] = function ($inserted) use ($relatedClass, &$index, $unique, $optional) {
$formatters[$assocName] = function ($inserted) use ($relatedClass, &$index, $unique, $optional, $generator) {
if (isset($inserted[$relatedClass])) {
if ($unique) {
$related = null;
Expand All @@ -152,7 +152,7 @@ public function guessColumnFormatters(\Faker\Generator $generator)
return $related;
}

return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
return $generator->randomElement($inserted[$relatedClass]);
}

return null;
Expand Down
19 changes: 12 additions & 7 deletions src/Faker/ORM/Mandango/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace Faker\ORM\Mandango;

use Faker\Generator;

class ColumnTypeGuesser
{
/**
* @var Generator
*/
protected $generator;

/**
* @param \Faker\Generator $generator
* @param Generator $generator
*/
public function __construct(\Faker\Generator $generator)
public function __construct(Generator $generator)
{
$this->generator = $generator;
}
Expand All @@ -26,20 +31,20 @@ public function guessFormat($field)
return $generator->boolean;
};
case 'integer':
return function () {
return mt_rand(0, intval('4294967295'));
return function () use ($generator) {
return $generator->numberBetween(0, 4294967295);
};
case 'float':
return function () {
return mt_rand(0, intval('4294967295')) / mt_rand(1, intval('4294967295'));
return function () use ($generator) {
return $generator->randomFloat();
};
case 'string':
return function () use ($generator) {
return $generator->text(255);
};
case 'date':
return function () use ($generator) {
return $generator->datetime;
return $generator->dateTime;
};
default:
// no smart way to guess what the user expects here
Expand Down
25 changes: 11 additions & 14 deletions src/Faker/ORM/Propel/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,27 @@ public function guessFormat(ColumnMap $column)
return $generator->randomNumber($size + 2) / 100;
};
case PropelColumnTypes::TINYINT:
return function () {
return mt_rand(0, 127);
return function () use ($generator) {
return $generator->numberBetween(0, 127);
};
case PropelColumnTypes::SMALLINT:
return function () {
return mt_rand(0, 32767);
return function () use ($generator) {
return $generator->numberBetween(0, 32767);
};
case PropelColumnTypes::INTEGER:
return function () {
return mt_rand(0, intval('2147483647'));
return function () use ($generator) {
return $generator->numberBetween(0, 2147483647);
};
case PropelColumnTypes::BIGINT:
return function () {
return mt_rand(0, intval('9223372036854775807'));
return function () use ($generator) {
return $generator->numberBetween(0, PHP_INT_MAX);
};
case PropelColumnTypes::FLOAT:
return function () {
return mt_rand(0, intval('2147483647')) / mt_rand(1, intval('2147483647'));
};
case PropelColumnTypes::DOUBLE:
case PropelColumnTypes::REAL:
return function () {
return mt_rand(0, intval('9223372036854775807')) / mt_rand(1, intval('9223372036854775807'));
};
return function () use ($generator) {
return $generator->randomFloat();
};
case PropelColumnTypes::CHAR:
case PropelColumnTypes::VARCHAR:
case PropelColumnTypes::BINARY:
Expand Down
9 changes: 4 additions & 5 deletions src/Faker/ORM/Propel/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function guessColumnFormatters(\Faker\Generator $generator)
}
if ($columnMap->isForeignKey()) {
$relatedClass = $columnMap->getRelation()->getForeignTable()->getClassname();
$formatters[$columnMap->getPhpName()] = function ($inserted) use ($relatedClass) {
return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
$formatters[$columnMap->getPhpName()] = function ($inserted) use ($relatedClass, $generator) {
return isset($inserted[$relatedClass]) ? $generator->randomElement($inserted[$relatedClass]) : null;
};
continue;
}
Expand Down Expand Up @@ -157,9 +157,8 @@ public function guessModifiers(\Faker\Generator $generator)
};
break;
case 'sortable':
$modifiers['sortable'] = function ($obj, $inserted) use ($class) {
$maxRank = isset($inserted[$class]) ? count($inserted[$class]) : 0;
$obj->insertAtRank(mt_rand(1, $maxRank + 1));
$modifiers['sortable'] = function ($obj, $inserted) use ($class, $generator) {
$obj->insertAtRank($generator->numberBetween(1, count($inserted[$class] ?? []) + 1));
};
break;
}
Expand Down
23 changes: 10 additions & 13 deletions src/Faker/ORM/Propel2/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,26 @@ public function guessFormat(ColumnMap $column)
return $generator->randomNumber($size + 2) / 100;
};
case PropelTypes::TINYINT:
return function () {
return mt_rand(0, 127);
return function () use ($generator) {
return $generator->numberBetween(0, 127);
};
case PropelTypes::SMALLINT:
return function () {
return mt_rand(0, 32767);
return function () use ($generator) {
return $generator->numberBetween(0, 32767);
};
case PropelTypes::INTEGER:
return function () {
return mt_rand(0, intval('2147483647'));
return function () use ($generator) {
return $generator->numberBetween(0, 2147483647);
};
case PropelTypes::BIGINT:
return function () {
return mt_rand(0, intval('9223372036854775807'));
return function () use ($generator) {
return $generator->numberBetween(0, PHP_INT_MAX);
};
case PropelTypes::FLOAT:
return function () {
return mt_rand(0, intval('2147483647')) / mt_rand(1, intval('2147483647'));
};
case PropelTypes::DOUBLE:
case PropelTypes::REAL:
return function () {
return mt_rand(0, intval('9223372036854775807')) / mt_rand(1, intval('9223372036854775807'));
return function () use ($generator) {
return $generator->randomFloat();
};
case PropelTypes::CHAR:
case PropelTypes::VARCHAR:
Expand Down
9 changes: 4 additions & 5 deletions src/Faker/ORM/Propel2/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function guessColumnFormatters(\Faker\Generator $generator)
}
if ($columnMap->isForeignKey()) {
$relatedClass = $columnMap->getRelation()->getForeignTable()->getClassname();
$formatters[$columnMap->getPhpName()] = function ($inserted) use ($relatedClass) {
$formatters[$columnMap->getPhpName()] = function ($inserted) use ($relatedClass, $generator) {
$relatedClass = trim($relatedClass, "\\");
return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
return isset($inserted[$relatedClass]) ? $generator->randomElement($inserted[$relatedClass]) : null;
};
continue;
}
Expand Down Expand Up @@ -158,9 +158,8 @@ public function guessModifiers(\Faker\Generator $generator)
};
break;
case 'sortable':
$modifiers['sortable'] = function ($obj, $inserted) use ($class) {
$maxRank = isset($inserted[$class]) ? count($inserted[$class]) : 0;
$obj->insertAtRank(mt_rand(1, $maxRank + 1));
$modifiers['sortable'] = function ($obj, $inserted) use ($class, $generator) {
$obj->insertAtRank($generator->numberBetween(1, count($inserted[$class] ?? []) + 1));
};
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Faker/ORM/Spot/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public function guessFormat(array $field)
};
case 'integer':
return function () use ($generator) {
return $generator->numberBetween(0, intval('2147483647'));
return $generator->numberBetween(0, 2147483647);
};
case 'bigint':
return function () use ($generator) {
return $generator->numberBetween(0, intval('18446744073709551615'));
return $generator->numberBetween(0, PHP_INT_MAX);
};
case 'float':
return function () use ($generator) {
return $generator->randomFloat(null, 0, intval('4294967295'));
return $generator->randomFloat(null, 0, 4294967295);
};
case 'string':
$size = $field['length'] ?? 255;
Expand Down
6 changes: 3 additions & 3 deletions src/Faker/ORM/Spot/EntityPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public function guessColumnFormatters(Generator $generator)

$locator = $this->locator;

$formatters[$fieldName] = function ($inserted) use ($required, $entityName, $locator) {
$formatters[$fieldName] = function ($inserted) use ($required, $entityName, $locator, $generator) {
if (!empty($inserted[$entityName])) {
return $inserted[$entityName][mt_rand(0, count($inserted[$entityName]) - 1)]->get('id');
return $generator->randomElement($inserted[$entityName])->get('id');
}

if ($required && $this->useExistingData) {
Expand All @@ -154,7 +154,7 @@ public function guessColumnFormatters(Generator $generator)
return null;
}

return $records[mt_rand(0, count($records) - 1)]['id'];
return $generator->randomElement($records)['id'];
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/Provider/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function isbn10()
*/
public function isbn13()
{
$code = '97' . static::numberBetween(8, 9) . static::numerify(str_repeat('#', 9));
$code = '97' . self::numberBetween(8, 9) . static::numerify(str_repeat('#', 9));

return $code . Ean::checksum($code);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Faker/Provider/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ class Color extends Base
*/
public static function hexColor()
{
return '#' . str_pad(dechex(mt_rand(1, 16777215)), 6, '0', STR_PAD_LEFT);
return '#' . str_pad(dechex(self::numberBetween(1, 16777215)), 6, '0', STR_PAD_LEFT);
}

/**
* @example '#ff0044'
*/
public static function safeHexColor()
{
$color = str_pad(dechex(mt_rand(0, 255)), 3, '0', STR_PAD_LEFT);
$color = str_pad(dechex(self::numberBetween(0, 255)), 3, '0', STR_PAD_LEFT);

return '#' . $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
Expand Down Expand Up @@ -122,9 +122,9 @@ public static function hslColor()
{
return sprintf(
'%s,%s,%s',
static::numberBetween(0, 360),
static::numberBetween(0, 100),
static::numberBetween(0, 100)
self::numberBetween(0, 360),
self::numberBetween(0, 100),
self::numberBetween(0, 100)
);
}

Expand All @@ -135,9 +135,9 @@ public static function hslColor()
public static function hslColorAsArray()
{
return [
static::numberBetween(0, 360),
static::numberBetween(0, 100),
static::numberBetween(0, 100)
self::numberBetween(0, 360),
self::numberBetween(0, 100),
self::numberBetween(0, 100)
];
}
}
6 changes: 3 additions & 3 deletions src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected static function getMaxTimestamp($max = 'now')
*/
public static function unixTime($max = 'now')
{
return mt_rand(0, static::getMaxTimestamp($max));
return self::numberBetween(0, static::getMaxTimestamp($max));
}

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ public static function dateTimeAD($max = 'now', $timezone = null)
{
$min = (PHP_INT_SIZE > 4 ? -62135597361 : -PHP_INT_MAX);
return static::setTimezone(
new \DateTime('@' . mt_rand($min, static::getMaxTimestamp($max))),
new \DateTime('@' . self::numberBetween($min, static::getMaxTimestamp($max))),
$timezone
);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now
throw new \InvalidArgumentException('Start date must be anterior to end date.');
}

$timestamp = mt_rand($startTimestamp, $endTimestamp);
$timestamp = self::numberBetween($startTimestamp, $endTimestamp);

return static::setTimezone(
new \DateTime('@' . $timestamp),
Expand Down
Loading

0 comments on commit d26ac52

Please sign in to comment.