Skip to content

Commit 0b51053

Browse files
committed
fix: force typing
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent b009a30 commit 0b51053

6 files changed

Lines changed: 52 additions & 18 deletions

File tree

src/JSignFileService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static function instance(): self
1313
return new self();
1414
}
1515

16-
public function contentFile($path, $isInBase64 = false): string
16+
public function contentFile(string $path, bool $isInBase64 = false): string
1717
{
1818
$content = file_get_contents($path);
1919
if ($content === false) {
@@ -22,7 +22,7 @@ public function contentFile($path, $isInBase64 = false): string
2222
return $isInBase64 ? base64_encode($content) : $content;
2323
}
2424

25-
public function storeFile($path, $name, $content): string
25+
public function storeFile(string $path, string $name, string $content): string
2626
{
2727
$filename = $path . $name;
2828
file_put_contents($filename, $content);

src/JSignPDF.php

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

33
namespace Jeidison\JSignPDF;
44

5+
use Exception;
56
use Jeidison\JSignPDF\Sign\JSignParam;
67
use Jeidison\JSignPDF\Sign\JSignService;
78

@@ -10,8 +11,8 @@
1011
*/
1112
class JSignPDF
1213
{
13-
private $service;
14-
private $param;
14+
private JSignService $service;
15+
private ?JSignParam $param = null;
1516

1617
public function __construct(?JSignParam $param = null)
1718
{
@@ -24,13 +25,19 @@ public static function instance(?JSignParam $param = null): self
2425
return new self($param);
2526
}
2627

27-
public function sign()
28+
public function sign(): string
2829
{
30+
if (!$this->param instanceof JSignParam) {
31+
throw new Exception('Invalid JSignParam instance');
32+
}
2933
return $this->service->sign($this->param);
3034
}
3135

32-
public function getVersion()
36+
public function getVersion(): string
3337
{
38+
if (!$this->param instanceof JSignParam) {
39+
throw new Exception('Invalid JSignParam instance');
40+
}
3441
return $this->service->getVersion($this->param);
3542
}
3643

src/Runtime/JSignPdfRuntimeService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function getPath(JSignParam $params): string
2525

2626
if ($downloadUrl && $jsignPdfPath) {
2727
$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);
28+
if (!is_string($baseDir)) {
29+
throw new InvalidArgumentException('Invalid JsignParamPath');
30+
}
2831
if (!is_dir($baseDir)) {
2932
$ok = mkdir($baseDir, 0755, true);
3033
if ($ok === false) {
@@ -53,7 +56,9 @@ private function downloadAndExtract(JSignParam $params): void
5356
$url = $params->getJSignPdfDownloadUrl();
5457

5558
$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);
56-
59+
if (!is_string($baseDir)) {
60+
throw new InvalidArgumentException('Invalid JsignParamPath');
61+
}
5762
if (!is_dir($baseDir)) {
5863
$ok = mkdir($baseDir, 0755, true);
5964
if (!$ok) {
@@ -89,6 +94,9 @@ private function chunkDownload(string $url, string $destination): void
8994

9095
if ($fp) {
9196
$ch = curl_init($url);
97+
if ($ch === false) {
98+
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
99+
}
92100
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
93101
curl_setopt($ch, CURLOPT_FILE, $fp);
94102
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

src/Runtime/JavaRuntimeService.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function getPath(JSignParam $params): string
3131

3232
if ($downloadUrl && $javaPath) {
3333
$baseDir = preg_replace('/\/bin\/java$/', '', $javaPath);
34+
if (!is_string($baseDir)) {
35+
throw new InvalidArgumentException('Invalid JsignParamPath');
36+
}
3437
if (!is_dir($baseDir)) {
3538
$ok = mkdir($baseDir, 0755, true);
3639
if ($ok === false) {
@@ -52,13 +55,19 @@ private function validateVersion(JSignParam $params): bool
5255
{
5356
$javaPath = $params->getJavaPath();
5457
$baseDir = preg_replace('/\/bin\/java$/', '', $javaPath);
58+
if (!is_string($baseDir)) {
59+
throw new InvalidArgumentException('Invalid JsignParamPath');
60+
}
5561
$lastVersion = $baseDir . '/.java_version_' . basename($params->getJavaDownloadUrl());
5662
return file_exists($lastVersion);
5763
}
5864

5965
private function downloadAndExtract(string $url, string $baseDir): void
6066
{
6167
$baseDir = preg_replace('/\/bin\/java$/', '', $baseDir);
68+
if (!is_string($baseDir)) {
69+
throw new InvalidArgumentException('Invalid JsignParamPath');
70+
}
6271

6372
if (!is_dir($baseDir)) {
6473
$ok = mkdir($baseDir, 0755, true);
@@ -76,7 +85,7 @@ private function downloadAndExtract(string $url, string $baseDir): void
7685
throw new InvalidArgumentException('The file ' . $baseDir . '/java.tar.gz cannot be extracted');
7786
}
7887
$rootDirInsideTar = $this->findRootDir($tar, $baseDir . '/java.tar.gz');
79-
if (!$rootDirInsideTar) {
88+
if (empty($rootDirInsideTar)) {
8089
throw new InvalidArgumentException('Invalid tar content.');
8190
}
8291
$tar->extractTo(directory: $baseDir, overwrite: true);
@@ -91,9 +100,12 @@ private function downloadAndExtract(string $url, string $baseDir): void
91100
chmod($baseDir . '/bin/java', 0700);
92101
}
93102

94-
private function findRootDir(PharData $phar, string $rootDir): ?string {
103+
private function findRootDir(PharData $phar, string $rootDir): string {
95104
$files = new \RecursiveIteratorIterator($phar, \RecursiveIteratorIterator::CHILD_FIRST);
96105
$rootDir = realpath($rootDir);
106+
if (!is_string($rootDir) || empty($rootDir)) {
107+
throw new InvalidArgumentException('Invalid tar content.');
108+
}
97109

98110
foreach ($files as $file) {
99111
$pathName = $file->getPathname();
@@ -104,7 +116,7 @@ private function findRootDir(PharData $phar, string $rootDir): ?string {
104116
return trim($parts[0], '/');
105117
}
106118
}
107-
return null;
119+
return '';
108120
}
109121

110122
private function chunkDownload(string $url, string $destination): void
@@ -113,6 +125,9 @@ private function chunkDownload(string $url, string $destination): void
113125

114126
if ($fp) {
115127
$ch = curl_init($url);
128+
if ($ch === false) {
129+
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
130+
}
116131
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
117132
curl_setopt($ch, CURLOPT_FILE, $fp);
118133
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

src/Sign/JSignParam.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function setTempPath(string $tempPath): self
100100
return $this;
101101
}
102102

103-
public function getTempName(string|null $extension = null): string
103+
public function getTempName(string $extension = ''): string
104104
{
105105
return $this->tempName.$extension;
106106
}

src/Sign/JSignService.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
class JSignService
1616
{
17-
private $fileService;
17+
private JSignFileService $fileService;
1818

1919
public function __construct()
2020
{
@@ -68,7 +68,8 @@ private function repackCertificateIfPasswordIsUnicode(
6868
\OpenSSLAsymmetricKey|\OpenSSLCertificate|string $pkey,
6969
): void
7070
{
71-
if (!mb_detect_encoding($params->getPassword(), 'ASCII', true)) {
71+
$detectedEncodingString = mb_detect_encoding($params->getPassword(), 'ASCII', true);
72+
if ($detectedEncodingString === false) {
7273
$password = md5(microtime());
7374
$newCert = $this->exportToPkcs12($cert, $pkey, $password);
7475
$params->setPassword($password);
@@ -101,6 +102,9 @@ private function validation(JSignParam $params): void
101102
$this->throwIf($this->isExpiredCertificate($params), 'Certificate expired.');
102103
if ($params->isUseJavaInstalled()) {
103104
$javaVersion = exec("java -version 2>&1");
105+
if ($javaVersion === false) {
106+
throw new Exception('Java not installed, set the flag "isUseJavaInstalled" as false or install java.');
107+
}
104108
$hasJavaVersion = strpos($javaVersion, 'not found') === false;
105109
$this->throwIf(!$hasJavaVersion, 'Java not installed, set the flag "isUseJavaInstalled" as false or install java.');
106110
}
@@ -182,8 +186,8 @@ private function pkcs12Read(JSignParam $params): array
182186
}
183187
$msg = openssl_error_string();
184188
if ($msg === 'error:0308010C:digital envelope routines::unsupported') {
185-
$opensslVersion = shell_exec('openssl version');
186-
if (empty($opensslVersion)) {
189+
$opensslVersion = exec('openssl version');
190+
if ($opensslVersion === false) {
187191
return [];
188192
}
189193
$tempPassword = tempnam(sys_get_temp_dir(), 'pfx');
@@ -195,7 +199,7 @@ private function pkcs12Read(JSignParam $params): array
195199
}
196200
file_put_contents($tempPassword, $password);
197201
file_put_contents($tempEncriptedOriginal, $certificate);
198-
$this->safeShellExec($tempPassword, $tempEncriptedOriginal, $tempDecrypted, $tempEncriptedRepacked);
202+
$this->safeExec($tempPassword, $tempEncriptedOriginal, $tempDecrypted, $tempEncriptedRepacked);
199203
$certificateRepacked = file_get_contents($tempEncriptedRepacked);
200204
if ($certificateRepacked === false) {
201205
return [];
@@ -212,7 +216,7 @@ private function pkcs12Read(JSignParam $params): array
212216
return [];
213217
}
214218

215-
private function safeShellExec(
219+
private function safeExec(
216220
string $tempPassword,
217221
string $tempEncriptedOriginal,
218222
string $tempDecrypted,
@@ -224,7 +228,7 @@ private function safeShellExec(
224228
$tempDecrypted = escapeshellarg($tempDecrypted);
225229
$tempEncriptedRepacked = escapeshellarg($tempEncriptedRepacked);
226230

227-
shell_exec(<<<REPACK_COMMAND
231+
exec(<<<REPACK_COMMAND
228232
cat $tempPassword | openssl pkcs12 -legacy -in $tempEncriptedOriginal -nodes -out $tempDecrypted -passin stdin &&
229233
cat $tempPassword | openssl pkcs12 -in $tempDecrypted -export -out $tempEncriptedRepacked -passout stdin
230234
REPACK_COMMAND

0 commit comments

Comments
 (0)