diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index 32d4c5b..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,61 +0,0 @@ -name('/\.php|\.php.dist$/') - ->exclude('build') - ->exclude('demo') - ->exclude('docs') - ->in(['lib', 'tests', 'testsDependency']) -; - -$config = new PhpCsFixer\Config(); - -return $config->setRules(array( - '@PSR2' => true, - '@PSR12' => true, - '@PHP82Migration' => true, - 'array_syntax' => ['syntax' => 'long'], - 'class_attributes_separation' => true, - 'declare_strict_types' => true, - 'dir_constant' => true, - 'is_null' => true, - 'no_homoglyph_names' => true, - 'no_null_property_initialization' => true, - 'no_php4_constructor' => true, - 'no_unused_imports' => true, - 'no_useless_else' => true, - 'non_printable_character' => true, - 'ordered_imports' => true, - 'ordered_class_elements' => true, - 'php_unit_construct' => true, - 'pow_to_exponentiation' => true, - 'psr_autoloading' => true, - 'random_api_migration' => true, - 'return_assignment' => true, - 'self_accessor' => true, - 'semicolon_after_instruction' => true, - 'short_scalar_cast' => true, - 'simplified_null_return' => true, - 'single_blank_line_before_namespace' => true, - 'single_class_element_per_statement' => true, - 'single_line_comment_style' => true, - 'single_quote' => true, - 'space_after_semicolon' => true, - 'standardize_not_equals' => true, - 'strict_param' => true, - 'ternary_operator_spaces' => true, - 'trailing_comma_in_multiline' => true, - 'trim_array_spaces' => true, - 'unary_operator_spaces' => true, - 'global_namespace_import' => [ - 'import_classes' => true, - 'import_functions' => true, - 'import_constants' => true, - ], -)) - ->setFinder($finder) - ->setRiskyAllowed(true) -; diff --git a/composer.json b/composer.json index a3264a0..44dcd66 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,9 @@ }, "require-dev": { "phpunit/phpunit": "^9", - "friendsofphp/php-cs-fixer": "^3.13", - "phpstan/phpstan": "^1.9" + "php-parallel-lint/php-parallel-lint": "^1.3", + "phpstan/phpstan": "^1.9", + "symplify/easy-coding-standard": "^11.3" }, "suggest": { "bacon/bacon-qr-code": "Needed for BaconQrCodeProvider provider", @@ -50,13 +51,15 @@ }, "scripts": { "phpstan": [ - "phpstan analyze --xdebug lib tests testsDependency" + "phpstan analyze" ], "lint": [ - "php-cs-fixer fix -v" + "parallel-lint --exclude vendor .", + "ecs --fix" ], "lint-ci": [ - "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --dry-run --stop-on-violation" + "parallel-lint --exclude vendor .", + "ecs" ], "test": [ "XDEBUG_MODE=coverage phpunit" diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..94d88ae --- /dev/null +++ b/ecs.php @@ -0,0 +1,33 @@ +paths([ + __DIR__ . '/lib', + __DIR__ . '/tests', + __DIR__ . '/testsDependency', + ]); + + $ecsConfig->sets([ + SetList::ARRAY, + SetList::CLEAN_CODE, + SetList::CONTROL_STRUCTURES, + SetList::DOCBLOCK, + SetList::NAMESPACES, + SetList::PSR_12, + SetList::SPACES, + ]); + + $ecsConfig->dynamicSets([ + '@PHP82Migration', + ]); + + $ecsConfig->rule(ArraySyntaxFixer::class); + $ecsConfig->rule(TypesSpacesFixer::class); +}; diff --git a/lib/Providers/Qr/BaconQrCodeProvider.php b/lib/Providers/Qr/BaconQrCodeProvider.php index 4b53445..d7f1613 100644 --- a/lib/Providers/Qr/BaconQrCodeProvider.php +++ b/lib/Providers/Qr/BaconQrCodeProvider.php @@ -25,7 +25,7 @@ public function __construct( private readonly int $borderWidth = 4, private string|array $backgroundColour = '#ffffff', private string|array $foregroundColour = '#000000', - private string $format = 'png', + private string $format = 'png', ) { $this->backgroundColour = $this->handleColour($this->backgroundColour); $this->foregroundColour = $this->handleColour($this->foregroundColour); @@ -75,10 +75,10 @@ public function getQRCodeImage(string $qrText, int $size): string */ private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backend) { - $rendererStyleArgs = array($size, $this->borderWidth); + $rendererStyleArgs = [$size, $this->borderWidth]; if (is_array($this->foregroundColour) && is_array($this->backgroundColour)) { - $rendererStyleArgs = array(...$rendererStyleArgs, ...array( + $rendererStyleArgs = [...$rendererStyleArgs, ...[ null, null, Fill::withForegroundColor( @@ -88,7 +88,7 @@ private function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backe new EyeFill(null, null), new EyeFill(null, null) ), - )); + ]]; } $writer = new Writer(new ImageRenderer( diff --git a/lib/Providers/Qr/BaseHTTPQRCodeProvider.php b/lib/Providers/Qr/BaseHTTPQRCodeProvider.php index a0c3d82..36af41e 100644 --- a/lib/Providers/Qr/BaseHTTPQRCodeProvider.php +++ b/lib/Providers/Qr/BaseHTTPQRCodeProvider.php @@ -12,7 +12,7 @@ protected function getContent(string $url): string|bool { $curlhandle = curl_init(); - curl_setopt_array($curlhandle, array( + curl_setopt_array($curlhandle, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 10, @@ -20,7 +20,7 @@ protected function getContent(string $url): string|bool CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => $this->verifyssl, CURLOPT_USERAGENT => 'TwoFactorAuth', - )); + ]); $data = curl_exec($curlhandle); curl_close($curlhandle); diff --git a/lib/Providers/Qr/EndroidQrCodeProvider.php b/lib/Providers/Qr/EndroidQrCodeProvider.php index 3750df5..5cec718 100755 --- a/lib/Providers/Qr/EndroidQrCodeProvider.php +++ b/lib/Providers/Qr/EndroidQrCodeProvider.php @@ -43,7 +43,7 @@ public function getMimeType(): string public function getQRCodeImage(string $qrText, int $size): string { - if (!$this->endroid4) { + if (! $this->endroid4) { return $this->qrCodeInstance($qrText, $size)->writeString(); } @@ -71,7 +71,14 @@ private function handleColor(string $color): Color|array $g = hexdec($split[1]); $b = hexdec($split[2]); - return $this->endroid4 ? new Color($r, $g, $b, 0) : array('r' => $r, 'g' => $g, 'b' => $b, 'a' => 0); + return $this->endroid4 + ? new Color($r, $g, $b, 0) + : [ + 'r' => $r, + 'g' => $g, + 'b' => $b, + 'a' => 0, + ]; } private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface|ErrorCorrectionLevel diff --git a/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php b/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php index 0a0b2d2..cd57d0e 100755 --- a/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php +++ b/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php @@ -22,12 +22,12 @@ class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider public function setLogo($path, $size = null) { $this->logoPath = $path; - $this->logoSize = (array)$size; + $this->logoSize = (array) $size; } public function getQRCodeImage(string $qrText, int $size): string { - if (!$this->endroid4) { + if (! $this->endroid4) { return $this->qrCodeInstance($qrText, $size)->writeString(); } @@ -49,7 +49,7 @@ protected function qrCodeInstance(string $qrText, int $size): QrCode { $qrCode = parent::qrCodeInstance($qrText, $size); - if (!$this->endroid4 && $this->logoPath) { + if (! $this->endroid4 && $this->logoPath) { $qrCode->setLogoPath($this->logoPath); if ($this->logoSize) { $qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1] ?? null); diff --git a/lib/Providers/Qr/GoogleChartsQrCodeProvider.php b/lib/Providers/Qr/GoogleChartsQrCodeProvider.php index 4dd6c2f..174f8da 100644 --- a/lib/Providers/Qr/GoogleChartsQrCodeProvider.php +++ b/lib/Providers/Qr/GoogleChartsQrCodeProvider.php @@ -7,8 +7,12 @@ // https://developers.google.com/chart/infographics/docs/qr_codes class GoogleChartsQrCodeProvider extends BaseHTTPQRCodeProvider { - public function __construct(protected bool $verifyssl = false, public string $errorcorrectionlevel = 'L', public int $margin = 4, public string $encoding = 'UTF-8') - { + public function __construct( + protected bool $verifyssl = false, + public string $errorcorrectionlevel = 'L', + public int $margin = 4, + public string $encoding = 'UTF-8' + ) { } public function getMimeType(): string @@ -23,13 +27,13 @@ public function getQRCodeImage(string $qrText, int $size): string public function getUrl(string $qrText, int $size): string { - $queryParameters = array( + $queryParameters = [ 'chs' => $size . 'x' . $size, 'chld' => strtoupper($this->errorcorrectionlevel) . '|' . $this->margin, 'cht' => 'qr', 'choe' => $this->encoding, 'chl' => $qrText, - ); + ]; return 'https://chart.googleapis.com/chart?' . http_build_query($queryParameters); } diff --git a/lib/Providers/Qr/HandlesDataUri.php b/lib/Providers/Qr/HandlesDataUri.php index 8b07845..f62370a 100644 --- a/lib/Providers/Qr/HandlesDataUri.php +++ b/lib/Providers/Qr/HandlesDataUri.php @@ -15,11 +15,11 @@ trait HandlesDataUri private function DecodeDataUri(string $datauri): ?array { if (preg_match('/data:(?P[\w\.\-\+\/]+);(?P\w+),(?P.*)/', $datauri, $m) === 1) { - return array( + return [ 'mimetype' => $m['mimetype'], 'encoding' => $m['encoding'], 'data' => base64_decode($m['data'], true), - ); + ]; } return null; diff --git a/lib/Providers/Qr/ImageChartsQRCodeProvider.php b/lib/Providers/Qr/ImageChartsQRCodeProvider.php index 2f1079e..8484d64 100644 --- a/lib/Providers/Qr/ImageChartsQRCodeProvider.php +++ b/lib/Providers/Qr/ImageChartsQRCodeProvider.php @@ -9,8 +9,11 @@ */ class ImageChartsQRCodeProvider extends BaseHTTPQRCodeProvider { - public function __construct(protected bool $verifyssl = false, public string $errorcorrectionlevel = 'L', public int $margin = 1) - { + public function __construct( + protected bool $verifyssl = false, + public string $errorcorrectionlevel = 'L', + public int $margin = 1 + ) { } public function getMimeType(): string @@ -25,12 +28,12 @@ public function getQRCodeImage(string $qrText, int $size): string public function getUrl(string $qrText, int $size): string { - $queryParameters = array( + $queryParameters = [ 'cht' => 'qr', 'chs' => ceil($size / 2) . 'x' . ceil($size / 2), 'chld' => $this->errorcorrectionlevel . '|' . $this->margin, 'chl' => $qrText, - ); + ]; return 'https://image-charts.com/chart?' . http_build_query($queryParameters); } diff --git a/lib/Providers/Qr/QRServerProvider.php b/lib/Providers/Qr/QRServerProvider.php index 9244795..cc7c134 100644 --- a/lib/Providers/Qr/QRServerProvider.php +++ b/lib/Providers/Qr/QRServerProvider.php @@ -9,8 +9,15 @@ */ class QRServerProvider extends BaseHTTPQRCodeProvider { - public function __construct(protected bool $verifyssl = false, public string $errorcorrectionlevel = 'L', public int $margin = 4, public int $qzone = 1, public string $bgcolor = 'ffffff', public string $color = '000000', public string $format = 'png') - { + public function __construct( + protected bool $verifyssl = false, + public string $errorcorrectionlevel = 'L', + public int $margin = 4, + public int $qzone = 1, + public string $bgcolor = 'ffffff', + public string $color = '000000', + public string $format = 'png' + ) { } public function getMimeType(): string @@ -38,7 +45,7 @@ public function getQRCodeImage(string $qrText, int $size): string public function getUrl(string $qrText, int $size): string { - $queryParameters = array( + $queryParameters = [ 'size' => $size . 'x' . $size, 'ecc' => strtoupper($this->errorcorrectionlevel), 'margin' => $this->margin, @@ -47,7 +54,7 @@ public function getUrl(string $qrText, int $size): string 'color' => $this->decodeColor($this->color), 'format' => strtolower($this->format), 'data' => $qrText, - ); + ]; return 'https://api.qrserver.com/v1/create-qr-code/?' . http_build_query($queryParameters); } diff --git a/lib/Providers/Qr/QRicketProvider.php b/lib/Providers/Qr/QRicketProvider.php index d976b5f..7642139 100644 --- a/lib/Providers/Qr/QRicketProvider.php +++ b/lib/Providers/Qr/QRicketProvider.php @@ -9,8 +9,12 @@ */ class QRicketProvider extends BaseHTTPQRCodeProvider { - public function __construct(public string $errorcorrectionlevel = 'L', public string $bgcolor = 'ffffff', public string $color = '000000', public string $format = 'p') - { + public function __construct( + public string $errorcorrectionlevel = 'L', + public string $bgcolor = 'ffffff', + public string $color = '000000', + public string $format = 'p' + ) { $this->verifyssl = false; } @@ -34,14 +38,14 @@ public function getQRCodeImage(string $qrText, int $size): string public function getUrl(string $qrText, int $size): string { - $queryParameters = array( + $queryParameters = [ 'qrsize' => $size, 'e' => strtolower($this->errorcorrectionlevel), 'bgdcolor' => $this->bgcolor, 'fgdcolor' => $this->color, 't' => strtolower($this->format), 'd' => $qrText, - ); + ]; return 'http://qrickit.com/api/qr?' . http_build_query($queryParameters); } diff --git a/lib/Providers/Rng/CSRNGProvider.php b/lib/Providers/Rng/CSRNGProvider.php index 97351f3..108e969 100644 --- a/lib/Providers/Rng/CSRNGProvider.php +++ b/lib/Providers/Rng/CSRNGProvider.php @@ -6,17 +6,11 @@ class CSRNGProvider implements IRNGProvider { - /** - * {@inheritdoc} - */ public function getRandomBytes(int $bytecount): string { return random_bytes($bytecount); // PHP7+ } - /** - * {@inheritdoc} - */ public function isCryptographicallySecure(): bool { return true; diff --git a/lib/Providers/Rng/HashRNGProvider.php b/lib/Providers/Rng/HashRNGProvider.php index 20241da..7cd9501 100644 --- a/lib/Providers/Rng/HashRNGProvider.php +++ b/lib/Providers/Rng/HashRNGProvider.php @@ -8,17 +8,15 @@ class HashRNGProvider implements IRNGProvider { - public function __construct(private readonly string $algorithm = 'sha256') - { + public function __construct( + private readonly string $algorithm = 'sha256' + ) { $algos = array_values(hash_algos()); - if (!in_array($this->algorithm, $algos, true)) { + if (! in_array($this->algorithm, $algos, true)) { throw new RNGException('Unsupported algorithm specified'); } } - /** - * {@inheritdoc} - */ public function getRandomBytes(int $bytecount): string { $result = ''; @@ -30,9 +28,6 @@ public function getRandomBytes(int $bytecount): string return $result; } - /** - * {@inheritdoc} - */ public function isCryptographicallySecure(): bool { return false; diff --git a/lib/Providers/Rng/OpenSSLRNGProvider.php b/lib/Providers/Rng/OpenSSLRNGProvider.php index 4063847..30ee75b 100644 --- a/lib/Providers/Rng/OpenSSLRNGProvider.php +++ b/lib/Providers/Rng/OpenSSLRNGProvider.php @@ -6,22 +6,17 @@ class OpenSSLRNGProvider implements IRNGProvider { - public function __construct(private readonly bool $requirestrong = true) - { + public function __construct( + private readonly bool $requirestrong = true + ) { } - /** - * {@inheritdoc} - */ public function getRandomBytes(int $bytecount): string { // will throw an Exception on failure return openssl_random_pseudo_bytes($bytecount, $crypto_strong); } - /** - * {@inheritdoc} - */ public function isCryptographicallySecure(): bool { return $this->requirestrong; diff --git a/lib/Providers/Time/HttpTimeProvider.php b/lib/Providers/Time/HttpTimeProvider.php index 55c20f6..e326b9f 100644 --- a/lib/Providers/Time/HttpTimeProvider.php +++ b/lib/Providers/Time/HttpTimeProvider.php @@ -21,26 +21,23 @@ public function __construct( public ?array $options = null, ) { if ($this->options === null) { - $this->options = array( - 'http' => array( + $this->options = [ + 'http' => [ 'method' => 'HEAD', 'follow_location' => false, 'ignore_errors' => true, 'max_redirects' => 0, 'request_fulluri' => true, - 'header' => array( + 'header' => [ 'Connection: close', 'User-agent: TwoFactorAuth HttpTimeProvider (https://github.com/RobThree/TwoFactorAuth)', 'Cache-Control: no-cache', - ), - ), - ); + ], + ], + ]; } } - /** - * {@inheritdoc} - */ public function getTime() { try { diff --git a/lib/Providers/Time/NTPTimeProvider.php b/lib/Providers/Time/NTPTimeProvider.php index f1db3a3..9c4df3b 100644 --- a/lib/Providers/Time/NTPTimeProvider.php +++ b/lib/Providers/Time/NTPTimeProvider.php @@ -13,8 +13,11 @@ */ class NTPTimeProvider implements ITimeProvider { - public function __construct(public string $host = 'time.google.com', public int $port = 123, public int $timeout = 1) - { + public function __construct( + public string $host = 'time.google.com', + public int $port = 123, + public int $timeout = 1 + ) { if ($this->port <= 0 || $this->port > 65535) { throw new TimeException('Port must be 0 < port < 65535'); } @@ -24,15 +27,15 @@ public function __construct(public string $host = 'time.google.com', public int } } - /** - * {@inheritdoc} - */ public function getTime() { try { // Create a socket and connect to NTP server $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0)); + socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, [ + 'sec' => $this->timeout, + 'usec' => 0, + ]); socket_connect($sock, $this->host, $this->port); // Send request @@ -47,7 +50,7 @@ public function getTime() // Interpret response $data = unpack('N12', $recv); - $timestamp = (int)sprintf('%u', $data[9]); + $timestamp = (int) sprintf('%u', $data[9]); // NTP is number of seconds since 0000 UT on 1 January 1900 Unix time is seconds since 0000 UT on 1 January 1970 return $timestamp - 2208988800; diff --git a/lib/TwoFactorAuth.php b/lib/TwoFactorAuth.php index f3f867e..5b913a7 100644 --- a/lib/TwoFactorAuth.php +++ b/lib/TwoFactorAuth.php @@ -21,20 +21,24 @@ class TwoFactorAuth { private static string $_base32dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567='; - /** @var array */ + /** + * @var array + */ private static array $_base32; - /** @var array */ - private static array $_base32lookup = array(); + /** + * @var array + */ + private static array $_base32lookup = []; public function __construct( - private readonly ?string $issuer = null, - private readonly int $digits = 6, - private readonly int $period = 30, + private readonly ?string $issuer = null, + private readonly int $digits = 6, + private readonly int $period = 30, private readonly Algorithm $algorithm = Algorithm::Sha1, - private ?IQRCodeProvider $qrcodeprovider = null, - private ?IRNGProvider $rngprovider = null, - private ?ITimeProvider $timeprovider = null + private ?IQRCodeProvider $qrcodeprovider = null, + private ?IRNGProvider $rngprovider = null, + private ?ITimeProvider $timeprovider = null ) { if ($this->digits <= 0) { throw new TwoFactorAuthException('Digits must be > 0'); @@ -54,9 +58,9 @@ public function __construct( public function createSecret(int $bits = 80, bool $requirecryptosecure = true): string { $secret = ''; - $bytes = (int)ceil($bits / 5); // We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) + $bytes = (int) ceil($bits / 5); // We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32) $rngprovider = $this->getRngProvider(); - if ($requirecryptosecure && !$rngprovider->isCryptographicallySecure()) { + if ($requirecryptosecure && ! $rngprovider->isCryptographicallySecure()) { throw new TwoFactorAuthException('RNG provider is not cryptographically secure'); } $rnd = $rngprovider->getRandomBytes($bytes); @@ -79,7 +83,7 @@ public function getCode(string $secret, ?int $time = null): string $value = unpack('N', $hashpart); // Unpack binary value $value = $value[1] & 0x7FFFFFFF; // Drop MSB, keep only 31 bits - return str_pad((string)($value % 10 ** $this->digits), $this->digits, '0', STR_PAD_LEFT); + return str_pad((string) ($value % 10 ** $this->digits), $this->digits, '0', STR_PAD_LEFT); } /** @@ -128,10 +132,10 @@ public function getQRCodeImageAsDataUri(string $label, string $secret, int $size public function ensureCorrectTime(?array $timeproviders = null, int $leniency = 5): void { if ($timeproviders === null) { - $timeproviders = array( + $timeproviders = [ new NTPTimeProvider(), new HttpTimeProvider(), - ); + ]; } // Get default time provider @@ -139,7 +143,7 @@ public function ensureCorrectTime(?array $timeproviders = null, int $leniency = // Iterate specified time providers foreach ($timeproviders as $t) { - if (!($t instanceof ITimeProvider)) { + if (! ($t instanceof ITimeProvider)) { throw new TwoFactorAuthException('Object does not implement ITimeProvider'); } @@ -157,7 +161,7 @@ public function getQRText(string $label, string $secret): string { return 'otpauth://totp/' . rawurlencode($label) . '?secret=' . rawurlencode($secret) - . '&issuer=' . rawurlencode((string)$this->issuer) + . '&issuer=' . rawurlencode((string) $this->issuer) . '&period=' . $this->period . '&algorithm=' . rawurlencode(strtoupper($this->algorithm->value)) . '&digits=' . $this->digits; @@ -223,7 +227,7 @@ private function getTime(?int $time = null): int private function getTimeSlice(?int $time = null, int $offset = 0): int { - return (int)floor($time / $this->period) + ($offset * $this->period); + return (int) floor($time / $this->period) + ($offset * $this->period); } private function base32Decode(string $value): string diff --git a/phpstan.neon b/phpstan.neon index 50bfcb6..831b3d1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,11 @@ parameters: level: 6 excludePaths: - - %currentWorkingDirectory%/lib/Providers/Qr/BaconQrCodeProvider.php - - %currentWorkingDirectory%/lib/Providers/Qr/EndroidQrCodeProvider.php - - %currentWorkingDirectory%/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php + - lib/Providers/Qr/BaconQrCodeProvider.php + - lib/Providers/Qr/EndroidQrCodeProvider.php + - lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php paths: - - %currentWorkingDirectory%/lib - - %currentWorkingDirectory%/tests - + - lib + - tests + - testsDependency diff --git a/tests/Providers/Rng/HashRNGProviderTest.php b/tests/Providers/Rng/HashRNGProviderTest.php index 4a71930..e316c40 100644 --- a/tests/Providers/Rng/HashRNGProviderTest.php +++ b/tests/Providers/Rng/HashRNGProviderTest.php @@ -11,10 +11,7 @@ class HashRNGProviderTest extends TestCase { use NeedsRngLengths; - /** - * @return void - */ - public function testHashRNGProvidersReturnExpectedNumberOfBytes() + public function testHashRNGProvidersReturnExpectedNumberOfBytes(): void { $rng = new HashRNGProvider(); foreach ($this->rngTestLengths as $l) { diff --git a/tests/Providers/Rng/NeedsRngLengths.php b/tests/Providers/Rng/NeedsRngLengths.php index 9c34c06..3d00eae 100644 --- a/tests/Providers/Rng/NeedsRngLengths.php +++ b/tests/Providers/Rng/NeedsRngLengths.php @@ -6,6 +6,8 @@ trait NeedsRngLengths { - /** @var array */ - protected $rngTestLengths = array(1, 16, 32, 256); + /** + * @var array + */ + protected array $rngTestLengths = [1, 16, 32, 256]; } diff --git a/tests/Providers/Rng/OpenSSLRNGProviderTest.php b/tests/Providers/Rng/OpenSSLRNGProviderTest.php index 368c7cc..97f658a 100644 --- a/tests/Providers/Rng/OpenSSLRNGProviderTest.php +++ b/tests/Providers/Rng/OpenSSLRNGProviderTest.php @@ -11,10 +11,7 @@ class OpenSSLRNGProviderTest extends TestCase { use NeedsRngLengths; - /** - * @return void - */ - public function testStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes() + public function testStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes(): void { $rng = new OpenSSLRNGProvider(true); foreach ($this->rngTestLengths as $l) { @@ -24,10 +21,7 @@ public function testStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes() $this->assertTrue($rng->isCryptographicallySecure()); } - /** - * @return void - */ - public function testNonStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes() + public function testNonStrongOpenSSLRNGProvidersReturnExpectedNumberOfBytes(): void { $rng = new OpenSSLRNGProvider(false); foreach ($this->rngTestLengths as $l) { diff --git a/tests/Providers/Rng/TestRNGProvider.php b/tests/Providers/Rng/TestRNGProvider.php index c166c66..4475ebc 100644 --- a/tests/Providers/Rng/TestRNGProvider.php +++ b/tests/Providers/Rng/TestRNGProvider.php @@ -8,13 +8,11 @@ class TestRNGProvider implements IRNGProvider { - public function __construct(private readonly bool $isSecure = false) - { + public function __construct( + private readonly bool $isSecure = false + ) { } - /** - * {@inheritdoc} - */ public function getRandomBytes(int $bytecount): string { $result = ''; @@ -26,9 +24,6 @@ public function getRandomBytes(int $bytecount): string return $result; } - /** - * {@inheritdoc} - */ public function isCryptographicallySecure(): bool { return $this->isSecure; diff --git a/tests/Providers/Time/ITimeProviderTest.php b/tests/Providers/Time/ITimeProviderTest.php index f67ac85..5b1a31b 100644 --- a/tests/Providers/Time/ITimeProviderTest.php +++ b/tests/Providers/Time/ITimeProviderTest.php @@ -18,7 +18,7 @@ public function testEnsureCorrectTimeDoesNotThrowForCorrectTime(): void $tpr2 = new TestTimeProvider(128); $tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1, null, null, $tpr1); - $tfa->ensureCorrectTime(array($tpr2)); // 128 - 123 = 5 => within default leniency + $tfa->ensureCorrectTime([$tpr2]); // 128 - 123 = 5 => within default leniency } public function testEnsureCorrectTimeThrowsOnIncorrectTime(): void @@ -30,13 +30,13 @@ public function testEnsureCorrectTimeThrowsOnIncorrectTime(): void $this->expectException(TwoFactorAuthException::class); - $tfa->ensureCorrectTime(array($tpr2), 0); // We force a leniency of 0, 124-123 = 1 so this should throw + $tfa->ensureCorrectTime([$tpr2], 0); // We force a leniency of 0, 124-123 = 1 so this should throw } public function testEnsureDefaultTimeProviderReturnsCorrectTime(): void { $this->expectNotToPerformAssertions(); $tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1); - $tfa->ensureCorrectTime(array(new TestTimeProvider(time())), 1); // Use a leniency of 1, should the time change between both time() calls + $tfa->ensureCorrectTime([new TestTimeProvider(time())], 1); // Use a leniency of 1, should the time change between both time() calls } } diff --git a/tests/Providers/Time/TestTimeProvider.php b/tests/Providers/Time/TestTimeProvider.php index 226c2ee..72cf395 100644 --- a/tests/Providers/Time/TestTimeProvider.php +++ b/tests/Providers/Time/TestTimeProvider.php @@ -8,21 +8,13 @@ class TestTimeProvider implements ITimeProvider { - /** @var int */ - private $time; - - /** - * @param int $time - */ - public function __construct($time) - { + public function __construct( + private int $time + ) { $this->time = $time; } - /** - * {@inheritdoc} - */ - public function getTime() + public function getTime(): int { return $this->time; } diff --git a/tests/TwoFactorAuthTest.php b/tests/TwoFactorAuthTest.php index 0fa9f11..184d43a 100644 --- a/tests/TwoFactorAuthTest.php +++ b/tests/TwoFactorAuthTest.php @@ -38,13 +38,13 @@ public function testGetCodeReturnsCorrectResults(): void public function testEnsureAllTimeProvidersReturnCorrectTime(): void { $tfa = new TwoFactorAuth('Test', 6, 30, Algorithm::Sha1); - $tfa->ensureCorrectTime(array( + $tfa->ensureCorrectTime([ new NTPTimeProvider(), // Uses pool.ntp.org by default //new \RobThree\Auth\Providers\Time\NTPTimeProvider('time.google.com'), // Somehow time.google.com and time.windows.com make travis timeout?? new HttpTimeProvider(), // Uses google.com by default //new \RobThree\Auth\Providers\Time\HttpTimeProvider('https://github.com'), // github.com will periodically report times that are off by more than 5 sec new HttpTimeProvider('https://yahoo.com'), - )); + ]); $this->expectNotToPerformAssertions(); } @@ -158,7 +158,7 @@ public function testKnownBase32DecodeUnpaddedTestVectors(): void $this->assertSame('foobar', $method->invoke($tfa, 'MZXW6YTBOI')); } - public function testKnownTestVectors_sha1(): void + public function testKnownTestVectorsSha1(): void { //Known test vectors for SHA1: https://tools.ietf.org/html/rfc6238#page-15 $secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'; //== base32encode('12345678901234567890') @@ -171,7 +171,7 @@ public function testKnownTestVectors_sha1(): void $this->assertSame('65353130', $tfa->getCode($secret, 20000000000)); } - public function testKnownTestVectors_sha256(): void + public function testKnownTestVectorsSha256(): void { //Known test vectors for SHA256: https://tools.ietf.org/html/rfc6238#page-15 $secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA'; //== base32encode('12345678901234567890123456789012') @@ -184,7 +184,7 @@ public function testKnownTestVectors_sha256(): void $this->assertSame('77737706', $tfa->getCode($secret, 20000000000)); } - public function testKnownTestVectors_sha512(): void + public function testKnownTestVectorsSha512(): void { //Known test vectors for SHA512: https://tools.ietf.org/html/rfc6238#page-15 $secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA'; //== base32encode('1234567890123456789012345678901234567890123456789012345678901234')