Skip to content

Commit

Permalink
"Replace mb_* functions with equivalent non-multibyte functions" (#578)
Browse files Browse the repository at this point in the history
This commit replaces mb_* functions with non-multibyte equivalent functions, which don't require mbstring extension. It also adjusts the required PHP version in composer.json from 8.3 to 8.2. This change reduces dependencies and broadens compatibility without impacting functionality. It is part of an overall code simplification and optimization effort.
  • Loading branch information
Spomky authored Jul 9, 2024
1 parent 57c78f2 commit 4af252f
Show file tree
Hide file tree
Showing 64 changed files with 199 additions and 181 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
operating-system:
- "ubuntu-latest"
php-version:
- "8.2"
- "8.3"
dependencies:
- "lowest"
Expand Down Expand Up @@ -196,7 +197,7 @@ jobs:
- name: "Install dependencies"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"
dependency-versions: "lowest"
composer-options: "--optimize-autoloader"

- name: "Execute Rector"
Expand Down
5 changes: 4 additions & 1 deletion castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ function cs(
}

#[AsTask(description: 'Running PHPStan')]
function stan(): void
function stan(bool $baseline = false): void
{
io()->title('Running PHPStan');
$command = ['php', 'vendor/bin/phpstan', 'analyse'];
if ($baseline) {
$command[] = '--generate-baseline';
}
$environment = [
'XDEBUG_MODE' => 'off',
];
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
}
},
"require": {
"php": ">=8.3",
"php": ">=8.2",
"ext-json": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"brick/math": "^0.12",
"psr/clock": "^1.0",
Expand Down
11 changes: 8 additions & 3 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,11 @@ parameters:
count: 1
path: src/Library/Core/JWKSet.php

-
message: "#^Parameter \\#2 \\$mode of function count expects 0\\|1, int given\\.$#"
count: 1
path: src/Library/Core/JWKSet.php

-
message: "#^Property Jose\\\\Component\\\\Core\\\\JWKSet\\:\\:\\$keys type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -1346,12 +1351,12 @@ parameters:
path: src/Library/Encryption/Algorithm/ContentEncryption/AESCBCHS.php

-
message: "#^Parameter \\#3 \\$length of function mb_substr expects int\\|null, float\\|int\\<1, max\\> given\\.$#"
message: "#^Parameter \\#3 \\$length of function substr expects int\\|null, float\\|int\\<1, max\\> given\\.$#"
count: 1
path: src/Library/Encryption/Algorithm/ContentEncryption/AESCBCHS.php

-
message: "#^Parameter \\#3 \\$length of function mb_substr expects int\\|null, float\\|int\\<min, \\-1\\> given\\.$#"
message: "#^Parameter \\#3 \\$length of function substr expects int\\|null, float\\|int\\<min, \\-1\\> given\\.$#"
count: 1
path: src/Library/Encryption/Algorithm/KeyEncryption/AbstractECDH.php

Expand Down Expand Up @@ -1761,7 +1766,7 @@ parameters:
path: src/Library/KeyManagement/JWKFactory.php

-
message: "#^Parameter \\#3 \\$length of function mb_substr expects int\\|null, float\\|int\\<min, \\-1\\> given\\.$#"
message: "#^Parameter \\#3 \\$length of function substr expects int\\|null, float\\|int\\<min, \\-1\\> given\\.$#"
count: 1
path: src/Library/KeyManagement/JWKFactory.php

Expand Down
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

return static function (RectorConfig $config): void {
$config->import(SetList::DEAD_CODE);
$config->import(LevelSetList::UP_TO_PHP_83);
$config->import(LevelSetList::UP_TO_PHP_82);
$config->import(SymfonySetList::SYMFONY_64);
$config->import(SymfonySetList::SYMFONY_50_TYPES);
$config->import(SymfonySetList::SYMFONY_52_VALIDATOR_ATTRIBUTES);
Expand All @@ -38,7 +38,7 @@
__DIR__ . '/src/Bundle/JoseFramework/DependencyInjection/Source/KeyManagement/JWKSource.php',
__DIR__ . '/src/Bundle/JoseFramework/DependencyInjection/Source/KeyManagement/JWKSetSource.php',
]);
$config->phpVersion(PhpVersion::PHP_83);
$config->phpVersion(PhpVersion::PHP_82);
$config->parallel();
$config->importNames();
$config->importShortClasses();
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Helper/ConfigurationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final readonly class ConfigurationHelper
{
final public const string BUNDLE_ALIAS = 'jose';
final public const BUNDLE_ALIAS = 'jose';

/**
* @param string[] $signatureAlgorithms
Expand Down
5 changes: 2 additions & 3 deletions src/Bundle/Serializer/JWEEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Throwable;
use function in_array;
use function is_int;
use function mb_strtolower;

final readonly class JWEEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
{
Expand Down Expand Up @@ -54,7 +53,7 @@ public function encode(mixed $data, string $format, array $context = []): string

try {
return $this->serializerManager->serialize(
mb_strtolower($format),
strtolower($format),
$data,
$this->getRecipientIndex($context)
);
Expand Down Expand Up @@ -91,6 +90,6 @@ private function getRecipientIndex(array $context): int
private function formatSupported(?string $format): bool
{
return $format !== null
&& in_array(mb_strtolower($format), $this->serializerManager->names(), true);
&& in_array(strtolower($format), $this->serializerManager->names(), true);
}
}
5 changes: 2 additions & 3 deletions src/Bundle/Serializer/JWESerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
use Override;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use function in_array;
use function mb_strtolower;

final readonly class JWESerializer implements DenormalizerInterface
{
private readonly JWESerializerManager $serializerManager;
private JWESerializerManager $serializerManager;

public function __construct(
JWESerializerManagerFactory $serializerManagerFactory,
Expand Down Expand Up @@ -63,6 +62,6 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
private function formatSupported(?string $format): bool
{
return $format !== null
&& in_array(mb_strtolower($format), $this->serializerManager->names(), true);
&& in_array(strtolower($format), $this->serializerManager->names(), true);
}
}
5 changes: 2 additions & 3 deletions src/Bundle/Serializer/JWSEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use function in_array;
use function is_int;
use function mb_strtolower;

final readonly class JWSEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
{
Expand Down Expand Up @@ -53,7 +52,7 @@ public function encode($data, $format, array $context = []): string

try {
return $this->serializerManager->serialize(
mb_strtolower($format),
strtolower($format),
$data,
$this->getSignatureIndex($context)
);
Expand Down Expand Up @@ -90,6 +89,6 @@ private function getSignatureIndex(array $context): int
private function formatSupported(?string $format): bool
{
return $format !== null
&& in_array(mb_strtolower($format), $this->serializerManager->list(), true);
&& in_array(strtolower($format), $this->serializerManager->list(), true);
}
}
3 changes: 1 addition & 2 deletions src/Bundle/Serializer/JWSSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Override;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use function in_array;
use function mb_strtolower;

final readonly class JWSSerializer implements DenormalizerInterface
{
Expand Down Expand Up @@ -63,6 +62,6 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
private function formatSupported(?string $format): bool
{
return $format !== null
&& in_array(mb_strtolower($format), $this->serializerManager->list(), true);
&& in_array(strtolower($format), $this->serializerManager->list(), true);
}
}
2 changes: 1 addition & 1 deletion src/Bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"require": {
"php": ">=8.3",
"php": ">=8.2",
"psr/event-dispatcher": "^1.0",
"symfony/config": "^7.0",
"symfony/console": "^7.0",
Expand Down
3 changes: 2 additions & 1 deletion src/Experimental/KeyEncryption/Chacha20Poly1305.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RuntimeException;
use function in_array;
use function is_string;
use function strlen;
use const OPENSSL_RAW_DATA;

final readonly class Chacha20Poly1305 implements KeyEncryption
Expand Down Expand Up @@ -68,7 +69,7 @@ public function decryptKey(JWK $key, string $encrypted_cek, array $header): stri
isset($header['nonce']) || throw new InvalidArgumentException('The header parameter "nonce" is missing.');
is_string($header['nonce']) || throw new InvalidArgumentException('The header parameter "nonce" is not valid.');
$nonce = Base64UrlSafe::decodeNoPadding($header['nonce']);
if (mb_strlen($nonce, '8bit') !== 12) {
if (strlen($nonce) !== 12) {
throw new InvalidArgumentException('The header parameter "nonce" is not valid.');
}

Expand Down
5 changes: 3 additions & 2 deletions src/Experimental/Signature/Blake2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
use function extension_loaded;
use function in_array;
use function is_string;
use function strlen;

/**
* @see \Jose\Tests\Component\Signature\Algorithm\Blake2bTest
*/
final readonly class Blake2b implements MacAlgorithm
{
private const int MINIMUM_KEY_LENGTH = 32;
private const MINIMUM_KEY_LENGTH = 32;

public function __construct()
{
Expand Down Expand Up @@ -67,7 +68,7 @@ private function getKey(JWK $key): string
throw new InvalidArgumentException('The key parameter "k" is invalid.');
}
$key = Base64UrlSafe::decodeNoPadding($k);
if (mb_strlen($key, '8bit') < self::MINIMUM_KEY_LENGTH) {
if (strlen($key) < self::MINIMUM_KEY_LENGTH) {
throw new InvalidArgumentException('Key provided is shorter than 256 bits.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Experimental/Signature/HS256_64.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function hash(JWK $key, string $input): string
{
$signature = parent::hash($key, $input);

return mb_substr($signature, 0, 8, '8bit');
return substr($signature, 0, 8);
}

#[Override]
Expand Down
2 changes: 1 addition & 1 deletion src/Experimental/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"require": {
"php": ">=8.3",
"php": ">=8.2",
"ext-openssl": "*",
"web-token/jwt-library": "^4.0"
}
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/AlgorithmChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
final readonly class AlgorithmChecker implements HeaderChecker
{
private const string HEADER_NAME = 'alg';
private const HEADER_NAME = 'alg';

/**
* @param string[] $supportedAlgorithms
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/AudienceChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final readonly class AudienceChecker implements ClaimChecker, HeaderChecker
{
private const string CLAIM_NAME = 'aud';
private const CLAIM_NAME = 'aud';

public function __construct(
private string $audience,
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/ExpirationTimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final readonly class ExpirationTimeChecker implements ClaimChecker, HeaderChecker
{
private const string NAME = 'exp';
private const NAME = 'exp';

public function __construct(
private ClockInterface $clock,
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/IssuedAtChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final readonly class IssuedAtChecker implements ClaimChecker, HeaderChecker
{
private const string NAME = 'iat';
private const NAME = 'iat';

public function __construct(
private ClockInterface $clock,
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/IssuerChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
final readonly class IssuerChecker implements ClaimChecker, HeaderChecker
{
private const string CLAIM_NAME = 'iss';
private const CLAIM_NAME = 'iss';

public function __construct(
private array $issuers,
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/NotBeforeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final readonly class NotBeforeChecker implements ClaimChecker, HeaderChecker
{
private const string NAME = 'nbf';
private const NAME = 'nbf';

public function __construct(
private ClockInterface $clock,
Expand Down
2 changes: 1 addition & 1 deletion src/Library/Checker/UnencodedPayloadChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final class UnencodedPayloadChecker implements HeaderChecker
{
private const string HEADER_NAME = 'b64';
private const HEADER_NAME = 'b64';

#[Override]
public function checkHeader(mixed $value): void
Expand Down
6 changes: 4 additions & 2 deletions src/Library/Core/Util/Base64UrlSafe.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Jose\Component\Core\Util;

use function strlen;

/**
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
Expand Down Expand Up @@ -202,14 +204,14 @@ private static function encode6Bits(int $src): string

private static function safeStrlen(string $str): int
{
return mb_strlen($str, '8bit');
return strlen($str);
}

private static function safeSubstr(string $str, int $start = 0, $length = null): string
{
if ($length === 0) {
return '';
}
return mb_substr($str, $start, $length, '8bit');
return substr($str, $start, $length);
}
}
3 changes: 2 additions & 1 deletion src/Library/Core/Util/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Brick\Math\BigInteger as BrickBigInteger;
use InvalidArgumentException;
use function chr;
use function strlen;

/**
* @internal
Expand Down Expand Up @@ -49,7 +50,7 @@ public function toBytes(): string
}

$temp = $this->value->toBase(16);
$temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0' . $temp : $temp;
$temp = 0 !== (strlen($temp) & 1) ? '0' . $temp : $temp;
$temp = hex2bin($temp);
if ($temp === false) {
throw new InvalidArgumentException('Unable to convert the value into bytes');
Expand Down
Loading

0 comments on commit 4af252f

Please sign in to comment.