FileTrait::shouldBlockPhpUpload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 2
cts 3
cp 0.6667
crap 2.1481
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