Image::dimension()   C
last analyzed

Complexity

Conditions 17
Paths 10

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 44.6014

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 56
ccs 19
cts 35
cp 0.5429
rs 5.2166
cc 17
nc 10
nop 3
crap 44.6014

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\MetadataParameter;
9
use Formularium\ValidatorInterface;
10
use Formularium\Metadata;
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 4
    protected static function dimension(array $options = [], int $width, int $height): void
26
    {
27 4
        $expectedHeight = $options[self::DIMENSION_HEIGHT] ?? false;
28 4
        if ($expectedHeight !== false && $expectedHeight !== $height) {
29 1
            throw new ValidatorException(
30 1
                'Image height should be exactly ' . $expectedHeight
31
            );
32
        }
33
34 3
        $expectedWidth = $options[self::DIMENSION_WIDTH] ?? false;
35 3
        if ($expectedWidth !== false && $expectedWidth !== $width) {
36 1
            throw new ValidatorException(
37 1
                'Image width should be exactly ' . $expectedWidth
38
            );
39
        }
40
41 2
        $minHeight = $options[self::DIMENSION_MIN_HEIGHT] ?? false;
42 2
        if ($minHeight !== false && $height < $minHeight) {
43
            throw new ValidatorException(
44
                'Image height should be at least ' . $minHeight
45
            );
46
        }
47
48 2
        $minWidth = $options[self::DIMENSION_MIN_WIDTH] ?? false;
49 2
        if ($minWidth !== false && $width < $minWidth) {
50
            throw new ValidatorException(
51
                'Image width should be at least ' . $minWidth
52
            );
53
        }
54
55 2
        $maxHeight = $options[self::DIMENSION_MAX_HEIGHT] ?? false;
56 2
        if ($maxHeight !== false && $height > $maxHeight) {
57
            throw new ValidatorException(
58
                'Image height should be at most ' . $maxHeight
59
            );
60
        }
61
62 2
        $maxWidth = $options[self::DIMENSION_MAX_WIDTH] ?? false;
63 2
        if ($maxWidth !== false && $width > $maxWidth) {
64
            throw new ValidatorException(
65
                'Image width should be at most ' . $maxWidth
66
            );
67
        }
68
69 2
        $ratio = $options[self::DIMENSION_RATIO] ?? false;
70 2
        if ($ratio !== false) {
71
            if (!$width || !$height) {
72
                throw new ValidatorException(
73
                    'Zero width or height'
74
                );
75
            }
76
            $ratio = $width/$height;
77
            $expected = $ratio;
78
            if (abs(($ratio-$expected)/$expected) > 0.0001) {
79
                throw new ValidatorException(
80
                    'Image width/height ratio should be ' . $ratio
81
                );
82
            }
83
        }
84 2
    }
85
86 4
    public static function validate($value, array $options = [], ?Model $model = null)
87
    {
88 4
        $imageData = getimagesize($value);
89 4
        if ($imageData === false) {
90
            throw new ValidatorException(
91
                'Not an image'
92
            );
93
        }
94
95 4
        if (($options[self::DIMENSION_HEIGHT] ?? false) ||
96 2
                ($options[self::DIMENSION_WIDTH] ?? false) ||
97
                ($options[self::DIMENSION_MIN_HEIGHT] ?? false) ||
98
                ($options[self::DIMENSION_MIN_WIDTH] ?? false) ||
99
                ($options[self::DIMENSION_MAX_HEIGHT] ?? false) ||
100
                ($options[self::DIMENSION_MAX_WIDTH] ?? false) ||
101 4
                ($options[self::DIMENSION_RATIO] ?? false)
102
            ) {
103 4
            $width = $imageData[0];
104 4
            $height = $imageData[1];
105 4
            static::dimension($options, $width, $height);
106
        }
107 2
        return $value;
108
    }
109
110 1
    public static function getMetadata(): Metadata
111
    {
112 1
        return new Metadata(
113 1
            'Image',
114 1
            "Image file validations.",
115
            [
116 1
                new MetadataParameter(
117 1
                    self::DIMENSION_RATIO,
118 1
                    'Float',
119 1
                    'The expected ratio (width/height)'
120
                )
121
                // TODO: missing args
122
            ]
123
        );
124
    }
125
}
126