Conditions | 49 |
Paths | 433 |
Total Lines | 271 |
Code Lines | 185 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
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 |
||
48 | public function gen(string $destination): ?string |
||
49 | { |
||
50 | foreach ($this->recipes as $recipe) { |
||
51 | // $find will try to return DocConfig for a given config $name. |
||
52 | $findConfig = function (string $name) use ($recipe): ?DocConfig { |
||
53 | if (array_key_exists($name, $recipe->config)) { |
||
54 | return $recipe->config[$name]; |
||
55 | } |
||
56 | foreach ($recipe->require as $r) { |
||
57 | if (array_key_exists($r, $this->recipes)) { |
||
58 | if (array_key_exists($name, $this->recipes[$r]->config)) { |
||
59 | return $this->recipes[$r]->config[$name]; |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | foreach ($this->recipes as $r) { |
||
64 | if (array_key_exists($name, $r->config)) { |
||
65 | return $r->config[$name]; |
||
66 | } |
||
67 | } |
||
68 | return null; |
||
69 | }; |
||
70 | $findConfigOverride = function (DocRecipe $recipe, string $name) use (&$findConfigOverride): ?DocConfig { |
||
71 | foreach ($recipe->require as $r) { |
||
72 | if (array_key_exists($r, $this->recipes)) { |
||
73 | if (array_key_exists($name, $this->recipes[$r]->config)) { |
||
74 | return $this->recipes[$r]->config[$name]; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | foreach ($recipe->require as $r) { |
||
79 | if (array_key_exists($r, $this->recipes)) { |
||
80 | return $findConfigOverride($this->recipes[$r], $name); |
||
81 | } |
||
82 | } |
||
83 | return null; |
||
84 | }; |
||
85 | // Replace all {{name}} with link to correct config declaration. |
||
86 | $replaceLinks = function (string $comment) use ($findConfig): string { |
||
87 | $output = ''; |
||
88 | $code = false; |
||
89 | foreach (explode("\n", $comment) as $i => $line) { |
||
90 | if (str_starts_with($line, '```') || str_starts_with($line, '~~~')) { |
||
91 | $code = !$code; |
||
|
|||
92 | } |
||
93 | if ($code) { |
||
94 | $output .= $line; |
||
95 | $output .= "\n"; |
||
96 | continue; |
||
97 | } |
||
98 | $output .= preg_replace_callback('#(\{\{(?<name>[\w_:\-/]+)\}\})#', function ($m) use ($findConfig) { |
||
99 | $name = $m['name']; |
||
100 | $config = $findConfig($name); |
||
101 | if ($config !== null) { |
||
102 | $md = php_to_md($config->recipePath); |
||
103 | $anchor = anchor($name); |
||
104 | return "[$name](/docs/$md#$anchor)"; |
||
105 | } |
||
106 | return "{{" . $name . "}}"; |
||
107 | }, $line); |
||
108 | $output .= "\n"; |
||
109 | } |
||
110 | return $output; |
||
111 | }; |
||
112 | $findTask = function (string $name, bool $searchOtherRecipes = true) use ($recipe): ?DocTask { |
||
113 | if (array_key_exists($name, $recipe->tasks)) { |
||
114 | return $recipe->tasks[$name]; |
||
115 | } |
||
116 | foreach ($recipe->require as $r) { |
||
117 | if (array_key_exists($r, $this->recipes)) { |
||
118 | if (array_key_exists($name, $this->recipes[$r]->tasks)) { |
||
119 | return $this->recipes[$r]->tasks[$name]; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | if ($searchOtherRecipes) { |
||
124 | foreach ($this->recipes as $r) { |
||
125 | if (array_key_exists($name, $r->tasks)) { |
||
126 | return $r->tasks[$name]; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | return null; |
||
131 | }; |
||
132 | |||
133 | $title = join(' ', array_map('ucfirst', explode('_', $recipe->recipeName))) . ' Recipe'; |
||
134 | $config = ''; |
||
135 | $tasks = ''; |
||
136 | $intro = <<<MD |
||
137 | ```php |
||
138 | require '$recipe->recipePath'; |
||
139 | ``` |
||
140 | |||
141 | [Source](/$recipe->recipePath) |
||
142 | |||
143 | |||
144 | MD; |
||
145 | if (is_framework_recipe($recipe)) { |
||
146 | $brandName = framework_brand_name($recipe->recipeName); |
||
147 | $typeOfProject = preg_match('/^symfony/i', $recipe->recipeName) ? 'Application' : 'Project'; |
||
148 | $title = "How to Deploy a $brandName $typeOfProject"; |
||
149 | |||
150 | $intro .= <<<MARKDOWN |
||
151 | Deployer is a free and open source deployment tool written in PHP. |
||
152 | It helps you to deploy your $brandName application to a server. |
||
153 | It is very easy to use and has a lot of features. |
||
154 | |||
155 | Three main features of Deployer are: |
||
156 | - **Provisioning** - provision your server for you. |
||
157 | - **Zero downtime deployment** - deploy your application without a downtime. |
||
158 | - **Rollbacks** - rollback your application to a previous version, if something goes wrong. |
||
159 | |||
160 | Additionally, Deployer has a lot of other features, like: |
||
161 | - **Easy to use** - Deployer is very easy to use. It has a simple and intuitive syntax. |
||
162 | - **Fast** - Deployer is very fast. It uses parallel connections to deploy your application. |
||
163 | - **Secure** - Deployer uses SSH to connect to your server. |
||
164 | - **Supports all major PHP frameworks** - Deployer supports all major PHP frameworks. |
||
165 | |||
166 | You can read more about Deployer in [Getting Started](/docs/getting-started.md). |
||
167 | |||
168 | |||
169 | MARKDOWN; |
||
170 | |||
171 | $map = function (DocTask $task, $ident = '') use (&$map, $findTask, &$intro): void { |
||
172 | foreach ($task->group as $taskName) { |
||
173 | $t = $findTask($taskName); |
||
174 | if ($t !== null) { |
||
175 | $intro .= "$ident* {$t->mdLink()} – $t->desc\n"; |
||
176 | if ($t->group !== null) { |
||
177 | $map($t, $ident . ' '); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | }; |
||
182 | $deployTask = $findTask('deploy'); |
||
183 | if ($deployTask !== null) { |
||
184 | $intro .= "The [deploy](#deploy) task of **$brandName** consists of:\n"; |
||
185 | $map($deployTask); |
||
186 | } |
||
187 | |||
188 | $intro .= "\n\n"; |
||
189 | |||
190 | $artifactBuildTask = $findTask('artifact:build', false); |
||
191 | $artifactDeployTask = $findTask('artifact:deploy', false); |
||
192 | if ($artifactDeployTask !== null && $artifactBuildTask !== null) { |
||
193 | $intro .= "In addition the **$brandName** recipe contains an artifact deployment.\n"; |
||
194 | $intro .= <<<MD |
||
195 | This is a two step process where you first execute |
||
196 | |||
197 | ```php |
||
198 | bin/dep artifact:build [options] [localhost] |
||
199 | ``` |
||
200 | |||
201 | to build an artifact, which then is deployed on a server with |
||
202 | |||
203 | ```php |
||
204 | bin/dep artifact:deploy [host] |
||
205 | ``` |
||
206 | |||
207 | The `localhost` to build the artifact on has to be declared local, so either add |
||
208 | ```php |
||
209 | localhost() |
||
210 | ->set('local', true); |
||
211 | ``` |
||
212 | to your deploy.php or |
||
213 | ```yaml |
||
214 | hosts: |
||
215 | localhost: |
||
216 | local: true |
||
217 | ``` |
||
218 | to your deploy yaml. |
||
219 | |||
220 | The [artifact:build](#artifact:build) command of **$brandName** consists of: |
||
221 | MD; |
||
222 | $map($artifactBuildTask); |
||
223 | |||
224 | $intro .= "\n\n The [artifact:deploy](#artifact:deploy) command of **$brandName** consists of:\n"; |
||
225 | |||
226 | $map($artifactDeployTask); |
||
227 | |||
228 | $intro .= "\n\n"; |
||
229 | } |
||
230 | } |
||
231 | if (count($recipe->require) > 0) { |
||
232 | if (is_framework_recipe($recipe)) { |
||
233 | $link = recipe_to_md_link($recipe->require[0]); |
||
234 | $intro .= "The $recipe->recipeName recipe is based on the $link recipe.\n"; |
||
235 | } else { |
||
236 | $intro .= "* Requires\n"; |
||
237 | foreach ($recipe->require as $r) { |
||
238 | $link = recipe_to_md_link($r); |
||
239 | $intro .= " * {$link}\n"; |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | if (!empty($recipe->comment)) { |
||
244 | $intro .= "\n$recipe->comment\n"; |
||
245 | } |
||
246 | if (count($recipe->config) > 0) { |
||
247 | $config .= "## Configuration\n"; |
||
248 | foreach ($recipe->config as $c) { |
||
249 | $config .= "### {$c->name}\n"; |
||
250 | $config .= "[Source](https://github.com/deployphp/deployer/blob/master/{$c->recipePath}#L{$c->lineNumber})\n\n"; |
||
251 | $o = $findConfigOverride($recipe, $c->name); |
||
252 | if ($o !== null) { |
||
253 | $md = php_to_md($o->recipePath); |
||
254 | $anchor = anchor($c->name); |
||
255 | $config .= "Overrides [{$c->name}](/docs/$md#$anchor) from `$o->recipePath`.\n\n"; |
||
256 | } |
||
257 | $config .= $replaceLinks($c->comment); |
||
258 | $config .= "\n"; |
||
259 | if ( |
||
260 | !empty($c->defaultValue) |
||
261 | && $c->defaultValue !== "''" |
||
262 | && $c->defaultValue !== '[]' |
||
263 | ) { |
||
264 | $config .= "```php title=\"Default value\"\n"; |
||
265 | $config .= $c->defaultValue; |
||
266 | $config .= "\n"; |
||
267 | $config .= "```\n"; |
||
268 | } |
||
269 | $config .= "\n\n"; |
||
270 | } |
||
271 | } |
||
272 | if (count($recipe->tasks) > 0) { |
||
273 | $tasks .= "## Tasks\n\n"; |
||
274 | foreach ($recipe->tasks as $t) { |
||
275 | $anchorTag = '{#' . anchor($t->name) . '}'; |
||
276 | $name = title($t->name); |
||
277 | $tasks .= "### $name $anchorTag\n"; |
||
278 | $tasks .= "[Source](https://github.com/deployphp/deployer/blob/master/{$t->recipePath}#L{$t->lineNumber})\n\n"; |
||
279 | $tasks .= add_tailing_dot($t->desc) . "\n\n"; |
||
280 | $tasks .= $replaceLinks($t->comment); |
||
281 | if (is_array($t->group)) { |
||
282 | $tasks .= "\n\n"; |
||
283 | $tasks .= "This task is group task which contains next tasks:\n"; |
||
284 | foreach ($t->group as $taskName) { |
||
285 | $t = $findTask($taskName); |
||
286 | if ($t !== null) { |
||
287 | $tasks .= "* {$t->mdLink()}\n"; |
||
288 | } else { |
||
289 | $tasks .= "* `$taskName`\n"; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | $tasks .= "\n\n"; |
||
294 | } |
||
295 | } |
||
296 | |||
297 | $output = <<<MD |
||
298 | <!-- DO NOT EDIT THIS FILE! --> |
||
299 | <!-- Instead edit $recipe->recipePath --> |
||
300 | <!-- Then run bin/docgen --> |
||
301 | |||
302 | # $title |
||
303 | |||
304 | $intro |
||
305 | $config |
||
306 | $tasks |
||
307 | MD; |
||
308 | |||
309 | $filePath = "$destination/" . php_to_md($recipe->recipePath); |
||
310 | if (!file_exists(dirname($filePath))) { |
||
311 | mkdir(dirname($filePath), 0o755, true); |
||
312 | } |
||
313 | $output = remove_text_emoji($output); |
||
314 | file_put_contents($filePath, $output); |
||
315 | } |
||
316 | $this->generateRecipesIndex($destination); |
||
317 | $this->generateContribIndex($destination); |
||
318 | return null; |
||
319 | } |
||
418 |