| Conditions | 23 |
| Paths | 28 |
| Total Lines | 81 |
| Code Lines | 55 |
| 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 |
||
| 32 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
||
| 33 | { |
||
| 34 | if (!$rule instanceof File) { |
||
| 35 | throw new UnexpectedRuleException(File::class, $rule); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (!is_array($value)) { |
||
| 39 | throw new RuntimeException('Value should be array of UploadedFileInterface'); |
||
| 40 | } |
||
| 41 | |||
| 42 | foreach ($value as $attachFiles) { |
||
| 43 | foreach ($attachFiles as $file) { |
||
| 44 | if (!$file instanceof UploadedFileInterface) { |
||
| 45 | throw new RuntimeException('File should be UploadedFileInterface'); |
||
| 46 | } elseif ($file->getError() == UPLOAD_ERR_NO_FILE) { |
||
| 47 | $formattedMessage = $this->formatter->format( |
||
| 48 | $rule->uploadRequired, |
||
| 49 | ['attribute' => $context->getAttribute(), 'value' => $file] |
||
| 50 | ); |
||
| 51 | } else { |
||
| 52 | $error = $file->getError(); |
||
| 53 | if ($error == UPLOAD_ERR_OK) { |
||
| 54 | if ($rule->maxSize !== null && $file->getSize() > $rule->getSizeLimit()) { |
||
|
|
|||
| 55 | $formattedMessage = $this->formatter->format( |
||
| 56 | $rule->tooBig, |
||
| 57 | [ |
||
| 58 | 'file' => $file->getClientFilename(), |
||
| 59 | 'limit' => $rule->getSizeLimit(), |
||
| 60 | 'formattedLimit' => $this->formatter->asShortSize($rule->getSizeLimit()), |
||
| 61 | ], |
||
| 62 | ); |
||
| 63 | } elseif ($rule->minSize !== null && $file->getSize() < $rule->minSize) { |
||
| 64 | $formattedMessage = $this->formatter->format( |
||
| 65 | $rule->tooSmall, |
||
| 66 | [ |
||
| 67 | 'file' => $file->getClientFilename(), |
||
| 68 | 'limit' => $rule->minSize, |
||
| 69 | 'formattedLimit' => Yii::$app->formatter->asShortSize($rule->minSize), |
||
| 70 | ], |
||
| 71 | ); |
||
| 72 | } elseif (!empty($rule->extensions) && !$this->validateExtension($file)) { |
||
| 73 | $formattedMessage = $this->formatter->format( |
||
| 74 | $rule->wrongExtension, |
||
| 75 | ['file' => $file->name, 'extensions' => implode(', ', $rule->extensions)], |
||
| 76 | ); |
||
| 77 | } elseif (!empty($rule->mimeTypes) && !$this->validateMimeType($file)) { |
||
| 78 | $formattedMessage = $this->formatter->format( |
||
| 79 | $rule->wrongMimeType, |
||
| 80 | ['file' => $file->name, 'mimeTypes' => implode(', ', $rule->mimeTypes)], |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | } elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE) { |
||
| 84 | $formattedMessage = $this->formatter->format( |
||
| 85 | $rule->tooBig, |
||
| 86 | [ |
||
| 87 | 'file' => $file->name, |
||
| 88 | 'limit' => $rule->getSizeLimit(), |
||
| 89 | 'formattedLimit' => Yii::$app->formatter->asShortSize($rule->getSizeLimit()), |
||
| 90 | ], |
||
| 91 | ); |
||
| 92 | } elseif ($error == UPLOAD_ERR_PARTIAL) { |
||
| 93 | $formattedMessage = $this->formatter->format( |
||
| 94 | $rule->uploadErrorPartial, |
||
| 95 | ['attribute' => $context->getAttribute(), 'value' => $file] |
||
| 96 | ); |
||
| 97 | } elseif ($error == UPLOAD_ERR_NO_TMP_DIR) { |
||
| 98 | throw new RuntimeException('Missing the temporary folder to store the uploaded file'); |
||
| 99 | } elseif ($error == UPLOAD_ERR_CANT_WRITE) { |
||
| 100 | throw new RuntimeException('Failed to write the uploaded file to disk'); |
||
| 101 | } elseif ($error == UPLOAD_ERR_EXTENSION) { |
||
| 102 | throw new RuntimeException('File upload was stopped by some PHP extension'); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $result = new Result(); |
||
| 109 | if (isset($formattedMessage)) { |
||
| 110 | $result->addError($formattedMessage); |
||
| 111 | } |
||
| 112 | return $result; |
||
| 113 | } |
||
| 115 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.