Passed
Branch 6.0 (c154c1)
by Olivier
11:35
created

ExtensionResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 19
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A assert_extension() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[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 ICanBoogie\Render;
13
14
use LogicException;
15
16
use function pathinfo;
17
18
use const PATHINFO_EXTENSION;
19
20
final class ExtensionResolver
21
{
22
    public const EXTENSION_DOT = '.';
23
24
    public static function resolve(string $pathname): ?string
25
    {
26
        $extension = pathinfo($pathname, PATHINFO_EXTENSION);
27
28
        if (!$extension) {
29
            return null;
30
        }
31
32
        return self::EXTENSION_DOT . $extension;
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        return self::EXTENSION_DOT . /** @scrutinizer ignore-type */ $extension;
Loading history...
33
    }
34
35
    public static function assert_extension(string $extension): void
36
    {
37
        if ($extension[0] !== self::EXTENSION_DOT) {
38
            throw new LogicException("Extension must start with a dot: $extension.");
39
        }
40
    }
41
}
42