| Total Complexity | 50 |
| Total Lines | 293 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Detection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Detection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Detection implements IMimeTypeDetector { |
||
| 47 | protected $mimetypes = []; |
||
| 48 | protected $secureMimeTypes = []; |
||
| 49 | |||
| 50 | protected $mimetypeIcons = []; |
||
| 51 | /** @var string[] */ |
||
| 52 | protected $mimeTypeAlias = []; |
||
| 53 | |||
| 54 | /** @var IURLGenerator */ |
||
| 55 | private $urlGenerator; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $customConfigDir; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | private $defaultConfigDir; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param IURLGenerator $urlGenerator |
||
| 65 | * @param string $customConfigDir |
||
| 66 | * @param string $defaultConfigDir |
||
| 67 | */ |
||
| 68 | public function __construct(IURLGenerator $urlGenerator, |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Add an extension -> mimetype mapping |
||
| 78 | * |
||
| 79 | * $mimetype is the assumed correct mime type |
||
| 80 | * The optional $secureMimeType is an alternative to send to send |
||
| 81 | * to avoid potential XSS. |
||
| 82 | * |
||
| 83 | * @param string $extension |
||
| 84 | * @param string $mimetype |
||
| 85 | * @param string|null $secureMimeType |
||
| 86 | */ |
||
| 87 | public function registerType($extension, |
||
| 88 | $mimetype, |
||
| 89 | $secureMimeType = null) { |
||
| 90 | $this->mimetypes[$extension] = array($mimetype, $secureMimeType); |
||
| 91 | $this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add an array of extension -> mimetype mappings |
||
| 96 | * |
||
| 97 | * The mimetype value is in itself an array where the first index is |
||
| 98 | * the assumed correct mimetype and the second is either a secure alternative |
||
| 99 | * or null if the correct is considered secure. |
||
| 100 | * |
||
| 101 | * @param array $types |
||
| 102 | */ |
||
| 103 | public function registerTypeArray($types) { |
||
| 104 | $this->mimetypes = array_merge($this->mimetypes, $types); |
||
| 105 | |||
| 106 | // Update the alternative mimetypes to avoid having to look them up each time. |
||
| 107 | foreach ($this->mimetypes as $mimeType) { |
||
| 108 | $this->secureMimeTypes[$mimeType[0]] = isset($mimeType[1]) ? $mimeType[1]: $mimeType[0]; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Add the mimetype aliases if they are not yet present |
||
| 114 | */ |
||
| 115 | private function loadAliases() { |
||
| 116 | if (!empty($this->mimeTypeAlias)) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
||
| 121 | |||
| 122 | if (file_exists($this->customConfigDir . '/mimetypealiases.json')) { |
||
| 123 | $custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypealiases.json'), true); |
||
| 124 | $this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string[] |
||
| 130 | */ |
||
| 131 | public function getAllAliases() { |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getOnlyDefaultAliases() { |
||
| 137 | $this->loadMappings(); |
||
| 138 | $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); |
||
| 139 | return $this->mimeTypeAlias; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add mimetype mappings if they are not yet present |
||
| 144 | */ |
||
| 145 | private function loadMappings() { |
||
| 146 | if (!empty($this->mimetypes)) { |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | |||
| 150 | $mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true); |
||
| 151 | |||
| 152 | //Check if need to load custom mappings |
||
| 153 | if (file_exists($this->customConfigDir . '/mimetypemapping.json')) { |
||
| 154 | $custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypemapping.json'), true); |
||
| 155 | $mimetypeMapping = array_merge($mimetypeMapping, $custom); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->registerTypeArray($mimetypeMapping); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getAllMappings() { |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * detect mimetype only based on filename, content of file is not used |
||
| 171 | * |
||
| 172 | * @param string $path |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function detectPath($path) { |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * detect mimetype based on both filename and content |
||
| 202 | * |
||
| 203 | * @param string $path |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function detect($path) { |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * detect mimetype based on the content of a string |
||
| 253 | * |
||
| 254 | * @param string $data |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function detectString($data) { |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get a secure mimetype that won't expose potential XSS. |
||
| 275 | * |
||
| 276 | * @param string $mimeType |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function getSecureMimeType($mimeType) { |
||
| 280 | $this->loadMappings(); |
||
| 281 | |||
| 282 | return isset($this->secureMimeTypes[$mimeType]) |
||
| 283 | ? $this->secureMimeTypes[$mimeType] |
||
| 284 | : 'application/octet-stream'; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get path to the icon of a file type |
||
| 289 | * @param string $mimetype the MIME type |
||
| 290 | * @return string the url |
||
| 291 | */ |
||
| 292 | public function mimeTypeIcon($mimetype) { |
||
| 339 | } |
||
| 340 | } |
||
| 341 |