Completed
Push — master ( a12164...2a5888 )
by Arman
16s queued 12s
created

File::imageDimensions()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 17
rs 9.2222
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.8
13
 */
14
15
namespace Quantum\Libraries\Validation\Traits;
16
17
use Quantum\Libraries\Storage\Exceptions\FileUploadException;
18
use Quantum\Libraries\Storage\UploadedFile;
19
20
/**
21
 * Trait File
22
 * @package Quantum\Libraries\Validation\Rules
23
 */
24
trait File
25
{
26
27
    /**
28
     * Validates file size
29
     * @param string $field
30
     * @param UploadedFile $file
31
     * @param int|null $maxSize
32
     * @param int|null $minSize
33
     * @return bool
34
     */
35
    public function fileSize(string $field, UploadedFile $file, int $maxSize, ?int $minSize = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

35
    public function fileSize(/** @scrutinizer ignore-unused */ string $field, UploadedFile $file, int $maxSize, ?int $minSize = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $size = $file->getSize();
38
        $minSize = $minSize ?? 0;
39
40
        return $size >= $minSize && $size <= $maxSize;
41
    }
42
43
    /**
44
     * Validates file mime type
45
     * @param string $field
46
     * @param UploadedFile $file
47
     * @param string ...$mimeTypes
48
     * @return bool
49
     */
50
    public function fileMimeType(string $field, UploadedFile $file, string ...$mimeTypes): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

50
    public function fileMimeType(/** @scrutinizer ignore-unused */ string $field, UploadedFile $file, string ...$mimeTypes): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        return in_array($file->getMimetype(), $mimeTypes);
53
    }
54
55
    /**
56
     * Validates file extension
57
     * @param string $field
58
     * @param UploadedFile $file
59
     * @param string ...$extensions
60
     * @return bool
61
     */
62
    public function fileExtension(string $field, UploadedFile $file, string ...$extensions): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function fileExtension(/** @scrutinizer ignore-unused */ string $field, UploadedFile $file, string ...$extensions): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        return in_array($file->getExtension(), $extensions);
65
    }
66
67
    /**
68
     * Validates image dimensions
69
     * @param string $field
70
     * @param UploadedFile $file
71
     * @param int|null $width
72
     * @param int|null $height
73
     * @return bool
74
     * @throws FileUploadException
75
     */
76
    public function imageDimensions(string $field, UploadedFile $file, ?int $width = null, ?int $height = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

76
    public function imageDimensions(/** @scrutinizer ignore-unused */ string $field, UploadedFile $file, ?int $width = null, ?int $height = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        $dimensions = $file->getDimensions();
79
80
        if (empty($dimensions)) {
81
            return true;
82
        }
83
84
        if ($width !== null && $dimensions['width'] != $width) {
85
            return false;
86
        }
87
88
        if ($height !== null && $dimensions['height'] != $height) {
89
            return false;
90
        }
91
92
        return true;
93
    }
94
}