Completed
Push — master ( 05eb07...368407 )
by Patrick
03:00
created

class.Provider.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 boolean|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
    /**
95
     * Calls the indicated function on the specified method or all methods if false
96
     *
97
     * @param string|boolean $methodName The method to call the function on, or false to call on all functions
98
     * @param string $functionName The function to call
99
     * @param array $args The arguments for the function
100
     * @param boolean|string $checkField A field to check if it is set a certain way before calling the function
101
     * @param mixed $checkValue The value that field should be set to to not call the function
102
     * @param callable $resFunction Function to call on the result, otherwise the function will return on the first non-false result
103
     *
104
     * @return mixed The return value
105
     */
106
    protected function callFunction($methodName, $functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
107
    {
108
        if($methodName === false)
109
        {
110
            return $this->callOnEach($functionName, $args, $checkField, $checkValue, $resFunction);
111
        }
112
        $method = $this->getMethodByName($methodName);
0 ignored issues
show
It seems like $methodName defined by parameter $methodName on line 106 can also be of type boolean; however, Provider::getMethodByName() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
113
        return call_user_func_array(array($method, $functionName), $args);
114
    }
115
}
116
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
117