| Conditions | 11 |
| Paths | 34 |
| Total Lines | 74 |
| Code Lines | 53 |
| 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); |
||
| 37 | protected static function accept(string $value, array $options = []): void |
||
| 38 | { |
||
| 39 | $accept = $options[self::ACCEPT]; |
||
| 40 | if (!is_array($accept)) { |
||
| 41 | $accept = [$accept]; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array $accept |
||
| 46 | */ |
||
| 47 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
| 48 | if ($finfo === false) { |
||
| 49 | throw new ValidatorException( |
||
| 50 | 'Cannot load fileinfo' |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | $mime = finfo_file($finfo, $value); |
||
| 54 | |||
| 55 | $valid = false; |
||
| 56 | foreach ($accept as $a) { |
||
| 57 | switch ($a) { |
||
| 58 | case self::ACCEPT_AUDIO: |
||
| 59 | $validMimes = [ |
||
| 60 | 'audio/aac', |
||
| 61 | 'audio/mpeg', |
||
| 62 | 'audio/ogg', |
||
| 63 | 'audio/wav', |
||
| 64 | 'audio/webm', |
||
| 65 | ]; |
||
| 66 | if (in_array($mime, $validMimes)) { |
||
| 67 | $valid = true; |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | break; |
||
| 71 | case self::ACCEPT_IMAGE: |
||
| 72 | $validMimes = [ |
||
| 73 | 'image/jpg', |
||
| 74 | 'image/jpeg', |
||
| 75 | 'image/gif', |
||
| 76 | 'image/png', |
||
| 77 | 'image/webp' |
||
| 78 | ]; |
||
| 79 | if (in_array($mime, $validMimes)) { |
||
| 80 | $valid = true; |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | break; |
||
| 84 | case self::ACCEPT_VIDEO: |
||
| 85 | $validMimes = [ |
||
| 86 | 'video/x-flv', |
||
| 87 | 'video/mp4', |
||
| 88 | 'video/mpeg', |
||
| 89 | 'application/x-mpegURL', |
||
| 90 | 'video/MP2T', |
||
| 91 | 'video/3gpp', |
||
| 92 | 'video/ogg', |
||
| 93 | 'video/quicktime', |
||
| 94 | 'video/x-msvideo', |
||
| 95 | 'video/x-ms-wmv', |
||
| 96 | 'video/webm', |
||
| 97 | ]; |
||
| 98 | if (in_array($mime, $validMimes)) { |
||
| 99 | $valid = true; |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // TODO: 'accept' extensions |
||
| 107 | |||
| 108 | if (!$valid) { |
||
| 109 | throw new ValidatorException( |
||
| 110 | 'Not an accepted file' |
||
| 111 | ); |
||
| 152 |