Conditions | 32 |
Paths | 17 |
Total Lines | 151 |
Code Lines | 103 |
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 |
||
42 | public function gen(string $destination) |
||
43 | { |
||
44 | foreach ($this->recipes as $recipe) { |
||
45 | // $find will try to return DocConfig for a given config $name. |
||
46 | $findConfig = function (string $name) use ($recipe): ?DocConfig { |
||
47 | if (array_key_exists($name, $recipe->config)) { |
||
48 | return $recipe->config[$name]; |
||
49 | } |
||
50 | foreach ($recipe->require as $r) { |
||
51 | if (array_key_exists($r, $this->recipes)) { |
||
52 | if (array_key_exists($name, $this->recipes[$r]->config)) { |
||
53 | return $this->recipes[$r]->config[$name]; |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | foreach ($this->recipes as $r) { |
||
58 | if (array_key_exists($name, $r->config)) { |
||
59 | return $r->config[$name]; |
||
60 | } |
||
61 | } |
||
62 | return null; |
||
63 | }; |
||
64 | $findConfigOverride = function (DocRecipe $recipe, string $name) use (&$findConfigOverride): ?DocConfig { |
||
65 | foreach ($recipe->require as $r) { |
||
66 | if (array_key_exists($r, $this->recipes)) { |
||
67 | if (array_key_exists($name, $this->recipes[$r]->config)) { |
||
68 | return $this->recipes[$r]->config[$name]; |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | foreach ($recipe->require as $r) { |
||
73 | if (array_key_exists($r, $this->recipes)) { |
||
74 | return $findConfigOverride($this->recipes[$r], $name); |
||
75 | } |
||
76 | } |
||
77 | return null; |
||
78 | }; |
||
79 | // Replace all {{name}} with link to correct config declaration. |
||
80 | $replaceLinks = function (string $comment) use ($findConfig): string { |
||
81 | return preg_replace_callback('#(\{\{(?<name>[\w_:]+)\}\})#', function ($m) use ($findConfig) { |
||
82 | $name = $m['name']; |
||
83 | $config = $findConfig($name); |
||
84 | if ($config !== null) { |
||
85 | $md = php_to_md($config->recipePath); |
||
86 | $anchor = anchor($name); |
||
87 | return "[$name](/docs/$md#$anchor)"; |
||
88 | } |
||
89 | return "{{" . $name . "}}"; |
||
90 | }, $comment); |
||
91 | }; |
||
92 | $findTask = function (string $name) use ($recipe): ?DocTask { |
||
93 | if (array_key_exists($name, $recipe->tasks)) { |
||
94 | return $recipe->tasks[$name]; |
||
95 | } |
||
96 | foreach ($recipe->require as $r) { |
||
97 | if (array_key_exists($r, $this->recipes)) { |
||
98 | if (array_key_exists($name, $this->recipes[$r]->tasks)) { |
||
99 | return $this->recipes[$r]->tasks[$name]; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | foreach ($this->recipes as $r) { |
||
104 | if (array_key_exists($name, $r->tasks)) { |
||
105 | return $r->tasks[$name]; |
||
106 | } |
||
107 | } |
||
108 | return null; |
||
109 | }; |
||
110 | |||
111 | |||
112 | $filePath = "$destination/" . php_to_md($recipe->recipePath); |
||
113 | |||
114 | $toc = ''; |
||
115 | $config = ''; |
||
116 | $tasks = ''; |
||
117 | if (count($recipe->require) > 0) { |
||
118 | $toc .= "* Require\n"; |
||
119 | foreach ($recipe->require as $r) { |
||
120 | $md = php_to_md($r); |
||
121 | $toc .= " * [`{$r}`](/docs/{$md})\n"; |
||
122 | } |
||
123 | } |
||
124 | if (count($recipe->config) > 0) { |
||
125 | $toc .= "* Config\n"; |
||
126 | $config .= "## Config\n"; |
||
127 | foreach ($recipe->config as $c) { |
||
128 | $anchor = anchor($c->name); |
||
129 | $toc .= " * [`{$c->name}`](#{$anchor})\n"; |
||
130 | $config .= "### {$c->name}\n"; |
||
131 | $config .= "[Source](/{$c->recipePath}#L{$c->lineNumber})\n\n"; |
||
132 | $o = $findConfigOverride($recipe, $c->name); |
||
133 | if ($o !== null) { |
||
134 | $md = php_to_md($o->recipePath); |
||
135 | $anchor = anchor($c->name); |
||
136 | $config .= "* Overrides [`{$c->name}`](/docs/$md#$anchor) from `$o->recipePath`\n\n"; |
||
137 | } |
||
138 | $config .= $replaceLinks($c->comment); |
||
139 | $config .= "\n\n"; |
||
140 | } |
||
141 | } |
||
142 | if (count($recipe->tasks) > 0) { |
||
143 | $toc .= "* Tasks\n"; |
||
144 | $tasks .= "## Tasks\n"; |
||
145 | foreach ($recipe->tasks as $t) { |
||
146 | $anchor = anchor($t->name); |
||
147 | $desc = ""; |
||
148 | if ($t->desc !== "") { |
||
149 | $desc = " — {$t->desc}"; |
||
150 | } |
||
151 | $toc .= " * [`{$t->name}`](#{$anchor}){$desc}\n"; |
||
152 | $tasks .= "### {$t->name}\n"; |
||
153 | $tasks .= "[Source](/{$t->recipePath}#L{$t->lineNumber})\n\n"; |
||
154 | $tasks .= $replaceLinks($t->comment); |
||
155 | if (is_array($t->group)) { |
||
156 | $tasks .= "\n\n"; |
||
157 | $tasks .= "This task is group task which contains next tasks:\n"; |
||
158 | foreach ($t->group as $taskName) { |
||
159 | $t = $findTask($taskName); |
||
160 | if ($t !== null) { |
||
161 | $md = php_to_md($t->recipePath); |
||
162 | $anchor = anchor($t->name); |
||
163 | $tasks .= "* [`$taskName`](/docs/$md#$anchor)\n"; |
||
164 | } else { |
||
165 | $tasks .= "* `$taskName`\n"; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | $tasks .= "\n\n"; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $output = <<<MD |
||
174 | <!-- DO NOT EDIT THIS FILE! --> |
||
175 | <!-- Instead edit {$recipe->recipePath} --> |
||
176 | <!-- Then run bin/docgen --> |
||
177 | |||
178 | # {$recipe->recipeName} |
||
179 | |||
180 | [Source](/{$recipe->recipePath}) |
||
181 | |||
182 | {$recipe->comment} |
||
183 | |||
184 | {$toc} |
||
185 | {$config} |
||
186 | {$tasks} |
||
187 | MD; |
||
188 | |||
189 | if (!file_exists(dirname($filePath))) { |
||
190 | mkdir(dirname($filePath), 0755, true); |
||
191 | } |
||
192 | file_put_contents($filePath, $output); |
||
193 | } |
||
218 |