FileUtils::normalizePath()   C
last analyzed

Complexity

Conditions 13
Paths 32

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 39
cp 0
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 28
nc 32
nop 3
crap 182

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace ConfigToken;
4
5
6
use ConfigToken\TreeSerializer\Types\IniTreeSerializer;
7
use ConfigToken\TreeSerializer\Types\JsonTreeSerializer;
8
use ConfigToken\TreeSerializer\Types\PhpTreeSerializer;
9
use ConfigToken\TreeSerializer\Types\XmlTreeSerializer;
10
use ConfigToken\TreeSerializer\Types\YmlTreeSerializer;
11
12
class FileUtils
13
{
14
15
    /**
16
     * Normalizes the given path containing '.' and '..' while optionally restricting navigation above root folder.
17
     * WARNING: The directory separator will be replaced.
18
     *
19
     * @param string $path The file path to be normalized.
20
     * @param boolean|false $restrictToCurrent Above root restriction flag.
21
     * @param string|null $directorySeparator The directory separator or null for system default.
22
     * @return string
23
     */
24
    public static function normalizePath($path, $restrictToCurrent = false, $directorySeparator = null)
25
    {
26
        if ($restrictToCurrent) {
27
            $path = ltrim($path, '\/');
28
        }
29
        $parts = array();
30
        $path = str_replace('\\', '/', $path);
31
        $path = preg_replace('/\/+/', '/', $path);
32
        $segments = explode('/', $path);
33
        foreach ($segments as $segment) {
34
            if ($segment == '.') {
35
                continue;
36
            }
37
            $test = array_pop($parts);
38
            if (is_null($test)) {
39
                $parts[] = $segment;
40
                continue;
41
            }
42
            if ($segment == '..') {
43
                $testIsBack = $test == '..';
44
                if ($testIsBack) {
45
                    $parts[] = $test;
46
                }
47
                if ($testIsBack || empty($test)) {
48
                    $parts[] = $segment;
49
                }
50
                continue;
51
            }
52
            $parts[] = $test;
53
            $parts[] = $segment;
54
        }
55
        if ($restrictToCurrent) {
56
            reset($parts);
57
            while ((!empty($parts)) && (current($parts) == '..')) {
58
                array_shift($parts);
59
            }
60
        }
61
        return implode(isset($directorySeparator) ? $directorySeparator : DIRECTORY_SEPARATOR, $parts);
62
    }
63
64
    /**
65
     * Return the MIME content type from the given file name.
66
     *
67
     * @param string $fileName The file name.
68
     * @param string $default The default MIME type to return for unknown extensions
69
     * @return string
70
     */
71
    public static function getContentTypeFromFileName($fileName, $default = 'application/octet-stream')
72
    {
73
        if (empty($fileName)) {
74
            return $default;
75
        }
76
        $ext = pathinfo($fileName, PATHINFO_EXTENSION);
77
        switch ($ext) {
78
            case 'ini':
79
                return IniTreeSerializer::getContentType();
80
            case 'json':
81
                return JsonTreeSerializer::getContentType();
82
            case 'php':
83
                return PhpTreeSerializer::getContentType();
84
            case 'xml':
85
                return XmlTreeSerializer::getContentType();
86
            case 'yml':
87
                return YmlTreeSerializer::getContentType();
88
            default:
89
                return $default;
90
        }
91
    }
92
}