| Conditions | 12 |
| Paths | 31 |
| Total Lines | 72 |
| Code Lines | 40 |
| 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 | foreach($this->javascript as $js) { |
||
| 60 | if(in_array(pathinfo($js, PATHINFO_FILENAME), $unbundledFilename)) { |
||
| 61 | $jsVersion = $this->getVersion($js); |
||
| 62 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($js) . '?v=' . $jsVersion . '"></script>'; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | $bundleJsCollection = $this->javascript->getArrayCopy(); |
||
| 67 | $bundleJsVersion = $this->getVersion(serialize($bundleJsCollection)); |
||
| 68 | $bundleJsFilename = 'body-' . controller()->getClassInfo()->getParameter(); |
||
| 69 | $bundleJsFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.js'; |
||
| 70 | $bundleJsMinifyFilePath = PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . $bundleJsFilename . '.min.js'; |
||
| 71 | |||
| 72 | if (is_file($bundleJsFilePath . '.map')) { |
||
| 73 | $bundleJsMap = json_decode(file_get_contents($bundleJsFilePath . '.map'), true); |
||
| 74 | // if the file version is changed delete it first |
||
| 75 | if ( ! hash_equals($bundleJsVersion, $bundleJsMap[ 'version' ])) { |
||
| 76 | unlink($bundleJsFilePath); |
||
| 77 | unlink($bundleJsFilePath . '.map'); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | if ( ! is_file($bundleJsFilePath)) { |
||
| 82 | $bundleJsMap = [ |
||
| 83 | 'version' => $bundleJsVersion, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | // Create js file |
||
| 87 | if ($bundleFileStream = @fopen($bundleJsFilePath, 'ab')) { |
||
| 88 | flock($bundleFileStream, LOCK_EX); |
||
| 89 | |||
| 90 | foreach ($this->css as $css) { |
||
|
|
|||
| 91 | if( ! in_array(pathinfo($css, PATHINFO_FILENAME), $unbundledFilename)) { |
||
| 92 | $bundleJsMap[ 'sources' ][] = $css; |
||
| 93 | fwrite($bundleFileStream, file_get_contents($css)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | flock($bundleFileStream, LOCK_UN); |
||
| 98 | fclose($bundleFileStream); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Create js map |
||
| 102 | if ($bundleFileStream = @fopen($bundleJsFilePath . '.map', 'ab')) { |
||
| 103 | flock($bundleFileStream, LOCK_EX); |
||
| 104 | |||
| 105 | fwrite($bundleFileStream, json_encode($bundleJsMap)); |
||
| 106 | |||
| 107 | flock($bundleFileStream, LOCK_UN); |
||
| 108 | fclose($bundleFileStream); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Create js file minify version |
||
| 112 | $minifyJsHandler = new JS($bundleJsFilePath); |
||
| 113 | $minifyJsHandler->minify($bundleJsMinifyFilePath); |
||
| 114 | } |
||
| 115 | |||
| 116 | if (input()->env('DEBUG_STAGE') === 'PRODUCTION') { |
||
| 117 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsMinifyFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
||
| 118 | } else { |
||
| 119 | $output[] = '<script type="text/javascript" src="' . $this->getUrl($bundleJsFilePath) . '?v=' . $bundleJsVersion . '"></script>'; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return implode(PHP_EOL, $output); |
||
| 124 | } |
||
| 125 | } |