MethodCall   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 2
cbo 2
dl 0
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A replace() 0 12 4
A add() 0 8 2
A remove() 0 4 1
A has() 0 4 1
A index() 0 10 3
1
<?php
2
/*
3
 * This file is part of the Borobudur-DependencyInjection package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\DependencyInjection\Definition;
12
13
use Borobudur\DependencyInjection\Exception\InvalidArgumentException;
14
use Borobudur\DependencyInjection\NumericParameterTrait;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/8/15
19
 */
20
class MethodCall
21
{
22
    use NumericParameterTrait {
23
        NumericParameterTrait::add as protected addMethod;
24
        NumericParameterTrait::remove as protected removeMethod;
25
        NumericParameterTrait::set as protected;
26
    }
27
28
    /**
29
     * Clear and replace with sets of method call.
30
     *
31
     * @param array $calls
32
     *
33
     * @return static
34
     */
35
    public function replace(array $calls)
36
    {
37
        $this->clear();
38
        foreach($calls as $call) {
39
            if (is_array($call)) {
40
                $args = isset($call[1]) ? $call[1] : array();
41
                $this->add($call[0], $args);
42
            }
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Add method call.
50
     *
51
     * @param string $method
52
     * @param array  $arguments
53
     *
54
     * @return static
55
     */
56
    public function add($method, array $arguments = array())
57
    {
58
        if (empty($method)) {
59
            throw new InvalidArgumentException('Method name cannot be empty.');
60
        }
61
62
        return $this->addMethod(array($method, $arguments));
63
    }
64
65
    /**
66
     * Remove method call.
67
     *
68
     * @param int $method
69
     *
70
     * @return static
71
     */
72
    public function remove($method)
73
    {
74
        return $this->removeMethod($this->index($method));
75
    }
76
77
    /**
78
     * Check if has method call.
79
     *
80
     * @param string $method
81
     *
82
     * @return bool
83
     */
84
    public function has($method)
85
    {
86
        return -1 !== $this->index($method);
87
    }
88
89
    /**
90
     * Get index of method.
91
     *
92
     * @param string $method
93
     *
94
     * @return int
95
     */
96
    public function index($method)
97
    {
98
        foreach ($this->all() as $i => $call) {
99
            if ($call[0] === $method) {
100
                return $i;
101
            }
102
        }
103
104
        return -1;
105
    }
106
}
107