Skip to content

Commit

Permalink
Add more specific psalm type for result of Random::string() method (#…
Browse files Browse the repository at this point in the history
…53)

* Add more specific psalm type for result of `Random::string()` method

* fix typo
  • Loading branch information
vjik committed Mar 18, 2024
1 parent 0d44dd6 commit eb3743c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.0.2 under development

- Bug #35: Add missed `ext-hash` and `ext-openssl` dependencies (@vjik)
- Enh #53: Add more specific psalm type for result of `Random::string()` method (@vjik)

## 1.0.1 February 10, 2021

Expand Down
18 changes: 13 additions & 5 deletions src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Yiisoft\Security;

use Exception;
use InvalidArgumentException;
use Yiisoft\Strings\StringHelper;

/**
* Random allows generating random values.
*
* Currently it has a single method "string".
* Currently, it has a single method "string".
* The following extras are available via PHP directly:
*
* - `random_bytes()` for bytes. Note that output may not be ASCII.
Expand All @@ -23,19 +25,25 @@ final class Random
*
* @param int $length The length of the key in characters.
*
* @throws \Exception On failure.
* @throws Exception On failure.
*
* @return string The generated random key.
*
* @psalm-return non-empty-string
*/
public static function string(int $length = 32): string
{
if ($length < 1) {
throw new \InvalidArgumentException('First parameter ($length) must be greater than 0.');
throw new InvalidArgumentException('First parameter ($length) must be greater than 0.');
}

// Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64
/** @psalm-suppress ArgumentTypeCoercion */
/**
* Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64
* @psalm-suppress ArgumentTypeCoercion
*/
$bytes = random_bytes((int) ceil($length * 0.75));

Check warning on line 44 in src/Random.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Multiplication": --- Original +++ New @@ @@ * Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64 * @psalm-suppress ArgumentTypeCoercion */ - $bytes = random_bytes((int) ceil($length * 0.75)); + $bytes = random_bytes((int) ceil($length / 0.75)); /** @var non-empty-string */ return substr(StringHelper::base64UrlEncode($bytes), 0, $length); } }

Check warning on line 44 in src/Random.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "RoundingFamily": --- Original +++ New @@ @@ * Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64 * @psalm-suppress ArgumentTypeCoercion */ - $bytes = random_bytes((int) ceil($length * 0.75)); + $bytes = random_bytes((int) round($length * 0.75)); /** @var non-empty-string */ return substr(StringHelper::base64UrlEncode($bytes), 0, $length); } }

/** @var non-empty-string */
return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
}
}

0 comments on commit eb3743c

Please sign in to comment.