Completed
Push — master ( afe7e9...2d02b0 )
by Thibaud
02:35
created

PathUtil::extractExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of alchemy/resource-component.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Resource;
13
14
final class PathUtil
15
{
16
    /**
17
     * Extracts the file basename from a file path
18
     *
19
     * @param string $path File path
20
     * @return string The file basename or the input value if it has no path component.
21
     */
22 24
    public static function basename($path)
23
    {
24 24
        return (false === $pos = strrpos(strtr($path, '\\', '/'), '/')) ? $path : substr($path, $pos + 1);
25
    }
26
27
    /**
28
     * Sanitizes an extension.
29
     *
30
     * Strips dot from the beginning, converts to lowercase and remove trailing
31
     * whitespaces
32
     *
33
     * @param string $extension
34
     * @return string The sanitized extension
35
     */
36 28
    public static function sanitizeExtension($extension)
37
    {
38 28
        return ltrim(trim(mb_strtolower($extension)), '.');
39
    }
40
41
    /**
42
     * Returns the extension of a file
43
     *
44
     * @param string $path
45
     * @return string
46
     */
47 28
    public static function extractExtension($path)
48
    {
49 28
        return self::sanitizeExtension(pathinfo($path, PATHINFO_EXTENSION));
50
    }
51
}
52