| Conditions | 12 |
| Paths | 48 |
| Total Lines | 36 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 1 | 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 |
||
| 60 | public function init(): void |
||
| 61 | { |
||
| 62 | // See if the $pluginDevServerEnvVar env var exists, and if not, don't run off of the dev server |
||
| 63 | $useDevServer = (bool)App::env($this->pluginDevServerEnvVar); |
||
| 64 | if ($useDevServer === false) { |
||
| 65 | $this->useDevServer = false; |
||
| 66 | } |
||
| 67 | parent::init(); |
||
| 68 | // If we're in a plugin, make sure the caches are unique |
||
| 69 | if ($this->assetClass) { |
||
| 70 | $this->cacheKeySuffix = $this->assetClass; |
||
| 71 | } |
||
| 72 | if ($this->devServerRunning()) { |
||
| 73 | $this->invalidateCaches(); |
||
| 74 | } |
||
| 75 | // If we have no asset bundle class, or the dev server is running, don't swap in our `/cpresources/` paths |
||
| 76 | if (!$this->assetClass || $this->devServerRunning()) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | // The Vite service is generally only needed for CP requests & previews, save a db write, see: |
||
| 80 | // https://github.com/nystudio107/craft-plugin-vite/issues/27 |
||
| 81 | $request = Craft::$app->getRequest(); |
||
| 82 | if (!$this->useForAllRequests && !$request->getIsConsoleRequest()) { |
||
| 83 | if (!$request->getIsCpRequest() && !$request->getIsPreview() && !in_array($request->getSegment(1), $this->firstSegmentRequests, true)) { |
||
| 84 | return; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | // Map the $manifestPath and $serverPublic to the hashed `/cpresources/` path & URL for our AssetBundle |
||
| 88 | $bundle = new $this->assetClass(); |
||
| 89 | $baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
||
| 90 | $bundle->sourcePath, |
||
| 91 | true |
||
| 92 | ); |
||
| 93 | $this->manifestPath = FileHelper::createUrl($bundle->sourcePath, self::MANIFEST_FILE_NAME); |
||
| 94 | if ($baseAssetsUrl !== false) { |
||
| 95 | $this->serverPublic = $baseAssetsUrl; |
||
| 96 | } |
||
| 99 |