| Conditions | 27 |
| Paths | 643 |
| Total Lines | 103 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 123 | public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null) |
||
| 124 | { |
||
| 125 | if ($datatype->getBasetype() !== 'file') { |
||
| 126 | throw new ValidatorException( |
||
| 127 | 'Not a file' |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | self::size($value, $options); |
||
| 132 | |||
| 133 | if ($options[self::ACCEPT] ?? false) { |
||
| 134 | self::accept($value, $options); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (($options[self::DIMENSION_HEIGHT] ?? false) || |
||
| 138 | ($options[self::DIMENSION_WIDTH] ?? false) || |
||
| 139 | ($options[self::DIMENSION_MIN_HEIGHT] ?? false) || |
||
| 140 | ($options[self::DIMENSION_MIN_WIDTH] ?? false) || |
||
| 141 | ($options[self::DIMENSION_MAX_HEIGHT] ?? false) || |
||
| 142 | ($options[self::DIMENSION_MAX_WIDTH] ?? false) || |
||
| 143 | ($options[self::DIMENSION_RATIO] ?? false) |
||
| 144 | ) { |
||
| 145 | $imageData = getimagesize($value); |
||
| 146 | if ($imageData === false) { |
||
| 147 | throw new ValidatorException( |
||
| 148 | 'Not an image' |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | $width = $imageData[0]; |
||
| 152 | $height = $imageData[1]; |
||
| 153 | |||
| 154 | $expectedHeight = $options[self::DIMENSION_HEIGHT] ?? false; |
||
| 155 | if ($expectedHeight !== false) { |
||
| 156 | if ($expectedHeight !== $height) { |
||
| 157 | throw new ValidatorException( |
||
| 158 | 'Image height should be exactly ' . $options[self::DIMENSION_HEIGHT] ?? false |
||
|
|
|||
| 159 | ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $expectedWidth = $options[self::DIMENSION_WIDTH] ?? false; |
||
| 164 | if ($expectedWidth !== false) { |
||
| 165 | if ($expectedWidth !== $width) { |
||
| 166 | throw new ValidatorException( |
||
| 167 | 'Image width should be exactly ' . $expectedWidth |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $minHeight = $options[self::DIMENSION_MIN_HEIGHT] ?? false; |
||
| 173 | if ($minHeight !== false) { |
||
| 174 | if ($height < $minHeight) { |
||
| 175 | throw new ValidatorException( |
||
| 176 | 'Image height should be at least ' . $minHeight |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | $minWidth = $options[self::DIMENSION_MIN_WIDTH] ?? false; |
||
| 182 | if ($minWidth !== false) { |
||
| 183 | if ($width < $minWidth) { |
||
| 184 | throw new ValidatorException( |
||
| 185 | 'Image width should be at least ' . $minWidth |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $maxHeight = $options[self::DIMENSION_MAX_HEIGHT] ?? false; |
||
| 191 | if ($maxHeight !== false) { |
||
| 192 | if ($height > $maxHeight) { |
||
| 193 | throw new ValidatorException( |
||
| 194 | 'Image height should be at most ' . $maxHeight |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | $maxWidth = $options[self::DIMENSION_MAX_WIDTH] ?? false; |
||
| 200 | if ($maxWidth !== false) { |
||
| 201 | if ($width > $maxWidth) { |
||
| 202 | throw new ValidatorException( |
||
| 203 | 'Image width should be at most ' . $maxWidth |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | $ratio = $options[self::DIMENSION_RATIO] ?? false; |
||
| 209 | if ($ratio !== false) { |
||
| 210 | if (!$width || !$height) { |
||
| 211 | throw new ValidatorException( |
||
| 212 | 'Zero width or height' |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | $ratio = $width/$height; |
||
| 216 | $expected = $ratio; |
||
| 217 | if (abs(($ratio-$expected)/$expected) > 0.0001) { |
||
| 218 | throw new ValidatorException( |
||
| 219 | 'Image width/height ratio should be ' . $ratio |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return $value; |
||
| 226 | } |
||
| 244 |