Completed
Push — master ( 578197...422f48 )
by Daniel
02:50
created

Arr::getFunction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 8
nc 4
nop 3
1
<?php
2
namespace Narrowspark\Arr;
3
4
use BadMethodCallException;
5
use ReflectionClass;
6
use ReflectionMethod;
7
use RuntimeException;
8
9
class Arr
10
{
11
    /**
12
      * A mapping of method names to the numbers of arguments it accepts. Each
13
      * should be two more than the equivalent method.
14
      *
15
      * @var string[]
16
      */
17
     protected $classes = [
18
        Access::class,
19
        Enumerator::class,
20
        Transform::class,
21
        Traverse::class,
22
    ];
23
24
    protected $methodArgs = null;
25
26
    /**
27
     * Invokes the given method with the rest of the passed arguments.
28
     * The result is not cast, so the return value may be of type Arr, array,
29
     * integer, boolean, etc.
30
     *
31
     * @param string  $name
32
     * @param mixed[] $args
33
     *
34
     * @throws \BadMethodCallException
35
     * @throws \RuntimeException
36
     *
37
     * @return mixed
38
     */
39
    public function __call($name, $args)
40
    {
41
        $this->getMethodArgs($name);
42
43
        if (!isset($this->methodArgs[$name])) {
44
            throw new BadMethodCallException(sprintf('%s is not a valid method.', $name));
45
        }
46
47
        // if (count($args) < 2) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
        //     throw new RuntimeException(
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
        //         sprintf(
50
        //             '%s counted arguments dont match needed arguments %s for function %s.',
51
        //             count($args),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        //             yy
53
        //             $name
54
        //         )
55
        //     );
56
        // }
57
58
        foreach ($this->classes as $class) {
59
            if (method_exists($class, $name)) {
60
                return call_user_func_array([new $class(), $name], $args);
61
            }
62
        }
63
    }
64
65
    /**
66
     * Get all methods arguments.
67
     *
68
     * @param string $name
69
     */
70
    protected function getMethodArgs($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        if (!$this->methodArgs) {
73
            foreach ($this->classes as $classInterface) {
74
                $class = new ReflectionClass($classInterface);
75
                $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
76
77
                foreach ($methods as $method) {
78
                    $params = $method->getNumberOfParameters();
79
                    $this->methodArgs[$method->name] = $params;
80
                }
81
            }
82
        }
83
    }
84
}
85