Helper::appendSlashes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sulao\LRTS;
4
5
class Helper
6
{
7
    /**
8
     * Append trail slashes
9
     *
10
     * @param string $path   the path to append trail slashes
11
     * @param string $origin the origin path with trail slashes
12
     *
13
     * @return string
14
     */
15
    public static function appendSlashes($path, $origin)
16
    {
17
        if ($path !== $origin) {
18
            $slash = self::getTrailingSlashes($origin);
19
            return rtrim($path, '/') . $slash;
20
        }
21
22
        return $path;
23
    }
24
25
    /**
26
     * Get trailing slashes
27
     *
28
     * @param string $path
29
     *
30
     * @return string
31
     */
32
    public static function getTrailingSlashes($path)
33
    {
34
        $slash = '';
35
        $offset = -1;
36
        $len = strlen($path);
37
        while ($len >= abs($offset) && substr($path, $offset, 1) === '/') {
38
            $slash .= '/';
39
            $offset -= 1;
40
        }
41
42
        return $slash;
43
    }
44
}
45