Completed
Push — master ( a9a146...85a956 )
by Jean
02:11
created

FunctionCallTrait::replacePlaceholderWithValue()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 1
nop 2
1
<?php
2
/**
3
 * FunctionCallTrait
4
 *
5
 * @package php-deferred-callchain
6
 * @author  Daniel S. Deboer
7
 * 
8
 */
9
namespace JClaveau\Async;
10
11
/**
12
 * This trait is a pure copy of the the argument handling of the Pipe
13
 * class https://github.com/danielsdeboer/pipe.
14
 */
15
trait FunctionCallTrait
16
{
17
    protected $placeholder;
18
    
19
    /**
20
     * Prepare the arguments list.
21
     * @param array $args
22
     * @param mixed $value
23
     * @return array
24
     */
25
    // protected function prepareArgs (array $args) : array
26
    protected function prepareArgs (array $args, $value)
27
    {
28
        return $this->hasPlaceholder($args)
29
            ? $this->replacePlaceholderWithValue($args, $value)
30
            : $this->addValueAsFirstArg($args, $value);
31
    }
32
33
    /**
34
     * Check if an array contains an element matching the placeholder.
35
     * @param array $args
36
     * @return bool
37
     */
38
    // protected function hasPlaceholder (array $args) : bool
39
    protected function hasPlaceholder (array $args)
40
    {
41
        return in_array($this->placeholder, $args, true);
42
    }
43
44
    /**
45
     * Add the value as the first argument in the arguments list.
46
     * @param array $args
47
     * @return array
48
     */
49
    // protected function addValueAsFirstArg (array $args) : array
50
    protected function addValueAsFirstArg (array $args, $value)
51
    {
52
        array_unshift($args, $value);
53
54
        return $args;
55
    }
56
57
    /**
58
     * Replace any occurrence of the placeholder with the value.
59
     * @param array $args
60
     * @param mixed $value
61
     * @return array
62
     */
63
    protected function replacePlaceholderWithValue (array $args, $value)
64
    // protected function replacePlaceholderWithValue (array $args) : array
65
    {
66
        return array_map([$this, function ($arg) use ($value) {
67
            return $arg === $this->placeholder
68
                ? $value
69
                : $arg;
70
        }], [$args]);
71
    }
72
73
    /**/
74
}
75