Conditions | 10 |
Paths | 10 |
Total Lines | 45 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
10 | public function getValueByArray(array $params) |
||
11 | { |
||
12 | $data = &$this; |
||
13 | $total = count($params); |
||
14 | $key = current($params); |
||
15 | |||
16 | if (0 === $total) { |
||
17 | throw new Exception('requires at least 1 arg'); |
||
18 | } |
||
19 | |||
20 | if (null === $key) { |
||
21 | throw new Exception('requires non NULL args'); |
||
22 | } |
||
23 | if (!is_scalar($key)) { |
||
24 | throw new Exception('requires scalar args'); |
||
25 | } |
||
26 | if (!isset($data[$key])) { |
||
27 | return null; |
||
28 | } |
||
29 | |||
30 | if (1 === $total) { |
||
31 | return $data[$key]; |
||
32 | } |
||
33 | |||
34 | $data = &$data[$key]; |
||
35 | $args = array_slice($params, 1); |
||
36 | |||
37 | $count = 0; |
||
38 | foreach ($args as $key) { |
||
39 | if ($count++ > 100) { |
||
40 | exit(); |
||
|
|||
41 | } |
||
42 | |||
43 | if (is_array($data)) { |
||
44 | if (!isset($data[$key])) { |
||
45 | return null; |
||
46 | } else { |
||
47 | $data = &$data[$key]; |
||
48 | } |
||
49 | } else { |
||
50 | return null; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return $data; |
||
55 | } |
||
62 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.