Completed
Branch master (3ee61a)
by Patrick
03:04
created

Provider   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 101
rs 10
wmc 15
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethodByName() 0 12 3
A addFromEach() 0 18 4
B callOnEach() 0 26 6
A callFunction() 0 9 2
1
<?php
2
3
class Provider extends Singleton
4
{
5
    /** The methods loaded by the provider */
6
    protected $methods;
7
8
    /**
9
     * Get the method object corresponding to the name
10
     *
11
     * @param string $methodName The class name of the method to get the instance for
12
     *
13
     * @return boolean|stdClass The specified method class instance or false if it is not loaded
14
     */
15
    public function getMethodByName($methodName)
16
    {
17
        $count = count($this->methods);
18
        for($i = 0; $i < $count; $i++)
19
        {
20
            if(strcasecmp(get_class($this->methods[$i]), $methodName) === 0)
21
            {
22
                return $this->methods[$i];
23
            }
24
        }
25
        return false;
26
    }
27
28
    /**
29
     * Calls the indicated function on each method and add the result
30
     *
31
     * @param string $functionName The function to call
32
     * @param string $checkField A field to check if it is set a certain way before calling the function
33
     * @param mixed $checkValue The value that field should be set to to not call the function
34
     *
35
     * @return integer The added returnValue
36
     */
37
    protected function addFromEach($functionName, $checkField = false, $checkValue = false)
38
    {
39
        $retCount = 0;
40
        $count = count($this->methods);
41
        for($i = 0; $i < $count; $i++)
42
        {
43
            if($checkField !== false)
44
            {
45
                if($this->methods[$i]->{$checkField} === $checkValue)
46
                {
47
                    continue;
48
                }
49
            }
50
            $res = call_user_func(array($this->methods[$i], $functionName));
51
            $retCount += $res;
52
        }
53
        return $retCount;
54
    }
55
56
    /**
57
     * Calls the indicated function on each method
58
     *
59
     * @param string $functionName The function to call
60
     * @param array $args The arguments for the function
61
     * @param string $checkField A field to check if it is set a certain way before calling the function
62
     * @param mixed $checkValue The value that field should be set to to not call the function
63
     * @param callable $resFunction Function to call on the result, otherwise the function will return on the first non-false result
64
     *
65
     * @return Auth\Group|Auth\User|false The merged returnValue
66
     */
67
    protected function callOnEach($functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
68
    {
69
        $ret = false;
70
        $count = count($this->methods);
71
        for($i = 0; $i < $count; $i++)
72
        {
73
            if($checkField !== false)
74
            {
75
                if($this->methods[$i]->{$checkField} === $checkValue)
76
                {
77
                    continue;
78
                }
79
            }
80
            $res = call_user_func_array(array($this->methods[$i], $functionName), $args);
81
            if($resFunction !== null)
82
            {
83
                call_user_func($resFunction, $ret, $res);
84
                continue;
85
            }
86
            if($res !== false)
87
            {
88
                return $res;
89
            }
90
        }
91
        return $ret;
92
    }
93
94
    protected function callFunction($methodName, $functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
95
    {
96
        if($methodName === false)
97
        {
98
            return $this->callOnEach($functionName, $args, $checkField, $checkValue, $resFunction);
0 ignored issues
show
Documentation introduced by
$checkField is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
        }
100
        $method = $this->getMethodByName($methodName);
101
        return call_user_func_array(array($method, $functionName), $args);
102
    }
103
}
104
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
105