Skip to content

Commit e4b057e

Browse files
authored
refactor: fix phpstan errors in Encryption (#10486)
1 parent 0a7b317 commit e4b057e

12 files changed

Lines changed: 39 additions & 121 deletions

system/Encryption/EncrypterInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ interface EncrypterInterface
2626
/**
2727
* Encrypt - convert plaintext into ciphertext
2828
*
29-
* @param string $data Input data
30-
* @param array|string|null $params Overridden parameters, specifically the key
29+
* @param string $data Input data
30+
* @param array<string, mixed>|string|null $params Overridden parameters, specifically the key
3131
*
3232
* @return string
3333
*
@@ -38,8 +38,8 @@ public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $para
3838
/**
3939
* Decrypt - convert ciphertext into plaintext
4040
*
41-
* @param string $data Encrypted data
42-
* @param array|string|null $params Overridden parameters, specifically the key
41+
* @param string $data Encrypted data
42+
* @param array<string, mixed>|string|null $params Overridden parameters, specifically the key
4343
*
4444
* @return string
4545
*

system/Encryption/Encryption.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Encryption
7070
/**
7171
* Map of drivers to handler classes, in preference order
7272
*
73-
* @var array
73+
* @var list<string>
7474
*/
7575
protected $drivers = [
7676
'OpenSSL',
@@ -138,8 +138,14 @@ public function initialize(?EncryptionConfig $config = null)
138138
$handlerName = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler';
139139
$this->encrypter = new $handlerName($config);
140140

141-
if (($config->previousKeys ?? []) !== []) {
142-
$this->encrypter = new KeyRotationDecorator($this->encrypter, $config->previousKeys);
141+
// (array) '' is [''], not [], so the unset default must be filtered out here.
142+
$previousKeys = array_values(array_filter(
143+
(array) ($config->previousKeys ?? []),
144+
static fn ($key): bool => $key !== '',
145+
));
146+
147+
if ($previousKeys !== []) {
148+
$this->encrypter = new KeyRotationDecorator($this->encrypter, $previousKeys);
143149
}
144150

145151
return $this->encrypter;
@@ -162,7 +168,7 @@ public static function createKey($length = 32)
162168
*
163169
* @param string $key Property name
164170
*
165-
* @return array|string|null
171+
* @return list<string>|string|null
166172
*/
167173
public function __get($key)
168174
{

system/Encryption/Handlers/OpenSSLHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
/**
2020
* Encryption handling for OpenSSL library
2121
*
22+
* @property-read string $cipher
23+
* @property-read string $key
24+
*
2225
* @see \CodeIgniter\Encryption\Handlers\OpenSSLHandlerTest
2326
*/
2427
class OpenSSLHandler extends BaseHandler
@@ -33,7 +36,7 @@ class OpenSSLHandler extends BaseHandler
3336
/**
3437
* List of supported HMAC algorithms
3538
*
36-
* @var array [name => digest size]
39+
* @var array<string, int> [name => digest size]
3740
*/
3841
protected array $digestSize = [
3942
'SHA224' => 28,

system/Encryption/Handlers/SodiumHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
/**
2121
* SodiumHandler uses libsodium in encryption.
2222
*
23+
* @property-read int $blockSize
24+
* @property-read string|null $key
25+
*
2326
* @see https://github.com/jedisct1/libsodium/issues/392
2427
* @see \CodeIgniter\Encryption\Handlers\SodiumHandlerTest
2528
*/
@@ -124,7 +127,7 @@ public function decrypt($data, #[SensitiveParameter] $params = null)
124127
/**
125128
* Parse the $params before doing assignment.
126129
*
127-
* @param array|string|null $params
130+
* @param array<string, mixed>|string|null $params
128131
*
129132
* @return void
130133
*

system/Encryption/KeyRotationDecorator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* Wraps any EncrypterInterface implementation to provide automatic
2323
* fallback to previous encryption keys during decryption. This enables
2424
* seamless key rotation without requiring re-encryption of existing data.
25+
*
26+
* @property-read string|null $cipher
27+
* @property-read string|null $key
2528
*/
2629
class KeyRotationDecorator implements EncrypterInterface
2730
{

tests/system/Encryption/EncryptionTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Config\Services as CodeIgniterServices;
1717
use CodeIgniter\Encryption\Exceptions\EncryptionException;
18+
use CodeIgniter\Encryption\Handlers\OpenSSLHandler;
1819
use CodeIgniter\Superglobals;
1920
use CodeIgniter\Test\CIUnitTestCase;
2021
use Config\Encryption as EncryptionConfig;
@@ -156,6 +157,7 @@ public function testServiceShared(): void
156157

157158
$config->key = 'Abracadabra';
158159
$encrypter = Services::encrypter($config, true);
160+
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
159161
$this->assertSame('anything', $encrypter->key);
160162
}
161163

@@ -166,7 +168,7 @@ public function testMagicIssetTrue(): void
166168

167169
public function testMagicIssetFalse(): void
168170
{
169-
$this->assertFalse(isset($this->encryption->bogus));
171+
$this->assertFalse(isset($this->encryption->bogus)); // @phpstan-ignore property.notFound
170172
}
171173

172174
public function testMagicGet(): void
@@ -176,7 +178,7 @@ public function testMagicGet(): void
176178

177179
public function testMagicGetMissing(): void
178180
{
179-
$this->assertNull($this->encryption->bogus);
181+
$this->assertNull($this->encryption->bogus); // @phpstan-ignore property.notFound
180182
}
181183

182184
public function testDecryptEncryptedDataByCI3AES128CBC(): void

tests/system/Encryption/Handlers/OpenSSLHandlerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function testSanity(): void
4646
$params->key = 'Something other than an empty string';
4747

4848
$encrypter = $this->encryption->initialize($params);
49+
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
4950

5051
$this->assertSame('AES-256-CTR', $encrypter->cipher);
5152
$this->assertSame('Something other than an empty string', $encrypter->key);
@@ -64,6 +65,7 @@ public function testSimple(): void
6465
$params->key = '\xd0\xc9\x08\xc4\xde\x52\x12\x6e\xf8\xcc\xdb\x03\xea\xa0\x3a\x5c';
6566
// Default state (AES-256/Rijndael-256 in CTR mode)
6667
$encrypter = $this->encryption->initialize($params);
68+
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
6769

6870
// Was the key properly set?
6971
$this->assertSame($params->key, $encrypter->key);
@@ -145,6 +147,7 @@ public function testInternalKeyNotModifiedByParams(): void
145147
$params->key = 'original-key-value';
146148

147149
$encrypter = $this->encryption->initialize($params);
150+
$this->assertInstanceOf(OpenSSLHandler::class, $encrypter);
148151

149152
$this->assertSame('original-key-value', $encrypter->key);
150153

tests/system/Encryption/Handlers/SodiumHandlerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ public function testPropertiesGetter(): void
4747
$this->config->key = sodium_crypto_secretbox_keygen();
4848
$this->config->blockSize = 256;
4949
$encrypter = $this->encryption->initialize($this->config);
50+
$this->assertInstanceOf(SodiumHandler::class, $encrypter);
5051

5152
$this->assertSame($this->config->key, $encrypter->key);
5253
$this->assertSame($this->config->blockSize, $encrypter->blockSize);
53-
$this->assertNull($encrypter->driver);
54+
$this->assertNull($encrypter->driver); // @phpstan-ignore property.notFound
5455
}
5556

5657
public function testEmptyKeyThrowsErrorOnInitialize(): void
@@ -136,6 +137,7 @@ public function testInternalKeyNotModifiedByParams(): void
136137

137138
$this->config->key = $originalKey;
138139
$encrypter = $this->encryption->initialize($this->config);
140+
$this->assertInstanceOf(SodiumHandler::class, $encrypter);
139141

140142
$this->assertSame($originalKey, $encrypter->key);
141143

tests/system/Encryption/KeyRotationDecoratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public function testPropertyAccessDelegation(): void
193193
$params->previousKeys = ['old-key'];
194194

195195
$encrypter = $this->encryption->initialize($params);
196+
$this->assertInstanceOf(KeyRotationDecorator::class, $encrypter);
196197

197198
$this->assertSame('AES-128-CBC', $encrypter->cipher);
198199
$this->assertSame('test-key-very-long', $encrypter->key);

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 210 errors
1+
# total 185 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)