FileTrait::isValidFileInstance()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 4
rs 10
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Traits;
13
14
use BlitzPHP\Filesystem\Files\File;
15
use BlitzPHP\Filesystem\Files\UploadedFile;
16
use Rakit\Validation\Rules\Traits\FileTrait as TraitsFileTrait;
17
18
trait FileTrait
19
{
20
    use TraitsFileTrait;
21
22
    /**
23
     * Check that the given value is a valid file instance.
24
     */
25
    public function isValidFileInstance(mixed $value): bool
26
    {
27
        if (class_exists(UploadedFile::class) && $value instanceof UploadedFile) {
28 8
            return $value->isValid();
29
        }
30
31 6
        return class_exists(File::class) && $value instanceof File;
32
    }
33
34
    /**
35
     * Check if PHP uploads are explicitly allowed.
36
     *
37
     * @param array<int, int|string> $parameters
38
     */
39
    protected function shouldBlockPhpUpload(mixed $value, array $parameters): bool
40
    {
41
        if (in_array('php', $parameters, true)) {
42
            return false;
43
        }
44
45
        $phpExtensions = [
46
            'php', 'php3', 'php4', 'php5', 'php7', 'php8', 'phtml', 'phar',
47 4
        ];
48
49 4
        return in_array(trim(strtolower($value->clientExtension())), $phpExtensions, true);
50
    }
51
}
52