Conditions | 17 |
Paths | 16 |
Total Lines | 94 |
Code Lines | 76 |
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 |
||
36 | public function parse($content) |
||
37 | { |
||
38 | $comment = ''; |
||
39 | $first = true; |
||
40 | $desc = ''; |
||
41 | $currentTask = null; |
||
42 | |||
43 | $state = 'root'; |
||
44 | foreach (explode("\n", $content) as $i => $line) { |
||
45 | $m = []; |
||
46 | $match = function ($regexp) use ($line, &$m) { |
||
47 | return preg_match("#$regexp#", $line, $m); |
||
48 | }; |
||
49 | switch ($state) { |
||
50 | case 'root': |
||
51 | if ($match('^/\*\*?')) { |
||
52 | $state = 'comment'; |
||
53 | $comment .= trimComment($line) . "\n"; |
||
54 | break; |
||
55 | } |
||
56 | if ($match('^//')) { |
||
57 | $comment .= trimComment($line) . "\n"; |
||
58 | break; |
||
59 | } |
||
60 | if ($match('^require.+?[\'"](?<recipe>.+?)[\'"]')) { |
||
61 | $this->require[] = dirname($this->recipePath) . $m['recipe']; |
||
62 | break; |
||
63 | } |
||
64 | if ($match('^set\([\'"](?<config_name>[\w_:]+?)[\'"]')) { |
||
65 | $set = new DocConfig(); |
||
66 | $set->name = $m['config_name']; |
||
67 | $set->comment = trim($comment); |
||
68 | $comment = ''; |
||
69 | $set->recipePath = $this->recipePath; |
||
70 | $set->lineNumber = $i + 1; |
||
71 | $this->config[$set->name] = $set; |
||
72 | break; |
||
73 | } |
||
74 | if ($match('^desc\([\'"](?<desc>.+?)[\'"]\);$')) { |
||
75 | $desc = $m['desc']; |
||
76 | break; |
||
77 | } |
||
78 | if ($match('^task\([\'"](?<task_name>[\w_:]+?)[\'"],\s\[$')) { |
||
79 | $task = new DocTask(); |
||
80 | $task->name = $m['task_name']; |
||
81 | $task->desc = $desc; |
||
82 | $task->comment = trim($comment); |
||
83 | $comment = ''; |
||
84 | $task->group = []; |
||
85 | $task->recipePath = $this->recipePath; |
||
86 | $task->lineNumber = $i + 1; |
||
87 | $this->tasks[$task->name] = $task; |
||
88 | $state = 'group_task'; |
||
89 | $currentTask = $task; |
||
90 | break; |
||
91 | } |
||
92 | if ($match('^task\([\'"](?<task_name>[\w_:]+?)[\'"]')) { |
||
93 | $task = new DocTask(); |
||
94 | $task->name = $m['task_name']; |
||
95 | $task->desc = $desc; |
||
96 | $task->comment = trim($comment); |
||
97 | $comment = ''; |
||
98 | $task->recipePath = $this->recipePath; |
||
99 | $task->lineNumber = $i + 1; |
||
100 | $this->tasks[$task->name] = $task; |
||
101 | break; |
||
102 | } |
||
103 | if ($match('^<\?php')) { |
||
104 | break; |
||
105 | } |
||
106 | |||
107 | $desc = ''; |
||
108 | if($first && $comment !== '') { |
||
109 | $this->comment = $comment; |
||
110 | } |
||
111 | $first = false; |
||
112 | $comment = ''; |
||
113 | break; |
||
114 | |||
115 | case 'comment': |
||
116 | if ($match('\*/\s*$')) { |
||
117 | $state = 'root'; |
||
118 | break; |
||
119 | } |
||
120 | $comment .= trimComment($line) . "\n"; |
||
121 | break; |
||
122 | |||
123 | case 'group_task': |
||
124 | if ($match('^\s+\'(?<task_name>[\w_:]+?)\',$')) { |
||
125 | $currentTask->group[] = $m['task_name']; |
||
126 | break; |
||
127 | } |
||
128 | $state = 'root'; |
||
129 | break; |
||
130 | } |
||
134 |