Test Setup Failed
Push — main ( efd097...c81411 )
by ikechukwu
03:54
created

ClamavFileUpload::areMultipleFilesSafe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 21
rs 9.8666
1
<?php
2
3
namespace Ikechukwukalu\Clamavfileupload\Support;
4
5
use Illuminate\Http\Request;
6
use Ikechukwukalu\Clamavfileupload\Models\FileUpload as FileUploadModel;
7
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ikechukwukalu\Clamavfileupload\Events\FileScanPass;
9
use Ikechukwukalu\Clamavfileupload\Events\FileScanFail;
10
use Ikechukwukalu\Clamavfileupload\Trait\ClamAV;
11
use Ikechukwukalu\Clamavfileupload\Foundation\FileUpload;
12
13
class ClamavFileUpload extends FileUpload
14
{
15
    use ClamAV;
16
17
    /**
18
     * Run files scan and upload.
19
     *
20
     * @return  \Ikechukwukalu\Clamavfileupload\Models\FileUpload
21
     * @return  \Illuminate\Database\Eloquent\Collection
22
     * @return  bool
23
     */
24
    public static function fileUpload(): bool|FileUploadModel|EloquentCollection
25
    {
26
        if(self::$request->file()) {
27
            self::storeFiles();
28
29
            if (!self::areFilesSafe()) {
30
                return self::removeFiles();
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::removeFiles() returns the type true which is incompatible with the documented return type Ikechukwukalu\Clamavfileupload\Models\FileUpload.
Loading history...
31
            }
32
33
            if (is_array(self::$request->file(self::$input))) {
34
                return self::insertMultipleFiles();
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::insertMultipleFiles() could return the type null which is incompatible with the type-hinted return Ikechukwukalu\Clamavfile...uent\Collection|boolean. Consider adding an additional type-check to rule them out.
Loading history...
35
            }
36
37
            return self::insertSingleFile();
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::insertSingleFile() could return the type null which is incompatible with the type-hinted return Ikechukwukalu\Clamavfile...uent\Collection|boolean. Consider adding an additional type-check to rule them out.
Loading history...
38
        }
39
40
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return Ikechukwukalu\Clamavfile...uent\Collection|boolean.
Loading history...
41
    }
42
43
    /**
44
     * Scan file.
45
     *
46
     * @param any $filePath
0 ignored issues
show
Bug introduced by
The type Ikechukwukalu\Clamavfileupload\Support\any was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     * @param any $file
48
     * @return  array
49
     */
50
    public static function scanFile($filePath, $file): array
51
    {
52
        $data = [
53
            'ref' => self::$ref,
54
            'status' => false
55
        ];
56
57
        if (self::ping()) {
58
            $data['status'] = self::scan($filePath);
59
        }
60
61
        $data['message'] = str_replace($filePath,
62
                            $file->getClientOriginalName(),
63
                            self::getMessage());
64
65
        if (self::getMessage() == 'OK') {
66
            $data['errorFile'] = null;
67
            $data['error'] = null;
68
69
            return $data;
70
        }
71
72
        $data['errorFile'] = $file;
73
        $data['error'] = self::getMessage();
74
75
        return $data;
76
    }
77
78
    /**
79
     * Are files safe.
80
     *
81
     * @return  bool
82
     */
83
    private static function areFilesSafe(): bool
84
    {
85
        if (!is_array(self::$request->file(self::$input))) {
86
            return self::isSingleFileSafe();
87
        }
88
89
        return self::areMultipleFilesSafe();
90
    }
91
92
    /**
93
     * Is single file safe.
94
     *
95
     * @return  bool
96
     */
97
    private static function isSingleFileSafe(): bool
98
    {
99
        [$fileName, $relativeFilePath] = self::fileNameAndPath();
100
101
        self::$scanData = self::scanFile(self::storageDisk()->path($relativeFilePath),
0 ignored issues
show
Bug introduced by
self::storageDisk()->path($relativeFilePath) of type string is incompatible with the type Ikechukwukalu\Clamavfileupload\Support\any expected by parameter $filePath of Ikechukwukalu\Clamavfile...vFileUpload::scanFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        self::$scanData = self::scanFile(/** @scrutinizer ignore-type */ self::storageDisk()->path($relativeFilePath),
Loading history...
102
                                self::$request->file(self::$input));
0 ignored issues
show
Bug introduced by
It seems like self::request->file(self::input) can also be of type Illuminate\Http\UploadedFile[] and array; however, parameter $file of Ikechukwukalu\Clamavfile...vFileUpload::scanFile() does only seem to accept Ikechukwukalu\Clamavfileupload\Support\any, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
                                /** @scrutinizer ignore-type */ self::$request->file(self::$input));
Loading history...
103
104
        if (self::$scanData['status']) {
105
            return true;
106
        }
107
108
        self::logScanData(self::$scanData['error']);
109
        FileScanFail::dispatch(self::$scanData);
110
111
        return self::$scanData['status'];
112
    }
113
114
    /**
115
     * Are multiple files safe.
116
     *
117
     * @return  bool
118
     */
119
    private static function areMultipleFilesSafe(): bool
120
    {
121
        $i = 1;
122
        foreach (self::$request->file(self::$input) as $file) {
123
            [$fileName, $relativeFilePath] = self::fileNameAndPath($file, $i);
0 ignored issues
show
Bug introduced by
$i of type integer is incompatible with the type Ikechukwukalu\Clamavfileupload\Foundation\any|null expected by parameter $i of Ikechukwukalu\Clamavfile...load::fileNameAndPath(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
            [$fileName, $relativeFilePath] = self::fileNameAndPath($file, /** @scrutinizer ignore-type */ $i);
Loading history...
124
125
            self::$scanData = self::scanFile(self::storageDisk()->path($relativeFilePath),
0 ignored issues
show
Bug introduced by
self::storageDisk()->path($relativeFilePath) of type string is incompatible with the type Ikechukwukalu\Clamavfileupload\Support\any expected by parameter $filePath of Ikechukwukalu\Clamavfile...vFileUpload::scanFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            self::$scanData = self::scanFile(/** @scrutinizer ignore-type */ self::storageDisk()->path($relativeFilePath),
Loading history...
126
                                $file);
127
128
            if (!self::$scanData['status']) {
129
                self::logScanData(self::$scanData['error']);
130
                FileScanFail::dispatch(self::$scanData);
131
132
                return self::$scanData['status'];
133
            }
134
135
            $i ++;
136
        }
137
138
        FileScanPass::dispatch(self::$scanData);
139
        return true;
140
    }
141
}
142