Passed
Push — master ( 69c271...cb615c )
by Valentin
07:24
created

injector.php ➔ injectValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 7
rs 10
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
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise;
13
14
use PhpParser\Node;
15
16
/**
17
 * @param Node\Stmt[] $stmts $stmts
18
 * @param string      $target
19
 * @param Node\Stmt[] $injection
20
 * @return Node\Stmt[]
21
 */
22
function inject(array $stmts, string $target, array $injection): array
23
{
24
    return injectValues($stmts, injectionID($stmts, $target), $injection);
25
}
26
27
/**
28
 * Inject values to array at given index.
29
 *
30
 * @param array $stmts
31
 * @param int   $index
32
 * @param array $values
33
 * @return array
34
 */
35
function injectValues(array $stmts, int $index, array $values): array
36
{
37
    $before = array_slice($stmts, 0, $index);
38
    $after = array_slice($stmts, $index);
39
40
    return array_merge($before, $values, $after);
41
}
42
43
/**
44
 * @param Node\Stmt[] $stmts
45
 * @param string      $target
46
 * @return int
47
 * @internal
48
 */
49
function injectionID(array $stmts, string $target): int
50
{
51
    foreach ($stmts as $index => $child) {
52
        if ($child instanceof $target) {
53
            return $index;
54
        }
55
    }
56
57
    return 0;
58
}
59