Passed
Pull Request — master (#24)
by mon
02:37
created

AbstractMap   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 368
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 79
dl 0
loc 368
rs 9.0399
c 0
b 0
f 0
ccs 95
cts 95
cp 1
wmc 42

19 Methods

Rating   Name   Duplication   Size   Complexity  
A addTypeExtensionMapping() 0 17 2
A hasType() 0 3 1
A hasAlias() 0 3 1
A listExtensions() 0 3 1
A listAliases() 0 3 1
A listTypes() 0 3 1
A addTypeDescription() 0 9 2
A hasExtension() 0 3 1
A addTypeAlias() 0 16 3
A getTypeDescriptions() 0 3 2
A setExtensionDefaultType() 0 19 3
A removeType() 0 22 4
A setTypeDefaultExtension() 0 3 1
A getAliasTypes() 0 3 2
A removeTypeAlias() 0 17 4
A getTypeAliases() 0 3 2
B removeTypeExtensionMapping() 0 24 7
A getExtensionTypes() 0 3 2
A getTypeExtensions() 0 3 2

How to fix   Complexity   

Complex Class

Complex classes like AbstractMap 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 AbstractMap, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace FileEye\MimeMap\Map;
4
5
use FileEye\MimeMap\MappingException;
6
7
/**
8
 * Abstract class for mapping file extensions to MIME types.
9
 *
10
 * This class cannot be instantiated; extend from it to implement a map.
11
 */
12
abstract class AbstractMap extends BaseMap
13
{
14
    /**
15
     * Determines if a MIME type exists.
16
     *
17
     * @param string $type The type to be found.
18
     *
19
     * @return bool
20
     */
21 17
    public function hasType($type)
22
    {
23 17
        return (bool) $this->getMapEntry('t', $type);
24
    }
25
26
    /**
27
     * Determines if a MIME type alias exists.
28
     *
29
     * @param string $alias The alias to be found.
30
     *
31
     * @return bool
32
     */
33 30
    public function hasAlias($alias)
34
    {
35 30
        return (bool) $this->getMapEntry('a', $alias);
36
    }
37
38
    /**
39
     * Determines if an entry exists from the 'extensions' array.
40
     *
41
     * @param string $extension The extension to be found.
42
     *
43
     * @return bool
44
     */
45 1
    public function hasExtension($extension)
46
    {
47 1
        return (bool) $this->getMapEntry('e', $extension);
48
    }
49
50
    /**
51
     * Lists all the MIME types defined in the map.
52
     *
53
     * @param string $match (Optional) a match wildcard to limit the list.
54
     *
55
     * @return string[]
56
     */
57 7
    public function listTypes($match = null)
58
    {
59 7
        return $this->listEntries('t', $match);
60
    }
61
62
    /**
63
     * Lists all the MIME types aliases defined in the map.
64
     *
65
     * @param string $match (Optional) a match wildcard to limit the list.
66
     *
67
     * @return string[]
68
     */
69 2
    public function listAliases($match = null)
70
    {
71 2
        return $this->listEntries('a', $match);
72
    }
73
74
    /**
75
     * Lists all the extensions defined in the map.
76
     *
77
     * @param string $match (Optional) a match wildcard to limit the list.
78
     *
79
     * @return string[]
80
     */
81 3
    public function listExtensions($match = null)
82
    {
83 3
        return $this->listEntries('e', $match);
84
    }
85
86
    /**
87
     * Adds a description of a MIME type.
88
     *
89
     * @param string $type
90
     *   A MIME type.
91
     * @param string $description
92
     *   The description of the MIME type.
93
     *
94
     * @throws MappingException if $type is an alias.
95
     *
96
     * @return $this
97
     */
98 3
    public function addTypeDescription($type, $description)
99
    {
100
        // Consistency checks.
101 3
        if ($this->hasAlias($type)) {
102 1
            throw new MappingException("Cannot add description for '{$type}', '{$type}' is an alias");
103
        }
104
105 2
        $this->addMapSubEntry('t', $type, 'desc', $description);
106 2
        return $this;
107
    }
108
109
    /**
110
     * Adds an alias of a MIME type.
111
     *
112
     * @param string $type
113
     *   A MIME type.
114
     * @param string $alias
115
     *   An alias of $type.
116
     *
117
     * @throws MappingException if no $type is found.
118
     *
119
     * @return $this
120
     */
121 7
    public function addTypeAlias($type, $alias)
122
    {
123 7
        $type = strtolower($type);
124 7
        $alias = strtolower($alias);
125
126
        // Consistency checks.
127 7
        if (!$this->hasType($type)) {
128 1
            throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$type}' not defined");
129
        }
130 6
        if ($this->hasType($alias)) {
131 2
            throw new MappingException("Cannot set '{$alias}' as alias for '{$type}', '{$alias}' is already defined as a type");
132
        }
133
134 5
        $this->addMapSubEntry('t', $type, 'a', $alias);
135 5
        $this->addMapSubEntry('a', $alias, 't', $type);
136 5
        return $this;
137
    }
138
139
    /**
140
     * Adds a type-to-extension mapping.
141
     *
142
     * @param string $type
143
     *   A MIME type.
144
     * @param string $extension
145
     *   A file extension.
146
     *
147
     * @throws MappingException if $type is an alias.
148
     *
149
     * @return $this
150
     */
151 10
    public function addTypeExtensionMapping($type, $extension)
152
    {
153 10
        $type = strtolower($type);
154 10
        $extension = strtolower($extension);
155
156
        // Consistency checks.
157 10
        if ($this->hasAlias($type)) {
158 2
            throw new MappingException("Cannot map '{$extension}' to '{$type}', '{$type}' is an alias");
159
        }
160
161
        // Add entry to 't'.
162 9
        $this->addMapSubEntry('t', $type, 'e', $extension);
163
164
        // Add entry to 'e'.
165 9
        $this->addMapSubEntry('e', $extension, 't', $type);
166
167 9
        return $this;
168
    }
169
170
    /**
171
     * Gets the descriptions of a MIME type.
172
     *
173
     * @param string $type The type to be found.
174
     *
175
     * @return string[] The mapped descriptions.
176
     */
177 1
    public function getTypeDescriptions($type)
178
    {
179 1
        return $this->getMapSubEntry('t', $type, 'desc') ?: [];
180
    }
181
182
    /**
183
     * Gets the aliases of a MIME type.
184
     *
185
     * @param string $type The type to be found.
186
     *
187
     * @return string[] The mapped aliases.
188
     */
189 3
    public function getTypeAliases($type)
190
    {
191 3
        return $this->getMapSubEntry('t', $type, 'a') ?: [];
192
    }
193
194
    /**
195
     * Gets the content of an entry from the 't' array.
196
     *
197
     * @param string $type The type to be found.
198
     *
199
     * @return string[] The mapped file extensions.
200
     */
201 6
    public function getTypeExtensions($type)
202
    {
203 6
        return $this->getMapSubEntry('t', $type, 'e') ?: [];
204
    }
205
206
    /**
207
     * Changes the default extension for a MIME type.
208
     *
209
     * @param string $type
210
     *   A MIME type.
211
     * @param string $extension
212
     *   A file extension.
213
     *
214
     * @throws MappingException if no mapping found.
215
     *
216
     * @return $this
217
     */
218 3
    public function setTypeDefaultExtension($type, $extension)
219
    {
220 3
        return $this->setValueAsDefault('t', $type, 'e', $extension);
221
    }
222
223
    /**
224
     * Removes the entire mapping of a type.
225
     *
226
     * @param string $type
227
     *   A MIME type.
228
     *
229
     * @return bool
230
     *   true if the mapping was removed, false if the type was not present.
231
     */
232 2
    public function removeType($type)
233
    {
234 2
        $type = strtolower($type);
235
236
        // Return false if type is not found.
237 2
        if (!$this->hasType($type)) {
238 1
            return false;
239
        }
240
241
        // Loop through all the associated extensions and remove them.
242 2
        foreach ($this->getTypeExtensions($type) as $extension) {
243 2
            $this->removeTypeExtensionMapping($type, $extension);
244
        }
245
246
        // Loop through all the associated aliases and remove them.
247 2
        foreach ($this->getTypeAliases($type) as $alias) {
248 2
            $this->removeTypeAlias($type, $alias);
249
        }
250
251 2
        unset(static::$map['t'][$type]);
252
253 2
        return true;
254
    }
255
256
    /**
257
     * Removes a MIME type alias.
258
     *
259
     * @param string $type
260
     *   A MIME type.
261
     * @param string $alias
262
     *   The alias to be removed.
263
     *
264
     * @return bool
265
     *   true if the alias was removed, false if the alias was not present.
266
     */
267 2
    public function removeTypeAlias($type, $alias)
268
    {
269 2
        $type = strtolower($type);
270 2
        $alias = strtolower($alias);
271
272
        // Remove any extension mapped to the alias.
273 2
        if ($extensions = $this->getMapSubEntry('a', $alias, 'e')) {
274 1
            foreach ($extensions as $extension) {
275 1
                $this->removeMapSubEntry('a', $alias, 'e', $extension);
276 1
                $this->removeMapSubEntry('e', $extension, 't', $alias);
277
            }
278
        }
279
280 2
        $type_ret = $this->removeMapSubEntry('t', $type, 'a', $alias);
281 2
        $alias_ret = $this->removeMapSubEntry('a', $alias, 't', $type);
282
283 2
        return $type_ret && $alias_ret;
284
    }
285
286
    /**
287
     * Removes a type-to-extension mapping.
288
     *
289
     * @param string $type
290
     *   A MIME type.
291
     * @param string $extension
292
     *   The file extension to be removed.
293
     *
294
     * @return bool
295
     *   true if the mapping was removed, false if the mapping was not present.
296
     */
297 4
    public function removeTypeExtensionMapping($type, $extension)
298
    {
299 4
        $type = strtolower($type);
300 4
        $extension = strtolower($extension);
301
302 4
        if ($this->hasAlias($type)) {
303 1
            $alias = $type;
304 1
            $type_ret = $this->removeMapSubEntry('a', $alias, 'e', $extension);
305 1
            $extension_ret = $this->removeMapSubEntry('e', $extension, 't', $alias);
306
        } else {
307
            // Remove any associated aliased type as well.
308 3
            if ($associated_types = $this->getMapSubEntry('e', $extension, 't')) {
309 3
                foreach ($associated_types as $associated_type) {
310 3
                    if ($this->hasAlias($associated_type) && $type === $this->getAliasTypes($associated_type)[0]) {
311 2
                        $this->removeMapSubEntry('a', $associated_type, 'e', $extension);
312 3
                        $this->removeMapSubEntry('e', $extension, 't', $associated_type);
313
                    }
314
                }
315
            }
316 3
            $type_ret = $this->removeMapSubEntry('t', $type, 'e', $extension);
317 3
            $extension_ret = $this->removeMapSubEntry('e', $extension, 't', $type);
318
        }
319
320 4
        return $type_ret && $extension_ret;
321
    }
322
323
    /**
324
     * Gets the parent types of an alias.
325
     *
326
     * @param string $alias The alias to be found.
327
     *
328
     * @return string[]
329
     */
330 7
    public function getAliasTypes($alias)
331
    {
332 7
        return $this->getMapSubEntry('a', $alias, 't') ?: [];
333
    }
334
335
    /**
336
     * Gets the content of an entry from the 'extensions' array.
337
     *
338
     * @param string $extension The extension to be found.
339
     *
340
     * @return string[] The mapped MIME types.
341
     */
342 12
    public function getExtensionTypes($extension)
343
    {
344 12
        return $this->getMapSubEntry('e', $extension, 't') ?: [];
345
    }
346
347
    /**
348
     * Changes the default MIME type for a file extension.
349
     *
350
     * Allows a MIME type alias to be set as default for the extension.
351
     *
352
     * @param string $extension
353
     *   A file extension.
354
     * @param string $type
355
     *   A MIME type.
356
     *
357
     * @throws MappingException if no mapping found.
358
     *
359
     * @return $this
360
     */
361 7
    public function setExtensionDefaultType($extension, $type)
362
    {
363 7
        $type = strtolower($type);
364 7
        $extension = strtolower($extension);
365
366 7
        if ($this->hasAlias($type)) {
367 4
            $alias = $type;
368 4
            $type = $this->getAliasTypes($alias)[0];
369
            // Check that the alias is referring to a type associated with that
370
            // extension.
371 4
            $associated_types = $this->getMapSubEntry('e', $extension, 't');
372 4
            if (!in_array($type, $associated_types)) {
0 ignored issues
show
Bug introduced by
It seems like $associated_types can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

372
            if (!in_array($type, /** @scrutinizer ignore-type */ $associated_types)) {
Loading history...
373 1
                throw new MappingException("Cannot set '{$alias}' as default type for extension '{$extension}', its unaliased type '{$type}' is not associated to '{$extension}'");
374
            }
375 3
            $this->addMapSubEntry('a', $alias, 'e', $extension);
376 3
            $this->addMapSubEntry('e', $extension, 't', $alias);
377 3
            return $this->setValueAsDefault('e', $extension, 't', $alias);
378
        } else {
379 3
            return $this->setValueAsDefault('e', $extension, 't', $type);
380
        }
381
    }
382
}
383