removeFileExtensionAndKeepQueryString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
ccs 10
cts 11
cp 0.9091
cc 3
eloc 12
nc 3
nop 1
crap 3.0067
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