Conditions | 17 |
Paths | 20 |
Total Lines | 80 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace Myth\Bay; |
||
53 | public function display($library, $params=null, $cache_name=null, $cache_ttl=0) |
||
54 | { |
||
55 | list($class, $method) = $this->determineClass($library); |
||
56 | |||
57 | // Is it cached? |
||
58 | $cache_name = ! empty($cache_name) ? $cache_name : $class . $method . md5(serialize($params)); |
||
59 | |||
60 | if (! empty($this->cache) && $output = $this->cache->get($cache_name)) |
||
61 | { |
||
62 | return $output; |
||
63 | } |
||
64 | |||
65 | // Not cached - so grab it... |
||
66 | $instance = new $class(); |
||
67 | |||
68 | if (!method_exists($instance, $method)) |
||
69 | { |
||
70 | throw new \InvalidArgumentException("{$class}::{$method} is not a valid method."); |
||
71 | } |
||
72 | |||
73 | $params_array = $this->prepareParams($params); |
||
74 | $ref_method = new \ReflectionMethod($instance, $method); |
||
75 | $num_of_params = $ref_method->getNumberOfParameters(); |
||
76 | $ref_params = $ref_method->getParameters(); |
||
77 | |||
78 | if ($num_of_params === 0) |
||
79 | { |
||
80 | if ($params_array !== null) |
||
81 | { |
||
82 | throw new \InvalidArgumentException("{$class}::{$method} has no params."); |
||
83 | } |
||
84 | |||
85 | $output = $instance->{$method}(); |
||
86 | } |
||
87 | elseif ( |
||
88 | ($num_of_params === 1) |
||
89 | && ( |
||
90 | (! array_key_exists($ref_params[0]->name, $params_array)) |
||
91 | || ( |
||
92 | array_key_exists($ref_params[0]->name, $params_array) |
||
93 | && count($params_array) !== 1 |
||
94 | ) |
||
95 | ) |
||
96 | ) |
||
97 | { |
||
98 | $output = $instance->{$method}($params_array); |
||
99 | } |
||
100 | else |
||
101 | { |
||
102 | $fire_args = []; |
||
103 | $method_params = []; |
||
104 | |||
105 | foreach($ref_params as $arg) |
||
106 | { |
||
107 | $method_params[$arg->name] = true; |
||
108 | if (array_key_exists($arg->name, $params_array)) |
||
109 | { |
||
110 | $fire_args[$arg->name] = $params_array[$arg->name]; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | foreach ($params_array as $key => $val) |
||
|
|||
115 | { |
||
116 | if (! isset($method_params[$key])) |
||
117 | { |
||
118 | throw new \InvalidArgumentException("{$key} is not a valid param name."); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $output = call_user_func_array([$instance, $method], $fire_args); |
||
123 | } |
||
124 | |||
125 | // Can we cache it? |
||
126 | if (! empty($this->cache) && $cache_ttl !== 0) |
||
127 | { |
||
128 | $this->cache->set($cache_name, $output, $cache_ttl); |
||
129 | } |
||
130 | |||
131 | return $output; |
||
132 | } |
||
133 | |||
252 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.