Conditions | 19 |
Paths | 364 |
Total Lines | 109 |
Code Lines | 71 |
Lines | 109 |
Ratio | 100 % |
Changes | 0 |
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 |
||
85 | View Code Duplication | public function silentTest($test_obj = null) |
|
86 | { |
||
87 | if (!$test_obj) { |
||
88 | $test_obj = $this; |
||
89 | } |
||
90 | |||
91 | $total = $success = $fail = $exception = 0; |
||
92 | $speed = $memory = 0; |
||
93 | |||
94 | $tested = array(); |
||
95 | $failures = array(); |
||
96 | foreach (get_class_methods($test_obj) as $method) { |
||
97 | if (strpos($method, 'test_') === 0) { |
||
98 | $m = substr($method, 5); |
||
99 | } elseif (strpos($method, 'prepare_') === 0) { |
||
100 | $m = substr($method, 8); |
||
101 | } else { |
||
102 | continue; |
||
103 | } |
||
104 | if ($tested[$m]) { |
||
105 | continue; |
||
106 | } |
||
107 | $tested[$m] = true; |
||
108 | |||
109 | foreach ($this->variances as $key => $vari) { |
||
110 | if (is_numeric($key)) { |
||
111 | $key = $vari; |
||
112 | } |
||
113 | |||
114 | // Input is a result of preparation function |
||
115 | try { |
||
116 | if (method_exists($test_obj, 'prepare_'.$m)) { |
||
117 | $input = $test_obj->{'prepare_'.$m}($vari, $method); |
||
118 | } else { |
||
119 | if (($test_obj instanceof AbstractObject && $test_obj->hasMethod('prepare')) |
||
120 | || method_exists($test_obj, 'prepare') |
||
121 | ) { |
||
122 | $input = $test_obj->prepare($vari, $method); |
||
123 | } else { |
||
124 | $input = null; |
||
125 | } |
||
126 | } |
||
127 | } catch (Exception $e) { |
||
128 | if ($e instanceof Exception_SkipTests) { |
||
129 | return array( |
||
130 | 'skipped' => $e->getMessage(), |
||
131 | ); |
||
132 | } |
||
133 | throw $e; |
||
134 | } |
||
135 | |||
136 | $this->input = $input; |
||
137 | |||
138 | $test_func = method_exists($test_obj, 'test_'.$m) ? |
||
139 | 'test_'.$m : 'test'; |
||
140 | |||
141 | ++$total; |
||
142 | |||
143 | $me = memory_get_peak_usage(); |
||
144 | $ms = microtime(true); |
||
145 | |||
146 | $this->cnt = 0; |
||
147 | declare (ticks = 1); |
||
148 | register_tick_function(array($this, 'ticker')); |
||
149 | |||
150 | try { |
||
151 | $result = $this->executeTest($test_obj, $test_func, $input); |
||
152 | $ms = microtime(true) - $ms; |
||
153 | $me = ($mend = memory_get_peak_usage()) - $me; |
||
154 | |||
155 | $result = $this->formatResult($row, $key, $result); |
||
156 | |||
157 | $k = $key.'_'.$m; |
||
158 | if ($this->proper_responses[$k] == $result && isset($this->proper_responses[$k])) { |
||
159 | ++$success; |
||
160 | } else { |
||
161 | $failures[] = $method; |
||
162 | ++$fail; |
||
163 | } |
||
164 | } catch (Exception $e) { |
||
165 | if ($e instanceof Exception_SkipTests) { |
||
166 | return array( |
||
167 | 'skipped' => $e->getMessage(), |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | ++$exception; |
||
172 | |||
173 | //$ms=microtime(true)-$ms; |
||
174 | $me = ($mend = memory_get_peak_usage()) - $me; |
||
175 | } |
||
176 | |||
177 | unregister_tick_function(array($this, 'ticker')); |
||
178 | |||
179 | $speed += $this->cnt * 1; |
||
180 | $memory += $me; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | return array( |
||
185 | 'total' => $total, |
||
186 | 'failures' => $failures, |
||
187 | 'success' => $success, |
||
188 | 'exception' => $exception, |
||
189 | 'fail' => $fail, |
||
190 | 'speed' => $speed, |
||
191 | 'memory' => $memory, |
||
192 | ); |
||
193 | } |
||
194 | |||
365 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: