Passed
Push — master ( d3a94c...76e109 )
by Valentin
05:50
created

Utils::shortName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise;
5
6
class Utils
7
{
8
    /**
9
     * Create short name (without namespaces).
10
     *
11
     * @param string $name
12
     *
13
     * @return string
14
     */
15
    public static function shortName(string $name): string
16
    {
17
        $pos = mb_strrpos($name, '\\');
18
        if ($pos === false) {
19
            return $name;
20
        }
21
22
        return mb_substr($name, $pos + 1);
23
    }
24
25
    /**
26
     * Inject values to array at given index.
27
     *
28
     * @param array $stmts
29
     * @param int   $index
30
     * @param array $child
31
     *
32
     * @return array
33
     */
34
    public static function injectValues(array $stmts, int $index, array $child): array
35
    {
36
        $before = array_slice($stmts, 0, $index);
37
        $after = array_slice($stmts, $index);
38
39
        return array_merge($before, $child, $after);
40
    }
41
42
    /**
43
     * Remove trailing digits in the given name.
44
     *
45
     * @param string $name
46
     * @param int    $number
47
     *
48
     * @return string
49
     */
50
    public static function trimTrailingDigits(string $name, int $number): string
51
    {
52
        $pos = mb_strripos($name, (string)$number);
53
        if ($pos === false) {
54
            return $name;
55
        }
56
57
        return mb_substr($name, 0, $pos);
58
    }
59
}