Skip to content

Commit eac9773

Browse files
author
roadiz-ci
committed
chore: bumped
1 parent e217bf6 commit eac9773

11 files changed

Lines changed: 87 additions & 22 deletions

.github/workflows/run-test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
strategy:
2121
matrix:
22-
php-version: ['8.2', '8.3']
22+
php-version: ['8.1', '8.2', '8.3']
2323
steps:
2424
- uses: shivammathur/setup-php@v2
2525
with:
@@ -35,5 +35,7 @@ jobs:
3535
${{ runner.os }}-php-${{ matrix.php-version }}-
3636
- name: Install Dependencies
3737
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
38+
- name: Run PHP Code Sniffer
39+
run: vendor/bin/phpcs --extensions=php --warning-severity=0 --standard=PSR12 -p ./src
3840
- name: Run PHPStan
3941
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
vendor/bin/phpcs --report=full --report-file=./report.txt -p ./src
3+
vendor/bin/phpstan analyse -c phpstan.neon

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=8.2",
15+
"php": ">=8.1",
1616
"psr/log": ">=1.1",
1717
"ext-openssl": "*"
1818
},
1919
"require-dev": {
2020
"phpstan/phpstan": "^1.5.3",
21-
"phpstan/phpdoc-parser": "<2"
21+
"squizlabs/php_codesniffer": "^3.5"
2222
},
2323
"autoload": {
2424
"psr-4": {
@@ -27,8 +27,8 @@
2727
},
2828
"extra": {
2929
"branch-alias": {
30-
"dev-main": "2.5.x-dev",
31-
"dev-develop": "2.6.x-dev"
30+
"dev-main": "2.3.x-dev",
31+
"dev-develop": "2.4.x-dev"
3232
}
3333
}
3434
}

phpcs.xml.dist

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
4+
5+
<arg name="basepath" value="."/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
<arg name="extensions" value="php"/>
9+
10+
<rule ref="PSR12">
11+
<exclude name="Generic.Files.LineLength"/>
12+
</rule>
13+
<file>./src</file>
14+
</ruleset>

src/PasswordGenerator.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ class PasswordGenerator extends RandomGenerator implements PasswordGeneratorInte
1111
* one uppercase letter, one digit, and one special character. The remaining characters
1212
* in the password are chosen at random from those four sets.
1313
*
14-
* The available characters in each set are user-friendly - there are no ambiguous
14+
* The available characters in each set are user friendly - there are no ambiguous
1515
* characters such as i, l, 1, o, 0, etc.
1616
*
17+
* @param int $length
18+
* @return string
19+
*
1720
* @see https://gist.github.com/tylerhall/521810
1821
*/
19-
public function generatePassword(int $length = 16): string
22+
public function generatePassword(int $length = 12)
2023
{
2124
$sets = [];
2225
$sets[] = 'abcdefghjkmnpqrstuvwxyz';
2326
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
2427
$sets[] = '23456789';
25-
$sets[] = '!@#$%&*?-';
28+
$sets[] = '!@#$%&*?';
2629

2730
$all = '';
2831
$password = '';
@@ -32,10 +35,12 @@ public function generatePassword(int $length = 16): string
3235
}
3336

3437
$all = \mb_str_split($all);
35-
for ($i = 0; $i < $length - count($sets); ++$i) {
38+
for ($i = 0; $i < $length - count($sets); $i++) {
3639
$password .= $all[array_rand($all)];
3740
}
3841

39-
return str_shuffle($password);
42+
$password = str_shuffle($password);
43+
44+
return $password;
4045
}
4146
}

src/PasswordGeneratorInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66

77
interface PasswordGeneratorInterface
88
{
9-
public function generatePassword(int $length = 16): string;
9+
/**
10+
* @param int $length
11+
* @return string
12+
*/
13+
public function generatePassword(int $length = 12);
1014
}

src/RandomGenerator.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,47 @@
88

99
class RandomGenerator
1010
{
11-
public function __construct(protected readonly LoggerInterface $logger)
11+
protected ?LoggerInterface $logger;
12+
protected bool $useOpenSsl;
13+
14+
/**
15+
* @param LoggerInterface|null $logger
16+
*/
17+
public function __construct(LoggerInterface $logger = null)
1218
{
13-
if (!function_exists('openssl_random_pseudo_bytes')) {
14-
throw new \RuntimeException('You must enable the "openssl" extension for secure random number generation.');
19+
$this->logger = $logger;
20+
// determine whether to use OpenSSL
21+
if (defined('PHP_WINDOWS_VERSION_BUILD') && version_compare(PHP_VERSION, '5.3.4', '<')) {
22+
$this->useOpenSsl = false;
23+
} elseif (!function_exists('openssl_random_pseudo_bytes')) {
24+
if (null !== $this->logger) {
25+
$this->logger->notice('It is recommended that you enable the "openssl" extension for random number generation.');
26+
}
27+
$this->useOpenSsl = false;
28+
} else {
29+
$this->useOpenSsl = true;
1530
}
1631
}
1732

33+
/**
34+
* @param int $nbBytes
35+
* @return string
36+
*/
1837
public function getRandomNumber(int $nbBytes = 32): string
1938
{
2039
// try OpenSSL
21-
$bytes = \openssl_random_pseudo_bytes($nbBytes, $strong);
40+
if ($this->useOpenSsl) {
41+
$bytes = \openssl_random_pseudo_bytes($nbBytes, $strong);
42+
43+
if (false !== $bytes && true === $strong) {
44+
return $bytes;
45+
}
2246

23-
if (false !== $bytes && true === $strong) {
24-
return $bytes;
47+
if (null !== $this->logger) {
48+
$this->logger->info('OpenSSL did not produce a secure random number.');
49+
}
2550
}
2651

27-
throw new \RuntimeException('Unable to generate a secure random number.');
52+
return hash('sha256', uniqid((string) mt_rand(), true), true);
2853
}
2954
}

src/SaltGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
class SaltGenerator extends RandomGenerator implements SaltGeneratorInterface
88
{
9-
public function generateSalt(): string
9+
/**
10+
* @return string
11+
*/
12+
public function generateSalt()
1013
{
1114
return strtr(base64_encode($this->getRandomNumber(24)), '{}', '-_');
1215
}

src/SaltGeneratorInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
interface SaltGeneratorInterface
88
{
9-
public function generateSalt(): string;
9+
/**
10+
* @return string
11+
*/
12+
public function generateSalt();
1013
}

src/TokenGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
class TokenGenerator extends RandomGenerator implements TokenGeneratorInterface
88
{
9-
public function generateToken(): string
9+
/**
10+
* @return string
11+
*/
12+
public function generateToken()
1013
{
1114
return rtrim(strtr(base64_encode($this->getRandomNumber()), '+/', '-_'), '=');
1215
}

0 commit comments

Comments
 (0)