Conditions | 26 |
Paths | 500 |
Total Lines | 112 |
Code Lines | 67 |
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 |
||
186 | public function __toString() |
||
187 | { |
||
188 | $output = []; |
||
189 | |||
190 | // Render fonts |
||
191 | if ($this->fonts->count()) { |
||
192 | |||
193 | $bundleFontsFile = $this->bundleFile('assets' . DIRECTORY_SEPARATOR . 'fonts.css', |
||
194 | implode(PHP_EOL, $this->fonts)); |
||
195 | |||
196 | if (is_file($bundleFontsFile[ 'filePath' ])) { |
||
197 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
198 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">'; |
||
199 | } else { |
||
200 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleFontsFile[ 'url' ] . '?v=' . $bundleFontsFile[ 'version' ] . '">'; |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | $unbundledFilenames = ['app', 'app.min', 'theme', 'theme.min']; |
||
206 | |||
207 | if (presenter()->page->file instanceof \SplFileInfo) { |
||
208 | if (presenter()->page->file->getFilename() === 'index') { |
||
209 | $bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName(); |
||
210 | } else { |
||
211 | $bundleFilename = 'head-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename(); |
||
212 | } |
||
213 | } elseif (services()->has('controller')) { |
||
214 | $bundleFilename = 'head-' . controller()->getParameter(); |
||
215 | |||
216 | if (controller()->getRequestMethod() !== 'index') { |
||
217 | $bundleFilename .= '-' . controller()->getRequestMethod(); |
||
218 | } |
||
219 | } else { |
||
220 | $bundleFilename = 'head-' . uniqid(); |
||
221 | } |
||
222 | |||
223 | $bundleFilename = 'assets' . DIRECTORY_SEPARATOR . $bundleFilename; |
||
224 | |||
225 | // Render style |
||
226 | if ($this->styles->count()) { |
||
227 | $bundleStyleSources = []; |
||
228 | |||
229 | foreach ($this->styles as $style) { |
||
230 | if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) { |
||
231 | $fileVersion = $this->getVersion(filemtime($style)); |
||
232 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->getUrl($style) . '?v=' . $fileVersion . '">'; |
||
233 | } elseif (in_array(pathinfo($style, PATHINFO_FILENAME), ['module', 'module.min'])) { |
||
234 | $modulePublicFile = $this->publishFile($style); |
||
235 | |||
236 | if (is_file($modulePublicFile[ 'filePath' ])) { |
||
237 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
238 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">'; |
||
239 | } else { |
||
240 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '">'; |
||
241 | } |
||
242 | } |
||
243 | } else { |
||
244 | $bundleStyleSources[] = $style; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | if (count($bundleStyleSources)) { |
||
249 | $bundleStylePublicFile = $this->bundleFile($bundleFilename . '.css', $bundleStyleSources); |
||
250 | |||
251 | if (is_file($bundleStylePublicFile[ 'filePath' ])) { |
||
252 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
253 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">'; |
||
254 | } else { |
||
255 | $output[] = '<link rel="stylesheet" type="text/css" media="all" href="' . $bundleStylePublicFile[ 'url' ] . '?v=' . $bundleStylePublicFile[ 'version' ] . '">'; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | // Render javascript |
||
262 | if ($this->javascripts->count()) { |
||
263 | $bundleJavascriptSources = []; |
||
264 | |||
265 | foreach ($this->javascripts as $javascript) { |
||
266 | if (in_array(pathinfo($style, PATHINFO_FILENAME), $unbundledFilenames)) { |
||
267 | $fileVersion = $this->getVersion(filemtime($javascript)); |
||
268 | $output[] = '<script type="text/javascript" id="js-'.pathinfo($javascript, PATHINFO_FILENAME).'" src="' . $this->getUrl($javascript) . '?v=' . $fileVersion . '"></script>'; |
||
269 | } elseif (in_array(pathinfo($javascript, PATHINFO_FILENAME), ['module', 'module.min'])) { |
||
270 | $modulePublicFile = $this->publishFile($javascript); |
||
271 | |||
272 | if (is_file($modulePublicFile[ 'filePath' ])) { |
||
273 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
274 | $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'minify' ][ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
||
275 | } else { |
||
276 | $output[] = '<script type="text/javascript" id="js-module" src="' . $modulePublicFile[ 'url' ] . '?v=' . $modulePublicFile[ 'version' ] . '"></script>'; |
||
277 | } |
||
278 | } |
||
279 | } else { |
||
280 | $bundleJavascriptSources[] = $javascript; |
||
281 | } |
||
282 | } |
||
283 | |||
284 | if (count($bundleJavascriptSources)) { |
||
285 | $bundleJavascriptPublicFile = $this->bundleFile($bundleFilename . '.js', $bundleJavascriptSources); |
||
286 | |||
287 | if (is_file($bundleJavascriptPublicFile[ 'filePath' ])) { |
||
288 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
289 | $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'minify' ][ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
||
290 | } else { |
||
291 | $output[] = '<script type="text/javascript" id="js-bundle" src="' . $bundleJavascriptPublicFile[ 'url' ] . '?v=' . $bundleJavascriptPublicFile[ 'version' ] . '"></script>'; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return implode(PHP_EOL, $output); |
||
298 | } |
||
299 | } |