Skip to content

Commit 5fe5fea

Browse files
authored
Merge pull request #31 from JSignPdf/feat/add-psalm-and-apply-fixes
feat: add psalm and apply fixes
2 parents a82b40f + 6f75779 commit 5fe5fea

13 files changed

Lines changed: 3464 additions & 77 deletions

.github/workflows/psalm.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Static analysis
2+
3+
on: pull_request
4+
5+
concurrency:
6+
group: psalm-${{ github.head_ref || github.run_id }}
7+
cancel-in-progress: true
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
static-analysis:
14+
runs-on: ubuntu-latest
15+
16+
name: static-psalm-analysis
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
20+
with:
21+
persist-credentials: false
22+
23+
- name: Set up php8.1
24+
uses: shivammathur/setup-php@ccf2c627fe61b1b4d924adfcbd19d661a18133a0 # v2.35.2
25+
with:
26+
php-version: 8.1
27+
extensions: json, openssl
28+
coverage: none
29+
ini-file: development
30+
# Temporary workaround for missing pcntl_* in PHP 8.3
31+
ini-values: disable_functions=
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
35+
- name: Install dependencies
36+
run: composer i
37+
38+
- name: Run coding standards check
39+
run: composer run psalm -- --output-format=github

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@
3737
}
3838
},
3939
"scripts": {
40+
"test:unit": "vendor/bin/phpunit --no-coverage --colors=always --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations",
41+
"test:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit",
42+
"psalm": "psalm --no-cache --threads=$(nproc)",
43+
"psalm:update-baseline": "psalm --threads=$(nproc) --update-baseline --set-baseline=tests/psalm-baseline.xml",
4044
"post-install-cmd": [
4145
"@composer bin all install --ansi",
4246
"composer dump-autoload"
4347
],
4448
"post-update-cmd": [
4549
"composer dump-autoload"
46-
],
47-
"test:unit": "vendor/bin/phpunit --no-coverage --colors=always --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations",
48-
"test:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit"
50+
]
4951
},
5052
"extra": {
5153
"bamarni-bin": {

psalm.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorBaseline="tests/psalm-baseline.xml"
4+
errorLevel="8"
5+
findUnusedBaselineEntry="true"
6+
findUnusedCode="false"
7+
resolveFromConfigFile="true"
8+
phpVersion="8.1"
9+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xmlns="https://getpsalm.org/schema/config"
11+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor-bin/psalm/vendor/vimeo/psalm/config.xsd"
12+
>
13+
<projectFiles>
14+
<directory name="src" />
15+
<ignoreFiles>
16+
<directory name="vendor" />
17+
</ignoreFiles>
18+
</projectFiles>
19+
<issueHandlers>
20+
<LessSpecificReturnStatement errorLevel="error"/>
21+
<LessSpecificReturnType errorLevel="error"/>
22+
<LessSpecificImplementedReturnType errorLevel="error"/>
23+
<MoreSpecificReturnType errorLevel="error"/>
24+
<UnusedVariable errorLevel="error"/>
25+
<UnusedMethod errorLevel="error"/>
26+
<UnusedClass errorLevel="error"/>
27+
<RedundantCondition errorLevel="error"/>
28+
</issueHandlers>
29+
</psalm>

src/JSignFileService.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@
88
class JSignFileService
99
{
1010

11-
public static function instance()
11+
public static function instance(): self
1212
{
1313
return new self();
1414
}
1515

16-
public function contentFile($path, $isInBase64 = false)
16+
public function contentFile(string $path, bool $isInBase64 = false): string
1717
{
1818
$content = file_get_contents($path);
19+
if ($content === false) {
20+
return '';
21+
}
1922
return $isInBase64 ? base64_encode($content) : $content;
2023
}
2124

22-
public function storeFile($path, $name, $content)
25+
public function storeFile(string $path, string $name, string $content): string
2326
{
2427
$filename = $path . $name;
2528
file_put_contents($filename, $content);
2629
return $filename;
2730
}
2831

29-
public function deleteFile(string $path)
32+
public function deleteFile(string $path): void
3033
{
3134
if (is_file($path))
3235
unlink($path);
3336
}
3437

35-
public function deleteTempFiles(string $pathTemp, string $name)
38+
public function deleteTempFiles(string $pathTemp, string $name): void
3639
{
3740
$pathPfxFile = "$pathTemp$name.pfx";
3841
$pathPdfFile = "$pathTemp$name.pdf";

src/JSignPDF.php

Lines changed: 13 additions & 6 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,31 +11,37 @@
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
{
1819
$this->service = new JSignService();
1920
$this->param = $param;
2021
}
2122

22-
public static function instance(?JSignParam $param = null)
23+
public static function instance(?JSignParam $param = null): self
2324
{
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

37-
public function setParam(JSignParam $param)
44+
public function setParam(JSignParam $param): void
3845
{
3946
$this->param = $param;
4047
}

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 & 2 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, $rootDir) {
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,6 +116,7 @@ private function findRootDir(PharData $phar, $rootDir) {
104116
return trim($parts[0], '/');
105117
}
106118
}
119+
return '';
107120
}
108121

109122
private function chunkDownload(string $url, string $destination): void
@@ -112,6 +125,9 @@ private function chunkDownload(string $url, string $destination): void
112125

113126
if ($fp) {
114127
$ch = curl_init($url);
128+
if ($ch === false) {
129+
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
130+
}
115131
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
116132
curl_setopt($ch, CURLOPT_FILE, $fp);
117133
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

0 commit comments

Comments
 (0)