| Conditions | 9 |
| Paths | 25 |
| Total Lines | 74 |
| Code Lines | 42 |
| 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 |
||
| 26 | public function getAssets(AssetType $type, array $assetsCollection, Environment $environment): array |
||
| 27 | { |
||
| 28 | $cacheDir = $environment->getCompileDir() . '/.cache'; |
||
| 29 | |||
| 30 | $logger = $environment->getLogger(); |
||
| 31 | |||
| 32 | foreach ($assetsCollection as $asset) { |
||
| 33 | if (!$asset->isValid()) { |
||
| 34 | continue; |
||
| 35 | } |
||
| 36 | |||
| 37 | $assetAttributeCollection = $asset->getAttributeCollection(); |
||
| 38 | |||
| 39 | if ($asset->isUrl()) { |
||
| 40 | $assetAttributeCollection->set( |
||
| 41 | $type->htmlAttribute(), |
||
| 42 | Helpers::addVersionToPath($asset->getPath(), $environment->getVersionQuery()) |
||
| 43 | ); |
||
| 44 | continue; |
||
| 45 | } |
||
| 46 | |||
| 47 | $link = str_replace( |
||
| 48 | [ |
||
| 49 | $environment->getCompileDir(), |
||
| 50 | $environment->getProjectDir() |
||
| 51 | ], |
||
| 52 | '', |
||
| 53 | $asset->getPath() |
||
| 54 | ); |
||
| 55 | |||
| 56 | $cacheFile = $cacheDir . '/' . $asset->getId(); |
||
| 57 | |||
| 58 | if (!file_exists($cacheFile) || (filemtime($cacheFile) + $environment->getCacheTime()) < time()) { |
||
| 59 | // feel AssetOption::SYMLINKS |
||
| 60 | (new Reader($asset, $environment))->replaceRelativeUrls(); |
||
| 61 | createFile($cacheFile); |
||
| 62 | $logger->info(sprintf('Create file: %s', $cacheFile)); |
||
| 63 | } |
||
| 64 | |||
| 65 | try { |
||
| 66 | /** @infection-ignore-all */ |
||
| 67 | $asset->getOptions()->setOption( |
||
| 68 | AssetOption::SYMLINKS, |
||
| 69 | array_merge( |
||
| 70 | [$environment->getCompileDir() . $link => $asset->getPath()], |
||
| 71 | $asset->getOptions()->getSymlinks() |
||
| 72 | ) |
||
| 73 | ); |
||
| 74 | |||
| 75 | foreach ($asset->getOptions()->getSymlinks() as $optLink => $optTarget) { |
||
| 76 | if (makeSymlink($optLink, $optTarget)) { |
||
| 77 | $logger->info(sprintf('Created symlink: %s', $optLink)); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } catch (Exception $e) { |
||
| 81 | $logger->error($e->getMessage()); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** @infection-ignore-all */ |
||
| 85 | $assetAttributeCollection->set( |
||
| 86 | $type->htmlAttribute(), |
||
| 87 | Helpers::addVersionToPath( |
||
| 88 | $environment->getBaseUrl() . str_replace( |
||
| 89 | DIRECTORY_SEPARATOR, |
||
| 90 | '/', |
||
| 91 | $link |
||
| 92 | ), |
||
| 93 | $environment->getVersionQuery() |
||
| 94 | ) |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return array_filter($assetsCollection, function (Asset $asset) { |
||
| 99 | return $asset->isValid(); |
||
| 100 | }); |
||
| 105 |