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