PathHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 42
rs 10
ccs 13
cts 14
cp 0.9286

2 Methods

Rating   Name   Duplication   Size   Complexity  
A arePathsEqualIgnoringFileExtension() 0 5 1
A removeFileExtensionAndKeepQueryString() 0 19 3
1
<?php
2
3
namespace Partnermarketing\FileSystemBundle\Utility;
4
5
class PathHelper
6
{
7
    /**
8
     * Return whether two paths are equal, ignore their file extension.
9
     *
10
     * @param  string $path1
11
     * @param  string $path2
12
     * @return string
13
     */
14 1
    public static function arePathsEqualIgnoringFileExtension($path1, $path2)
15
    {
16 1
        return self::removeFileExtensionAndKeepQueryString($path1)
17 1
            === self::removeFileExtensionAndKeepQueryString($path2);
18
    }
19
20
    /**
21
     * Remove the file extension from the given path. The query string, if there is one, will be
22
     * preserved.
23
     *
24
     * @param  string $path E.g. http://example/path/file.jpg?foo=bar
25
     * @return string       E.g. http://example/path/file?foo=bar
26
     */
27 6
    public static function removeFileExtensionAndKeepQueryString($path)
28
    {
29 6
        if (strpos($path, '.') === false) {
30
            return $path;
31
        }
32
33 6
        $pathParts = explode('.', $path);
34 6
        $extensionAndQueryString = $pathParts[count($pathParts) - 1];
35 6
        $extensionAndQueryStringParts = explode('?', $extensionAndQueryString, 2);
36 6
        unset($pathParts[count($pathParts) - 1]);
37
38 6
        $withoutExtensionAndWithoutQueryString = implode('.', $pathParts);
39
40 6
        if (count($extensionAndQueryStringParts) === 2) {
41 3
            return $withoutExtensionAndWithoutQueryString . '?' . $extensionAndQueryStringParts[1];
42
        } else {
43 3
            return $withoutExtensionAndWithoutQueryString;
44
        }
45
    }
46
}
47