Completed
Pull Request — master (#5)
by Valentin
03:04
created

utils.php ➔ shortName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
declare(strict_types=1);
10
11
namespace Cycle\ORM\Promise;
12
13
/**
14
 * Inject values to array at given index.
15
 *
16
 * @param array $stmts
17
 * @param int   $index
18
 * @param array $values
19
 * @return array
20
 */
21
function injectValues(array $stmts, int $index, array $values): array
22
{
23
    $before = array_slice($stmts, 0, $index);
24
    $after = array_slice($stmts, $index);
25
26
    return array_merge($before, $values, $after);
27
}
28
29
/**
30
 * Remove trailing digits in the given name.
31
 *
32
 * @param string $name
33
 * @param int    $number
34
 * @return string
35
 */
36
function trimTrailingDigits(string $name, int $number): string
37
{
38
    $pos = mb_strripos($name, (string)$number);
39
    if ($pos === false) {
40
        return $name;
41
    }
42
43
    return mb_substr($name, 0, $pos);
44
}
45
46
/**
47
 * Remove any kinds of php open tags.
48
 *
49
 * @param string $code
50
 * @return string
51
 */
52
function trimPHPOpenTag(string $code): string
53
{
54
    if (mb_strpos($code, '<?php') === 0) {
55
        return mb_substr($code, 5);
56
    }
57
58
    if (mb_strpos($code, '<?') === 0) {
59
        return mb_substr($code, 2);
60
    }
61
62
    return $code;
63
}
64
65
66
/**
67
 * Create short name (without namespaces).
68
 *
69
 * @param string $name
70
 * @return string
71
 */
72
function shortName(string $name): string
73
{
74
    $pos = mb_strrpos($name, '\\');
75
    if ($pos === false) {
76
        return $name;
77
    }
78
79
    return mb_substr($name, $pos + 1);
80
}
81