FunctionCallTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasPlaceholder() 0 3 1
A replacePlaceholderWithValue() 0 5 2
A prepareArgs() 0 3 2
A addValueAsFirstArg() 0 4 1
1
<?php
2
3
/**
4
 * FunctionCallTrait
5
 *
6
 * @package php-deferred-callchain
7
 * @author  Daniel S. Deboer
8
 * @author  Jean Claveau
9
 */
10
namespace JClaveau\Async;
11
12
/**
13
 * This trait is an almost pure copy of the the argument handling of the 
14
 * Pipe class https://github.com/danielsdeboer/pipe.
15
 */
16
trait FunctionCallTrait
17
{
18
    /**
19
     * @var string $placeholder The value that will be replaced by the chain subject
20
     */
21
    protected $placeholder = '$$';
22
    /**
23
     * Prepare the arguments list.
24
     * @param array $args
25
     * @param mixed $value
26
     * @return array
27
     */
28
    protected function prepareArgs(array $args, $value)
29
    {
30
        return $this->hasPlaceholder($args) ? $this->replacePlaceholderWithValue($args, $value) : $this->addValueAsFirstArg($args, $value);
31
    }
32
    /**
33
     * Check if an array contains an element matching the placeholder.
34
     * @param array $args
35
     * @return bool
36
     */
37
    protected function hasPlaceholder(array $args)
38
    {
39
        return in_array($this->placeholder, $args, true);
40
    }
41
    /**
42
     * Add the value as the first argument in the arguments list.
43
     * @param array $args
44
     * @param mixed $value
45
     * @return array
46
     */
47
    protected function addValueAsFirstArg(array $args, $value)
48
    {
49
        array_unshift($args, $value);
50
        return $args;
51
    }
52
    /**
53
     * Replace any occurrence of the placeholder with the value.
54
     * @param array $args
55
     * @param mixed $value
56
     * @return array
57
     */
58
    protected function replacePlaceholderWithValue(array $args, $value)
59
    {
60
        return array_map(function ($arg) use($value) {
61
            return $arg === $this->placeholder ? $value : $arg;
62
        }, $args);
63
    }
64
}