Conditions | 10 |
Paths | 63 |
Total Lines | 52 |
Code Lines | 30 |
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 |
||
30 | protected function parse() { |
||
31 | $jsonString = str_replace('//OK', '', $this->getHtmlResponse()); |
||
32 | $array = json_decode($jsonString); |
||
|
|||
33 | |||
34 | $payload = null; |
||
35 | //Find the interesting part |
||
36 | foreach ($array as $item) { |
||
37 | if (is_array($item)) { |
||
38 | $payload = $item; |
||
39 | break; |
||
40 | } |
||
41 | } |
||
42 | if (null === $payload) { |
||
43 | throw new \Exception('unable to parse payload from ' . $jsonString); |
||
44 | } |
||
45 | |||
46 | //Remove the Javastuff |
||
47 | foreach ($payload as $key => $item) { |
||
48 | if (preg_match('/de.swm.mvglive.gwt.client.newsticker.NewstickerItem/', $item)) { |
||
49 | unset($payload[$key]); |
||
50 | } |
||
51 | if (preg_match('/java.util.ArrayList/', $item)) { |
||
52 | unset($payload[$key]); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | if (0 != (count($payload) % 2)) { |
||
57 | throw new \Exception('Item count is odd! ' . $jsonString); |
||
58 | |||
59 | } |
||
60 | $object = new \stdClass(); |
||
61 | foreach ($payload as $key => $item) { |
||
62 | if (0 == ($key % 2)) { |
||
63 | $object = new \stdClass(); |
||
64 | $object->lines = $item; |
||
65 | |||
66 | //get Lines with problems |
||
67 | $stringToAnalyze = trim(str_replace('Linie(n)', '', $item)); |
||
68 | $linesString = explode(':', $stringToAnalyze)[0]; |
||
69 | $object->affectedLines = explode(',', $linesString); |
||
70 | array_walk($object->affectedLines, function (&$item) { |
||
71 | $item = trim($item); |
||
72 | }); |
||
73 | |||
74 | } else { |
||
75 | $object->messages = $item; |
||
76 | $this->interferences[] = $object; |
||
77 | } |
||
78 | |||
79 | } |
||
80 | |||
81 | } |
||
82 | |||
86 | } |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.