Skip to content

Commit e60fc75

Browse files
authored
refactor: set minimum PHP 8.2 (#11)
* refactor: set minimum PHP 8.2 * refactor: set rector level list PHP 8.2 * docs: phpdocs
1 parent c3d4d12 commit e60fc75

5 files changed

Lines changed: 35 additions & 78 deletions

File tree

.github/workflows/test-phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
if: (! contains(github.event.head_commit.message, '[ci skip]'))
3232
strategy:
3333
matrix:
34-
php-versions: ['8.1', '8.2', '8.3', '8.4', '8.5']
34+
php-versions: ['8.2', '8.3', '8.4', '8.5']
3535

3636
steps:
3737
- name: Checkout

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@
2121
],
2222
"homepage": "https://github.com/PHPDevsr/PHPSpreadsheet-Secure",
2323
"require": {
24-
"php": "^8.1 || ^8.2 || ^8.3 || ^8.4 || ^8.5",
24+
"php": "^8.2 || ^8.3 || ^8.4 || ^8.5",
2525
"ext-openssl": "*",
2626
"ext-hash": "*",
2727
"ext-simplexml": "*",
2828
"pear/ole": "^v1.0.0",
2929
"pear/pear-core-minimal": "^v1.10.15"
3030
},
3131
"require-dev": {
32-
"codeigniter/coding-standard": "^1.8.1 || ^1.9.1",
32+
"codeigniter/coding-standard": "^1.9.1",
3333
"nexusphp/tachycardia": "^2.4",
3434
"phpstan/extension-installer": "^1.4",
3535
"phpstan/phpstan": "^2.1.40",
3636
"phpstan/phpstan-strict-rules": "^2.0.10",
37-
"phpunit/phpcov": "^9.0.2 || ^10.0 || ^11.0",
38-
"phpunit/phpunit": "^10.5.16 || ^11.5",
37+
"phpunit/phpcov": "^10.0 || ^11.0",
38+
"phpunit/phpunit": "^11.5",
3939
"rector/rector": "2.3.8"
4040
},
4141
"minimum-stability": "dev",

phpstan-baseline.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,12 @@
1313
'count' => 1,
1414
'path' => __DIR__ . '/src/Secure.php',
1515
];
16-
$ignoreErrors[] = [
17-
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_convertPasswordToKey\\(\\) has parameter \\$hashAlgorithm with no type specified\\.$#',
18-
'identifier' => 'missingType.parameter',
19-
'count' => 1,
20-
'path' => __DIR__ . '/src/Secure.php',
21-
];
22-
$ignoreErrors[] = [
23-
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_convertPasswordToKey\\(\\) has parameter \\$keyBits with no type specified\\.$#',
24-
'identifier' => 'missingType.parameter',
25-
'count' => 1,
26-
'path' => __DIR__ . '/src/Secure.php',
27-
];
28-
$ignoreErrors[] = [
29-
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_convertPasswordToKey\\(\\) has parameter \\$password with no type specified\\.$#',
30-
'identifier' => 'missingType.parameter',
31-
'count' => 1,
32-
'path' => __DIR__ . '/src/Secure.php',
33-
];
3416
$ignoreErrors[] = [
3517
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_convertPasswordToKey\\(\\) has parameter \\$saltValue with no type specified\\.$#',
3618
'identifier' => 'missingType.parameter',
3719
'count' => 1,
3820
'path' => __DIR__ . '/src/Secure.php',
3921
];
40-
$ignoreErrors[] = [
41-
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_convertPasswordToKey\\(\\) has parameter \\$spinCount with no type specified\\.$#',
42-
'identifier' => 'missingType.parameter',
43-
'count' => 1,
44-
'path' => __DIR__ . '/src/Secure.php',
45-
];
4622
$ignoreErrors[] = [
4723
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:_createIV\\(\\) has no return type specified\\.$#',
4824
'identifier' => 'missingType.return',
@@ -79,12 +55,6 @@
7955
'count' => 1,
8056
'path' => __DIR__ . '/src/Secure.php',
8157
];
82-
$ignoreErrors[] = [
83-
'message' => '#^Method PHPDevsr\\\\Spreadsheet\\\\Secure\\:\\:output\\(\\) should return string but returns string\\|false\\.$#',
84-
'identifier' => 'return.type',
85-
'count' => 1,
86-
'path' => __DIR__ . '/src/Secure.php',
87-
];
8858
$ignoreErrors[] = [
8959
'message' => '#^Offset \'name\' might not exist on array\\<string, string\\>\\|string\\.$#',
9060
'identifier' => 'offsetAccess.notFound',
@@ -127,12 +97,6 @@
12797
'count' => 1,
12898
'path' => __DIR__ . '/src/Secure.php',
12999
];
130-
$ignoreErrors[] = [
131-
'message' => '#^Parameter \\#1 \\$string of function bin2hex expects string, array\\|string given\\.$#',
132-
'identifier' => 'argument.type',
133-
'count' => 1,
134-
'path' => __DIR__ . '/src/Secure.php',
135-
];
136100
$ignoreErrors[] = [
137101
'message' => '#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#',
138102
'identifier' => 'argument.type',

rector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
$rectorConfig->sets([
8080
SetList::DEAD_CODE,
8181
SetList::CODE_QUALITY,
82-
SetList::STRICT_BOOLEANS,
83-
LevelSetList::UP_TO_PHP_80,
82+
SetList::CODING_STYLE,
83+
LevelSetList::UP_TO_PHP_82,
8484
]);
8585

8686
// The paths to refactor (can also be supplied with CLI arguments)
@@ -99,7 +99,7 @@
9999
}
100100

101101
// Set the target version for refactoring
102-
$rectorConfig->phpVersion(PhpVersion::PHP_82);
102+
$rectorConfig->phpVersion(PhpVersion::PHP_83);
103103

104104
// Auto-import fully qualified class names
105105
$rectorConfig->importNames();

src/Secure.php

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function setFile($data = '')
101101
while (! feof($fp)) {
102102
yield (array) unpack('C*', (string) fread($fp, 4096));
103103
}
104+
104105
fclose($fp);
105106
unset($fp);
106107
});
@@ -305,6 +306,7 @@ public function output(?string $filePath = null)
305306

306307
$OLE2 = new OLE_PPS_File(OLE::Asc2Ucs('EncryptedPackage'));
307308
$OLE2->init();
309+
308310
$filesize = (int) filesize($encryptedPackage['tmpFile']);
309311

310312
for ($i = 0; $i < ($filesize / 4096); $i++) {
@@ -324,7 +326,7 @@ public function output(?string $filePath = null)
324326

325327
$root->save($filePath);
326328

327-
return file_get_contents($filePath);
329+
return (string) file_get_contents($filePath);
328330
}
329331

330332
/**
@@ -427,21 +429,18 @@ private function arrayToXml(array $array = [])
427429
/**
428430
* Crypt method
429431
*
430-
* @param string $cipherAlgorithm
431-
* @param string $cipherChaining
432432
* @param array $key
433-
* @param bool $encrypt
434433
* @param array<int|string, int|string> $iv
435434
* @param array<int|string, int|string> $input
436435
*/
437-
private function _crypt($encrypt, $cipherAlgorithm, $cipherChaining, $key, $iv, $input)
436+
private function _crypt(bool $encrypt, string $cipherAlgorithm, string $cipherChaining, $key, $iv, $input)
438437
{
439438
$algorithm = $cipherAlgorithm . '-' . (count($key) * 8);
440439

441440
if ($cipherChaining === 'ChainingModeCBC') {
442441
$algorithm .= '-cbc';
443442
} else {
444-
throw new Exception("Unknown cipher chaining: {$cipherChaining}");
443+
throw new Exception('Unknown cipher chaining: ' . $cipherChaining);
445444
}
446445

447446
$cipher = [];
@@ -463,11 +462,9 @@ private function _crypt($encrypt, $cipherAlgorithm, $cipherChaining, $key, $iv,
463462
/**
464463
* Hashing
465464
*
466-
* @param string $algorithm
467-
*
468465
* @return array<int|string, int|string>
469466
*/
470-
private function _hash($algorithm, ...$buffers)
467+
private function _hash(string $algorithm, ...$buffers)
471468
{
472469
try {
473470
$ctx = hash_init($algorithm);
@@ -489,7 +486,7 @@ private function _hash($algorithm, ...$buffers)
489486
*
490487
* @return array<int|string, int|string>
491488
*/
492-
private function _hmac($algorithm, $key, $fileName)
489+
private function _hmac(string $algorithm, $key, string $fileName)
493490
{
494491
return (array) unpack('C*', hash_hmac_file(
495492
$algorithm,
@@ -501,19 +498,16 @@ private function _hmac($algorithm, $key, $fileName)
501498

502499
/**
503500
* Create Unsigned Integer 32-bit Buffer
504-
*
505-
* @param int $value
506-
* @param int $bufferSize
507501
*/
508-
private function _createUInt32LEBuffer($value, $bufferSize = 4)
502+
private function _createUInt32LEBuffer(int $value, int $bufferSize = 4)
509503
{
510504
return array_pad(array_values((array) unpack('C*', pack('V', $value))), $bufferSize, 0);
511505
}
512506

513-
private function _convertPasswordToKey($password, $hashAlgorithm, $saltValue, $spinCount, $keyBits, $blockKey)
507+
private function _convertPasswordToKey(string $password, string $hashAlgorithm, $saltValue, int $spinCount, int $keyBits, $blockKey)
514508
{
515509
// Password must be in unicode buffer
516-
$passwordBuffer = array_map('hexdec', str_split(bin2hex(mb_convert_encoding($password, 'UTF-16LE', 'utf-8')), 2));
510+
$passwordBuffer = array_map(hexdec(...), str_split(bin2hex(mb_convert_encoding($password, 'UTF-16LE', 'utf-8')), 2));
517511

518512
// Generate the initial hash
519513
$key = $this->_hash($hashAlgorithm, $saltValue, $passwordBuffer);
@@ -547,12 +541,10 @@ private function _convertPasswordToKey($password, $hashAlgorithm, $saltValue, $s
547541
/**
548542
* Create initialization vector
549543
*
550-
* @param string $hashAlgorithm
551544
* @param array<int|string, int|string> $saltValue
552-
* @param int $blockSize
553545
* @param mixed $blockKey
554546
*/
555-
private function _createIV($hashAlgorithm, $saltValue, $blockSize, $blockKey)
547+
private function _createIV(string $hashAlgorithm, $saltValue, int $blockSize, $blockKey)
556548
{
557549
// Create the block key from the current index
558550
if (is_int($blockKey)) {
@@ -574,40 +566,38 @@ private function _createIV($hashAlgorithm, $saltValue, $blockSize, $blockKey)
574566
/**
575567
* Package Crypt
576568
*
577-
* @param bool $encrypt
578-
* @param string $cipherAlgorithm
579-
* @param string $cipherChaining
580-
* @param string $hashAlgorithm
581-
* @param int $blockSize
582569
* @param array<int|string, int|string> $saltValue
583-
* @param mixed $input
570+
* @param Closure|null $input
584571
* @param array<int|string, int|string> $key
585572
*/
586573
private function _cryptPackage(
587-
$encrypt,
588-
$cipherAlgorithm,
589-
$cipherChaining,
590-
$hashAlgorithm,
591-
$blockSize,
574+
bool $encrypt,
575+
string $cipherAlgorithm,
576+
string $cipherChaining,
577+
string $hashAlgorithm,
578+
int $blockSize,
592579
$saltValue,
593580
$input,
594581
$key = [],
595582
) {
596-
$tmpOutputChunk = (string) tempnam(sys_get_temp_dir(), 'outputChunk');
597-
$tmpFileHeaderLength = (string) tempnam(sys_get_temp_dir(), 'fileHeaderLength');
598-
$tmpFile = (string) tempnam(sys_get_temp_dir(), 'file');
583+
$sysTempDir = sys_get_temp_dir();
584+
$tmpOutputChunk = (string) tempnam($sysTempDir, 'outputChunk');
585+
$tmpFileHeaderLength = (string) tempnam($sysTempDir, 'fileHeaderLength');
586+
$tmpFile = (string) tempnam($sysTempDir, 'file');
599587

600-
if (is_callable($input) && is_a($in = $input(), 'Generator')) {
588+
if ($input !== null && is_a($in = $input(), 'Generator')) {
601589
$inputCount = 0;
602590

603591
foreach ($in as $i => $inputChunk) {
604592
$lengthInputChunk = count($inputChunk);
605593
// Grab the next chunk
606594
$inputCount += $lengthInputChunk;
607595
$remainder = $lengthInputChunk % $blockSize;
596+
608597
if ($remainder !== 0) {
609598
$inputChunk = array_pad($inputChunk, $lengthInputChunk + (16 - $remainder), 0);
610599
}
600+
611601
// Create the initialization vector
612602
$iv = $this->_createIV($hashAlgorithm, $saltValue, $blockSize, $i);
613603

@@ -632,6 +622,8 @@ private function _cryptPackage(
632622
'tmpFile' => $tmpFile,
633623
];
634624
}
625+
626+
return null;
635627
}
636628

637629
/**
@@ -657,6 +649,7 @@ private function build(SimpleXMLElement $rootNode, array $data = [])
657649
$rootNode->addAttribute($kk, $vv);
658650
}
659651
}
652+
660653
if ($k === 'children') {
661654
$isNamespace = count(explode(':', $vv['name'])) === 2;
662655
$r = $isNamespace ? $rootNode->addChild('xmlns:' . $vv['name'], '') : $rootNode->addChild($vv['name'], '');

0 commit comments

Comments
 (0)