Passed
Push — master ( 5f9c86...827258 )
by Carlos C
05:51
created

tempname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Utils;
6
7
use RuntimeException;
8
9
function array_rtrim(array $input): array
10
{
11
    $count = count($input);
12
    while ($count > 0 && '' === (string) $input[$count - 1]) {
13
        array_pop($input);
14
        $count = $count - 1;
15
    }
16
17
    return $input;
18
}
19
20
/**
21
 * @param string $dir
22
 * @param string $prefix
23
 * @return string
24
 * @throws RuntimeException Cannot create a temporary file name
25
 */
26
function tempname($dir = '', $prefix = ''): string
27
{
28
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
29
    $tempname = @tempnam($dir, $prefix);
30
    if (false === $tempname) {
31
        throw new RuntimeException('Cannot create a temporary file name');
32
    }
33
    return $tempname;
34
}
35
36
/**
37
 * @param string $dir
38
 * @param string $prefix
39
 * @return string
40
 * @throws RuntimeException Cannot create a temporary file name
41
 */
42
function tempdir(string $dir = '', string $prefix = ''): string
43
{
44
    $dirname = tempname($dir, $prefix);
45
    if (file_exists($dirname)) {
46
        unlink($dirname);
47
    }
48
    mkdir($dirname);
49
    return $dirname;
50
}
51
52
function preg_is_valid(string $input): bool
53
{
54
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
55
    return (false !== @preg_match('/' . $input . '/', ''));
56
}
57
58
function file_extension(string $filename): string
59
{
60
    return substr(strrchr(basename($filename), '.') ?: '', 1) ?: '';
61
}
62
63
function file_extension_replace(string $filename, string $extension): string
64
{
65
    $current = file_extension($filename);
66
    if ('' === $current) {
67
        $dot = '.';
68
        if ('.' === substr($filename, -1)) {
69
            $dot = '';
70
        }
71
        return $filename . $dot . $extension;
72
    }
73
    return substr($filename, 0, - (strlen($current) + 1)) . '.' . $extension;
74
}
75