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) { |
|
|
|
|
48
|
|
|
// throw new RuntimeException( |
|
|
|
|
49
|
|
|
// sprintf( |
50
|
|
|
// '%s counted arguments dont match needed arguments %s for function %s.', |
51
|
|
|
// count($args), |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.