Formatting   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizePath() 0 25 5
A sanitizeSeparators() 0 4 1
A toRegex() 0 8 1
1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Utils\Formatting
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Utils;
22
23
/**
24
 * Will provide basic formatting for same needed conversions of strings in special ways
25
 *
26
 * @author    Bernhard Wick <[email protected]>
27
 * @copyright 2015 TechDivision GmbH - <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/doppelgaenger
30
 * @link      http://www.appserver.io/
31
 */
32
class Formatting
33
{
34
35
    /**
36
     * Will break up any path into a canonical form like realpath(), but does not require the file to exist.
37
     *
38
     * @param string $path The path to normalize
39
     *
40
     * @return mixed
41
     */
42
    public function normalizePath($path)
43
    {
44
        return array_reduce(
45
            explode('/', $path),
46
            function ($a, $b) {
47
                if ($a === 0) {
48
                    $a = "/";
49
                }
50
51
                if ($b === "") {
52
                    return $a;
53
                }
54
55
                if ($b === ".") {
56
                    return str_replace(DIRECTORY_SEPARATOR . "Utils", "", __DIR__);
57
                }
58
59
                if ($b === "..") {
60
                    return dirname($a);
61
                }
62
63
                return preg_replace("/\/+/", "/", "$a/$b");
64
            }
65
        );
66
    }
67
68
    /**
69
     * Will turn any wrongly used directory separator in the OS specific one
70
     *
71
     * @param string $path      The path to sanitize
72
     * @param string $separator The separator to sanitize with, DIRECTORY_SEPARATOR by default
73
     *
74
     * @return string
75
     */
76
    public function sanitizeSeparators($path, $separator = DIRECTORY_SEPARATOR)
77
    {
78
        return str_replace(array('/', '\\'), $separator, $path);
79
    }
80
81
    /**
82
     * Converts a string by escaping all regex relevant characters in it.
83
     *
84
     * @param string $string The string to convert
85
     *
86
     * @return string|array
87
     */
88
    public function toRegex($string)
89
    {
90
        return str_replace(
91
            array('$', '(', ')', '*', '[', ']', ' ', '/'),
92
            array('\$', '\(', '\)', '\*', '\[', '\]', '\s*', '\/'),
93
            $string
94
        );
95
    }
96
}
97