Conditions | 25 |
Paths | 32 |
Total Lines | 134 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
49 | public function parse(string $content) |
||
50 | { |
||
51 | $comment = ''; |
||
52 | $desc = ''; |
||
53 | $currentTask = null; |
||
54 | |||
55 | $content = str_replace("\r\n", "\n", $content); |
||
56 | |||
57 | $state = 'root'; |
||
58 | $lines = explode("\n", $content); |
||
59 | |||
60 | for ($i = 0; $i < count($lines); $i++) { |
||
|
|||
61 | $line = $lines[$i]; |
||
62 | |||
63 | if (empty($line)) { |
||
64 | continue; // Skip empty lines |
||
65 | } |
||
66 | |||
67 | $m = []; |
||
68 | $match = function ($regexp) use ($line, &$m) { |
||
69 | return preg_match("#$regexp#", $line, $m); |
||
70 | }; |
||
71 | switch ($state) { |
||
72 | case 'root': |
||
73 | if ($match('^/\*\*?')) { |
||
74 | $state = 'comment'; |
||
75 | $comment .= trim_comment($line) . "\n"; |
||
76 | break; |
||
77 | } |
||
78 | if ($match('^//')) { |
||
79 | $comment .= trim_comment($line) . "\n"; |
||
80 | break; |
||
81 | } |
||
82 | if ($match('^require.+?[\'"](?<recipe>.+?)[\'"]')) { |
||
83 | $this->require[] = dirname($this->recipePath) . $m['recipe']; |
||
84 | break; |
||
85 | } |
||
86 | if ($match('^set\([\'"](?<config_name>[\w_:\-/]+?)[\'"]')) { |
||
87 | $set = new DocConfig(); |
||
88 | $set->name = $m['config_name']; |
||
89 | $set->comment = trim($comment); |
||
90 | $comment = ''; |
||
91 | $set->recipePath = $this->recipePath; |
||
92 | $set->lineNumber = $i + 1; |
||
93 | if (preg_match('#^set\(.+?,\s(?<value>.+?)\);$#', $line, $m)) { |
||
94 | $set->defaultValue = $m['value']; |
||
95 | } |
||
96 | if (preg_match('#^set\(.+?,\s\[$#', $line, $m)) { |
||
97 | $multiLineArray = "[\n"; |
||
98 | $line = $lines[++$i]; |
||
99 | while (!preg_match('/^]/', $line)) { |
||
100 | $multiLineArray .= $line . "\n"; |
||
101 | $line = $lines[++$i]; |
||
102 | } |
||
103 | $multiLineArray .= "]"; |
||
104 | $set->defaultValue = $multiLineArray; |
||
105 | } |
||
106 | if (preg_match('/^set\(.+?, function/', $line, $m)) { |
||
107 | $body = []; |
||
108 | $line = $lines[++$i]; |
||
109 | while (!preg_match('/^}\);$/', $line)) { |
||
110 | $body[] = trim($line); |
||
111 | $line = $lines[++$i]; |
||
112 | } |
||
113 | if (count($body) === 1 && preg_match('/throw new/', $body[0])) { |
||
114 | $set->comment .= "\n:::info Required\nThrows exception if not set.\n:::\n"; |
||
115 | } elseif (count($body) <= 4) { |
||
116 | $set->defaultValue = implode("\n", $body); |
||
117 | } else { |
||
118 | $set->comment .= "\n:::info Autogenerated\nThe value of this configuration is autogenerated on access.\n:::\n"; |
||
119 | } |
||
120 | } |
||
121 | $this->config[$set->name] = $set; |
||
122 | break; |
||
123 | } |
||
124 | if ($match('^desc\([\'"](?<desc>.+?)[\'"]\);$')) { |
||
125 | $desc = $m['desc']; |
||
126 | break; |
||
127 | } |
||
128 | if ($match('^task\([\'"](?<task_name>[\w_:-]+?)[\'"],\s\[$')) { |
||
129 | $task = new DocTask(); |
||
130 | $task->name = $m['task_name']; |
||
131 | $task->desc = $desc; |
||
132 | $task->comment = trim($comment); |
||
133 | $comment = ''; |
||
134 | $task->group = []; |
||
135 | $task->recipePath = $this->recipePath; |
||
136 | $task->lineNumber = $i + 1; |
||
137 | $this->tasks[$task->name] = $task; |
||
138 | $state = 'group_task'; |
||
139 | $currentTask = $task; |
||
140 | break; |
||
141 | } |
||
142 | if ($match('^task\([\'"](?<task_name>[\w_:-]+?)[\'"],')) { |
||
143 | $task = new DocTask(); |
||
144 | $task->name = $m['task_name']; |
||
145 | $task->desc = $desc; |
||
146 | $task->comment = trim($comment); |
||
147 | $comment = ''; |
||
148 | $task->recipePath = $this->recipePath; |
||
149 | $task->lineNumber = $i + 1; |
||
150 | $this->tasks[$task->name] = $task; |
||
151 | break; |
||
152 | } |
||
153 | if ($match('^<\?php')) { |
||
154 | break; |
||
155 | } |
||
156 | if ($match('^namespace Deployer;$')) { |
||
157 | $this->comment = $comment; |
||
158 | break; |
||
159 | } |
||
160 | |||
161 | $desc = ''; |
||
162 | $comment = ''; |
||
163 | break; |
||
164 | |||
165 | case 'comment': |
||
166 | if ($match('\*/\s*$')) { |
||
167 | $state = 'root'; |
||
168 | break; |
||
169 | } |
||
170 | $comment .= trim_comment($line) . "\n"; |
||
171 | break; |
||
172 | |||
173 | case 'group_task': |
||
174 | if ($match('^\s+\'(?<task_name>[\w_:-]+?)\',$')) { |
||
175 | $currentTask->group[] = $m['task_name']; |
||
176 | break; |
||
177 | } |
||
178 | $state = 'root'; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | return false; |
||
183 | } |
||
185 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: