| Conditions | 16 |
| Paths | 226 |
| Total Lines | 89 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 52 | public function __toString() |
||
| 53 | { |
||
| 54 | $output = []; |
||
| 55 | $unbundledFilename = ['app', 'app.min', 'theme', 'theme.min']; |
||
| 56 | |||
| 57 | // Render js |
||
| 58 | if ($this->javascript->count()) { |
||
| 59 | $bundleJsContents = []; |
||
| 60 | |||
| 61 | foreach ($this->javascript as $js) { |
||
| 62 | if (in_array(pathinfo($js, PATHINFO_FILENAME), $unbundledFilename)) { |
||
| 63 | $jsVersion = $this->getVersion($js); |
||
| 64 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>'; |
||
| 65 | } else { |
||
| 66 | $bundleJsMap[ 'sources' ][] = $js; |
||
| 67 | $bundleJsContents[] = file_get_contents($js); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | // Bundled Js |
||
| 72 | $bundleJsVersion = $this->getVersion(serialize($bundleJsContents)); |
||
| 73 | |||
| 74 | if(presenter()->page->file instanceof \SplFileInfo) { |
||
|
|
|||
| 75 | if(presenter()->page->file->getFilename() === 'index') { |
||
| 76 | $bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName(); |
||
| 77 | } else { |
||
| 78 | $bundleJsFilename = 'body-' . presenter()->page->file->getDirectoryInfo()->getDirName() . '-' . presenter()->page->file->getFilename(); |
||
| 79 | } |
||
| 80 | } elseif(services()->has('controller')) { |
||
| 81 | $bundleJsFilename = 'body-' . controller()->getParameter(); |
||
| 82 | |||
| 83 | if(controller()->getRequestMethod() !== 'index') { |
||
| 84 | $bundleJsFilename.= '-' . controller()->getRequestMethod(); |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $bundleJsFilename = 'body-' . md5($bundleJsVersion); |
||
| 88 | } |
||
| 89 | |||
| 90 | $bundlePublicDir = modules()->current()->getPublicDir() . 'assets' . DIRECTORY_SEPARATOR; |
||
| 91 | $bundleJsFilePath = $bundlePublicDir . $bundleJsFilename . '.js'; |
||
| 92 | $bundleJsMinifyFilePath = $bundlePublicDir . $bundleJsFilename . '.min.js'; |
||
| 93 | |||
| 94 | if (is_file($bundleJsFilePath . '.map')) { |
||
| 95 | $bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true); |
||
| 96 | // if the file version is changed delete it first |
||
| 97 | if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) { |
||
| 98 | unlink($bundleJsFilePath); |
||
| 99 | unlink($bundleJsFilePath . '.map'); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | if ( ! is_file($bundleJsFilePath)) { |
||
| 104 | $bundleJsMap[ 'version' ] = $bundleJsVersion; |
||
| 105 | |||
| 106 | // Create css file |
||
| 107 | if (count($bundleJsContents)) { |
||
| 108 | if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) { |
||
| 109 | flock($bundleFileStream, LOCK_EX); |
||
| 110 | fwrite($bundleFileStream, implode(PHP_EOL, $bundleJsContents)); |
||
| 111 | flock($bundleFileStream, LOCK_UN); |
||
| 112 | fclose($bundleFileStream); |
||
| 113 | |||
| 114 | // Create css map |
||
| 115 | if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) { |
||
| 116 | flock($bundleFileStream, LOCK_EX); |
||
| 117 | |||
| 118 | fwrite($bundleFileStream, json_encode($bundleJsMap)); |
||
| 119 | |||
| 120 | flock($bundleFileStream, LOCK_UN); |
||
| 121 | fclose($bundleFileStream); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Create javascript file minify version |
||
| 125 | $minifyJsHandler = new JS($bundleJsFilePath); |
||
| 126 | $minifyJsHandler->minify($bundleJsMinifyFilePath); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if(is_file($bundleJsFilePath)) { |
||
| 132 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
| 133 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
||
| 134 | } else { |
||
| 135 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return implode(PHP_EOL, $output); |
||
| 141 | } |
||
| 142 | } |