| Conditions | 11 |
| Paths | 15 |
| Total Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 41 | public function validate($value, Constraint $constraint) |
||
| 42 | { |
||
| 43 | if (!$constraint instanceof ImageUploadDimension) { |
||
| 44 | throw new UnexpectedTypeException($constraint, ImageUploadDimension::class); |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!$value instanceof MediaInterface) { |
||
| 48 | throw new UnexpectedTypeException($value, MediaInterface::class); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (null === $value->getBinaryContent()) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | $minWidth = 0; |
||
| 56 | $minHeight = 0; |
||
| 57 | |||
| 58 | foreach ($this->imageProvider->getFormatsForContext($value->getContext()) as $format) { |
||
| 59 | if (!$format['constraint']) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | $minWidth = max($minWidth, $format['width'] ?? 0); |
||
| 64 | $minHeight = max($minHeight, $format['height'] ?? 0); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (0 === $minWidth && 0 === $minHeight) { |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | try { |
||
| 72 | $image = $this->imagineAdapter->open($value->getBinaryContent()->getPathname()); |
||
| 73 | } catch (\RuntimeException $e) { |
||
| 74 | // Do nothing. The parent validator will throw a violation error. |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $size = $image->getSize(); |
||
| 79 | |||
| 80 | if ($size->getWidth() < $minWidth || $size->getHeight() < $minHeight) { |
||
| 81 | $this->context |
||
| 82 | ->buildViolation($constraint->message, [ |
||
| 83 | '%min_width%' => $minWidth, |
||
| 84 | '%min_height%' => $minHeight, |
||
| 85 | ]) |
||
| 86 | ->setTranslationDomain('SonataMediaBundle') |
||
| 87 | ->atPath('binaryContent') |
||
| 88 | ->addViolation(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 |