Skip to content

chore: use random_int() in some places #15017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function render(\Magento\Framework\DataObject $row)
*/
protected function _getCheckboxHtml($value, $checked)
{
$id = 'id_' . rand(0, 999);
$id = 'id_' . random_int(0, 999);
$html = '<label class="data-grid-checkbox-cell-inner" for="'. $id .'">';
$html .= '<input type="checkbox" name="' . $this->getColumn()->getName() . '" ';
$html .= 'id="' . $id . '" data-role="select-row"';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ protected function getTemporaryTreeIndexTableName()
if (empty($this->tempTreeIndexTableName)) {
$this->tempTreeIndexTableName = $this->connection->getTableName('temp_catalog_category_tree_index')
. '_'
. substr(md5(time() . rand(0, 999999999)), 0, 8);
. substr(md5(time() . random_int(0, 999999999)), 0, 8);
}

return $this->tempTreeIndexTableName;
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/SalesRule/Model/Coupon/Codegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function generateCode()
$length = $this->getActualLength();
$code = '';
for ($i = 0, $indexMax = strlen($alphabet) - 1; $i < $length; ++$i) {
$code .= substr($alphabet, mt_rand(0, $indexMax), 1);
$code .= substr($alphabet, random_int(0, $indexMax), 1);
}

return $code;
Expand All @@ -54,7 +54,7 @@ protected function getActualLength()
$lengthMin = $this->getLengthMin() ? $this->getLengthMin() : static::DEFAULT_LENGTH_MIN;
$lengthMax = $this->getLengthMax() ? $this->getLengthMax() : static::DEFAULT_LENGTH_MAX;

return $this->getLength() ? $this->getLength() : mt_rand($lengthMin, $lengthMax);
return $this->getLength() ? $this->getLength() : random_int($lengthMin, $lengthMax);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/SalesRule/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public function acquireCoupon($saveNewlyCreated = true, $saveAttemptCount = 10)
$coupon->setCode(
$couponCode . self::getCouponCodeGenerator()->getDelimiter() . sprintf(
'%04u',
rand(0, 9999)
random_int(0, 9999)
)
);
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Encryption/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(
$abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$initVector = '';
for ($i = 0; $i < $initVectorSize; $i++) {
$initVector .= $abc[rand(0, strlen($abc) - 1)];
$initVector .= $abc[random_int(0, strlen($abc) - 1)];
}
} elseif (false === $initVector) {
/* Set vector to zero bytes to not use it */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ class BackendFrontnameGenerator
public static function generate()
{
return self::ADMIN_AREA_PATH_PREFIX
. substr(base_convert(rand(0, PHP_INT_MAX), 10, 36), 0, self::ADMIN_AREA_PATH_RANDOM_PART_LENGTH);
. substr(base_convert(random_int(0, PHP_INT_MAX), 10, 36), 0, self::ADMIN_AREA_PATH_RANDOM_PART_LENGTH);
}
}
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Webapi/ErrorProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function apiShutdownFunction()
protected function _saveFatalErrorReport($reportData)
{
$this->directoryWrite->create('report/api');
$reportId = abs(intval(microtime(true) * rand(100, 1000)));
$reportId = abs(intval(microtime(true) * random_int(100, 1000)));
$this->directoryWrite->writeFile('report/api/' . $reportId, $this->serializer->serialize($reportData));
return $reportId;
}
Expand Down
2 changes: 1 addition & 1 deletion pub/errors/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ protected function _setReportData($reportData)
public function saveReport($reportData)
{
$this->reportData = $reportData;
$this->reportId = abs(intval(microtime(true) * rand(100, 1000)));
$this->reportId = abs(intval(microtime(true) * random_int(100, 1000)));
$this->_reportFile = $this->_reportDir . '/' . $this->reportId;
$this->_setReportData($reportData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AddressDataGenerator
public function generateAddress()
{
return [
'postcode' => mt_rand(10000, 99999)
'postcode' => random_int(10000, 99999)
];
}
}
4 changes: 2 additions & 2 deletions setup/src/Magento/Setup/Model/DataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ protected function readData()
*/
public function generate($minAmountOfWords, $maxAmountOfWords, $key = null)
{
$numberOfWords = mt_rand($minAmountOfWords, $maxAmountOfWords);
$numberOfWords = random_int($minAmountOfWords, $maxAmountOfWords);
$result = '';

if ($key === null || !array_key_exists($key, $this->generatedValues)) {
for ($i = 0; $i < $numberOfWords; $i++) {
$result .= ' ' . $this->dictionaryData[mt_rand(0, count($this->dictionaryData) - 1)];
$result .= ' ' . $this->dictionaryData[random_int(0, count($this->dictionaryData) - 1)];
}
$result = trim($result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function generate()
*/
private function generateRawDescription()
{
$paragraphsCount = mt_rand(
$paragraphsCount = random_int(
$this->descriptionConfig['paragraphs']['count-min'],
$this->descriptionConfig['paragraphs']['count-max']
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
*/
public function generate()
{
$sentencesCount = mt_rand(
$sentencesCount = random_int(
$this->paragraphConfig['sentences']['count-min'],
$this->paragraphConfig['sentences']['count-max']
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
*/
public function generate()
{
$sentenceWordsCount = mt_rand(
$sentenceWordsCount = random_int(
$this->sentenceConfig['words']['count-min'],
$this->sentenceConfig['words']['count-max']
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function apply($text)

return $this->wordWrapper->wrapWords(
$text,
$this->randomWordSelector->getRandomWords($rawText, mt_rand(5, 8)),
$this->randomWordSelector->getRandomWords($rawText, random_int(5, 8)),
'<b>%s</b>'
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getRandomWords($source, $count)
$randWords = [];
$wordsSize = count($words);
while ($count) {
$randWords[] = $words[mt_rand(0, $wordsSize - 1)];
$randWords[] = $words[random_int(0, $wordsSize - 1)];
$count--;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function apply($text)

return $this->wordWrapper->wrapWords(
$text,
$this->randomWordSelector->getRandomWords($rawText, mt_rand(5, 8)),
$this->randomWordSelector->getRandomWords($rawText, random_int(5, 8)),
'<i>%s</i>'
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function apply($text)

return $this->wordWrapper->wrapWords(
$text,
$this->randomWordSelector->getRandomWords($rawText, mt_rand(5, 8)),
$this->randomWordSelector->getRandomWords($rawText, random_int(5, 8)),
'<span>%s</span>'
);
}
Expand Down
2 changes: 1 addition & 1 deletion setup/src/Magento/Setup/Model/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getRandWord()
$this->readDictionary();
}

$randIndex = mt_rand(0, count($this->dictionary) - 1);
$randIndex = random_int(0, count($this->dictionary) - 1);
return trim($this->dictionary[$randIndex]);
}

Expand Down