Skip to content

Commit 4af252f

Browse files
authored
"Replace mb_* functions with equivalent non-multibyte functions" (#578)
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.
1 parent 57c78f2 commit 4af252f

File tree

64 files changed

+199
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+199
-181
lines changed

.github/workflows/integrate.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
operating-system:
6969
- "ubuntu-latest"
7070
php-version:
71+
- "8.2"
7172
- "8.3"
7273
dependencies:
7374
- "lowest"
@@ -196,7 +197,7 @@ jobs:
196197
- name: "Install dependencies"
197198
uses: "ramsey/composer-install@v3"
198199
with:
199-
dependency-versions: "highest"
200+
dependency-versions: "lowest"
200201
composer-options: "--optimize-autoloader"
201202

202203
- name: "Execute Rector"

castor.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ function cs(
7777
}
7878

7979
#[AsTask(description: 'Running PHPStan')]
80-
function stan(): void
80+
function stan(bool $baseline = false): void
8181
{
8282
io()->title('Running PHPStan');
8383
$command = ['php', 'vendor/bin/phpstan', 'analyse'];
84+
if ($baseline) {
85+
$command[] = '--generate-baseline';
86+
}
8487
$environment = [
8588
'XDEBUG_MODE' => 'off',
8689
];

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646
}
4747
},
4848
"require": {
49-
"php": ">=8.3",
49+
"php": ">=8.2",
5050
"ext-json": "*",
51-
"ext-mbstring": "*",
5251
"ext-openssl": "*",
5352
"brick/math": "^0.12",
5453
"psr/clock": "^1.0",

phpstan-baseline.neon

+8-3
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,11 @@ parameters:
12251225
count: 1
12261226
path: src/Library/Core/JWKSet.php
12271227

1228+
-
1229+
message: "#^Parameter \\#2 \\$mode of function count expects 0\\|1, int given\\.$#"
1230+
count: 1
1231+
path: src/Library/Core/JWKSet.php
1232+
12281233
-
12291234
message: "#^Property Jose\\\\Component\\\\Core\\\\JWKSet\\:\\:\\$keys type has no value type specified in iterable type array\\.$#"
12301235
count: 1
@@ -1346,12 +1351,12 @@ parameters:
13461351
path: src/Library/Encryption/Algorithm/ContentEncryption/AESCBCHS.php
13471352

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

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

@@ -1761,7 +1766,7 @@ parameters:
17611766
path: src/Library/KeyManagement/JWKFactory.php
17621767

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

rector.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
return static function (RectorConfig $config): void {
1515
$config->import(SetList::DEAD_CODE);
16-
$config->import(LevelSetList::UP_TO_PHP_83);
16+
$config->import(LevelSetList::UP_TO_PHP_82);
1717
$config->import(SymfonySetList::SYMFONY_64);
1818
$config->import(SymfonySetList::SYMFONY_50_TYPES);
1919
$config->import(SymfonySetList::SYMFONY_52_VALIDATOR_ATTRIBUTES);
@@ -38,7 +38,7 @@
3838
__DIR__ . '/src/Bundle/JoseFramework/DependencyInjection/Source/KeyManagement/JWKSource.php',
3939
__DIR__ . '/src/Bundle/JoseFramework/DependencyInjection/Source/KeyManagement/JWKSetSource.php',
4040
]);
41-
$config->phpVersion(PhpVersion::PHP_83);
41+
$config->phpVersion(PhpVersion::PHP_82);
4242
$config->parallel();
4343
$config->importNames();
4444
$config->importShortClasses();

src/Bundle/Helper/ConfigurationHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final readonly class ConfigurationHelper
1111
{
12-
final public const string BUNDLE_ALIAS = 'jose';
12+
final public const BUNDLE_ALIAS = 'jose';
1313

1414
/**
1515
* @param string[] $signatureAlgorithms

src/Bundle/Serializer/JWEEncoder.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Throwable;
1818
use function in_array;
1919
use function is_int;
20-
use function mb_strtolower;
2120

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

5554
try {
5655
return $this->serializerManager->serialize(
57-
mb_strtolower($format),
56+
strtolower($format),
5857
$data,
5958
$this->getRecipientIndex($context)
6059
);
@@ -91,6 +90,6 @@ private function getRecipientIndex(array $context): int
9190
private function formatSupported(?string $format): bool
9291
{
9392
return $format !== null
94-
&& in_array(mb_strtolower($format), $this->serializerManager->names(), true);
93+
&& in_array(strtolower($format), $this->serializerManager->names(), true);
9594
}
9695
}

src/Bundle/Serializer/JWESerializer.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
use Override;
1212
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1313
use function in_array;
14-
use function mb_strtolower;
1514

1615
final readonly class JWESerializer implements DenormalizerInterface
1716
{
18-
private readonly JWESerializerManager $serializerManager;
17+
private JWESerializerManager $serializerManager;
1918

2019
public function __construct(
2120
JWESerializerManagerFactory $serializerManagerFactory,
@@ -63,6 +62,6 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
6362
private function formatSupported(?string $format): bool
6463
{
6564
return $format !== null
66-
&& in_array(mb_strtolower($format), $this->serializerManager->names(), true);
65+
&& in_array(strtolower($format), $this->serializerManager->names(), true);
6766
}
6867
}

src/Bundle/Serializer/JWSEncoder.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1717
use function in_array;
1818
use function is_int;
19-
use function mb_strtolower;
2019

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

5453
try {
5554
return $this->serializerManager->serialize(
56-
mb_strtolower($format),
55+
strtolower($format),
5756
$data,
5857
$this->getSignatureIndex($context)
5958
);
@@ -90,6 +89,6 @@ private function getSignatureIndex(array $context): int
9089
private function formatSupported(?string $format): bool
9190
{
9291
return $format !== null
93-
&& in_array(mb_strtolower($format), $this->serializerManager->list(), true);
92+
&& in_array(strtolower($format), $this->serializerManager->list(), true);
9493
}
9594
}

src/Bundle/Serializer/JWSSerializer.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Override;
1212
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1313
use function in_array;
14-
use function mb_strtolower;
1514

1615
final readonly class JWSSerializer implements DenormalizerInterface
1716
{
@@ -63,6 +62,6 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
6362
private function formatSupported(?string $format): bool
6463
{
6564
return $format !== null
66-
&& in_array(mb_strtolower($format), $this->serializerManager->list(), true);
65+
&& in_array(strtolower($format), $this->serializerManager->list(), true);
6766
}
6867
}

src/Bundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
}
3939
},
4040
"require": {
41-
"php": ">=8.3",
41+
"php": ">=8.2",
4242
"psr/event-dispatcher": "^1.0",
4343
"symfony/config": "^7.0",
4444
"symfony/console": "^7.0",

src/Experimental/KeyEncryption/Chacha20Poly1305.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use RuntimeException;
1414
use function in_array;
1515
use function is_string;
16+
use function strlen;
1617
use const OPENSSL_RAW_DATA;
1718

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

src/Experimental/Signature/Blake2b.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
use function extension_loaded;
1414
use function in_array;
1515
use function is_string;
16+
use function strlen;
1617

1718
/**
1819
* @see \Jose\Tests\Component\Signature\Algorithm\Blake2bTest
1920
*/
2021
final readonly class Blake2b implements MacAlgorithm
2122
{
22-
private const int MINIMUM_KEY_LENGTH = 32;
23+
private const MINIMUM_KEY_LENGTH = 32;
2324

2425
public function __construct()
2526
{
@@ -67,7 +68,7 @@ private function getKey(JWK $key): string
6768
throw new InvalidArgumentException('The key parameter "k" is invalid.');
6869
}
6970
$key = Base64UrlSafe::decodeNoPadding($k);
70-
if (mb_strlen($key, '8bit') < self::MINIMUM_KEY_LENGTH) {
71+
if (strlen($key) < self::MINIMUM_KEY_LENGTH) {
7172
throw new InvalidArgumentException('Key provided is shorter than 256 bits.');
7273
}
7374

src/Experimental/Signature/HS256_64.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function hash(JWK $key, string $input): string
1515
{
1616
$signature = parent::hash($key, $input);
1717

18-
return mb_substr($signature, 0, 8, '8bit');
18+
return substr($signature, 0, 8);
1919
}
2020

2121
#[Override]

src/Experimental/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
}
3939
},
4040
"require": {
41-
"php": ">=8.3",
41+
"php": ">=8.2",
4242
"ext-openssl": "*",
4343
"web-token/jwt-library": "^4.0"
4444
}

src/Library/Checker/AlgorithmChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
final readonly class AlgorithmChecker implements HeaderChecker
1717
{
18-
private const string HEADER_NAME = 'alg';
18+
private const HEADER_NAME = 'alg';
1919

2020
/**
2121
* @param string[] $supportedAlgorithms

src/Library/Checker/AudienceChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
final readonly class AudienceChecker implements ClaimChecker, HeaderChecker
1616
{
17-
private const string CLAIM_NAME = 'aud';
17+
private const CLAIM_NAME = 'aud';
1818

1919
public function __construct(
2020
private string $audience,

src/Library/Checker/ExpirationTimeChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
final readonly class ExpirationTimeChecker implements ClaimChecker, HeaderChecker
1818
{
19-
private const string NAME = 'exp';
19+
private const NAME = 'exp';
2020

2121
public function __construct(
2222
private ClockInterface $clock,

src/Library/Checker/IssuedAtChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
final readonly class IssuedAtChecker implements ClaimChecker, HeaderChecker
1616
{
17-
private const string NAME = 'iat';
17+
private const NAME = 'iat';
1818

1919
public function __construct(
2020
private ClockInterface $clock,

src/Library/Checker/IssuerChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
final readonly class IssuerChecker implements ClaimChecker, HeaderChecker
1717
{
18-
private const string CLAIM_NAME = 'iss';
18+
private const CLAIM_NAME = 'iss';
1919

2020
public function __construct(
2121
private array $issuers,

src/Library/Checker/NotBeforeChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
final readonly class NotBeforeChecker implements ClaimChecker, HeaderChecker
1616
{
17-
private const string NAME = 'nbf';
17+
private const NAME = 'nbf';
1818

1919
public function __construct(
2020
private ClockInterface $clock,

src/Library/Checker/UnencodedPayloadChecker.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
final class UnencodedPayloadChecker implements HeaderChecker
1616
{
17-
private const string HEADER_NAME = 'b64';
17+
private const HEADER_NAME = 'b64';
1818

1919
#[Override]
2020
public function checkHeader(mixed $value): void

src/Library/Core/Util/Base64UrlSafe.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Jose\Component\Core\Util;
66

7+
use function strlen;
8+
79
/**
810
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
911
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
@@ -202,14 +204,14 @@ private static function encode6Bits(int $src): string
202204

203205
private static function safeStrlen(string $str): int
204206
{
205-
return mb_strlen($str, '8bit');
207+
return strlen($str);
206208
}
207209

208210
private static function safeSubstr(string $str, int $start = 0, $length = null): string
209211
{
210212
if ($length === 0) {
211213
return '';
212214
}
213-
return mb_substr($str, $start, $length, '8bit');
215+
return substr($str, $start, $length);
214216
}
215217
}

src/Library/Core/Util/BigInteger.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Brick\Math\BigInteger as BrickBigInteger;
88
use InvalidArgumentException;
99
use function chr;
10+
use function strlen;
1011

1112
/**
1213
* @internal
@@ -49,7 +50,7 @@ public function toBytes(): string
4950
}
5051

5152
$temp = $this->value->toBase(16);
52-
$temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0' . $temp : $temp;
53+
$temp = 0 !== (strlen($temp) & 1) ? '0' . $temp : $temp;
5354
$temp = hex2bin($temp);
5455
if ($temp === false) {
5556
throw new InvalidArgumentException('Unable to convert the value into bytes');

0 commit comments

Comments
 (0)