Passed
Push — master ( f53c15...e7ac2a )
by Bruno
09:12
created

Image::dimension()   F

Complexity

Conditions 17
Paths 319

Size

Total Lines 68
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 40
c 1
b 0
f 0
nc 319
nop 3
dl 0
loc 68
rs 2.8958

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Formularium\Validator;
4
5
use Formularium\Datatype;
6
use Formularium\Exception\ValidatorException;
7
use Formularium\Model;
8
use Formularium\ValidatorArgs;
9
use Formularium\ValidatorInterface;
10
use Formularium\ValidatorMetadata;
11
12
/**
13
 * Image file validation
14
 */
15
class Image implements ValidatorInterface
16
{
17
    const DIMENSION_WIDTH = 'dimensionWidth';
18
    const DIMENSION_HEIGHT = 'dimensionHeight';
19
    const DIMENSION_MIN_WIDTH = 'dimensionMinWidth';
20
    const DIMENSION_MAX_WIDTH = 'dimensionMaxWidth';
21
    const DIMENSION_MIN_HEIGHT = 'dimensionMinHeight';
22
    const DIMENSION_MAX_HEIGHT = 'dimensionMaxHeight';
23
    const DIMENSION_RATIO = 'dimensionRatio';
24
25
    protected static function dimension(array $options = [], int $width, int $height): void
26
    {
27
        $expectedHeight = $options[self::DIMENSION_HEIGHT] ?? false;
28
        if ($expectedHeight !== false) {
29
            if ($expectedHeight !== $height) {
30
                throw new ValidatorException(
31
                    'Image height should be exactly ' . $options[self::DIMENSION_HEIGHT] ?? false
0 ignored issues
show
Bug introduced by
It seems like 'Image height should be ...ENSION_HEIGHT] ?? false can also be of type false; however, parameter $message of Formularium\Exception\Va...xception::__construct() does only seem to accept string, 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

31
                    /** @scrutinizer ignore-type */ 'Image height should be exactly ' . $options[self::DIMENSION_HEIGHT] ?? false
Loading history...
32
                );
33
            }
34
        }
35
36
        $expectedWidth = $options[self::DIMENSION_WIDTH] ?? false;
37
        if ($expectedWidth !== false) {
38
            if ($expectedWidth !== $width) {
39
                throw new ValidatorException(
40
                    'Image width should be exactly ' . $expectedWidth
41
                );
42
            }
43
        }
44
45
        $minHeight = $options[self::DIMENSION_MIN_HEIGHT] ?? false;
46
        if ($minHeight !== false) {
47
            if ($height < $minHeight) {
48
                throw new ValidatorException(
49
                    'Image height should be at least ' . $minHeight
50
                );
51
            }
52
        }
53
54
        $minWidth = $options[self::DIMENSION_MIN_WIDTH] ?? false;
55
        if ($minWidth !== false) {
56
            if ($width < $minWidth) {
57
                throw new ValidatorException(
58
                    'Image width should be at least ' . $minWidth
59
                );
60
            }
61
        }
62
63
        $maxHeight = $options[self::DIMENSION_MAX_HEIGHT] ?? false;
64
        if ($maxHeight !== false) {
65
            if ($height > $maxHeight) {
66
                throw new ValidatorException(
67
                    'Image height should be at most ' . $maxHeight
68
                );
69
            }
70
        }
71
72
        $maxWidth = $options[self::DIMENSION_MAX_WIDTH] ?? false;
73
        if ($maxWidth !== false) {
74
            if ($width > $maxWidth) {
75
                throw new ValidatorException(
76
                    'Image width should be at most ' . $maxWidth
77
                );
78
            }
79
        }
80
81
        $ratio = $options[self::DIMENSION_RATIO] ?? false;
82
        if ($ratio !== false) {
83
            if (!$width || !$height) {
84
                throw new ValidatorException(
85
                    'Zero width or height'
86
                );
87
            }
88
            $ratio = $width/$height;
89
            $expected = $ratio;
90
            if (abs(($ratio-$expected)/$expected) > 0.0001) {
91
                throw new ValidatorException(
92
                    'Image width/height ratio should be ' . $ratio
93
                );
94
            }
95
        }
96
    }
97
98
    public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null)
99
    {
100
        if ($datatype->getBasetype() !== 'file') {
101
            throw new ValidatorException(
102
                'Not a file'
103
            );
104
        }
105
    
106
        $imageData = getimagesize($value);
107
        if ($imageData === false) {
108
            throw new ValidatorException(
109
                'Not an image'
110
            );
111
        }
112
113
        if (($options[self::DIMENSION_HEIGHT] ?? false) ||
114
                ($options[self::DIMENSION_WIDTH] ?? false) ||
115
                ($options[self::DIMENSION_MIN_HEIGHT] ?? false) ||
116
                ($options[self::DIMENSION_MIN_WIDTH] ?? false) ||
117
                ($options[self::DIMENSION_MAX_HEIGHT] ?? false) ||
118
                ($options[self::DIMENSION_MAX_WIDTH] ?? false) ||
119
                ($options[self::DIMENSION_RATIO] ?? false)
120
            ) {
121
            $width = $imageData[0];
122
            $height = $imageData[1];
123
            static::dimension($options, $width, $height);
124
        }
125
        return $value;
126
    }
127
128
    public static function getMetadata(): ValidatorMetadata
129
    {
130
        return new ValidatorMetadata(
131
            'Image',
132
            "Image file validations.",
133
            [
134
                new ValidatorArgs(
135
                    self::DIMENSION_RATIO,
136
                    'Float',
137
                    'The expected ratio (width/height)'
138
                )
139
                // TODO: missing args
140
            ]
141
        );
142
    }
143
}
144